remove trailing zeros from output - c

So my program works and compiles it does what I ask.
But my program's output has these trailing zeros that I would like to eliminate.
It's not like it bothers me but I definitely would like to have it cleaned up a little.
If anyone could give me a little insight on how to eliminate the trailing zeros your help,
would be greatly appreciated.
#include "stdafx.h"
#include "stdio.h"
#define BPR 10 // Basic Pay Rate is $10.00/hr.
#define OTPR 15 // Over Time is time and a half.
#define OT 40 // Overtime is after 40 hours.
#define RATE1 .15 // Tax Rate 15%.
#define RATE2 .20 // Tax Rate 20%.
#define RATE3 .25 // Tax Rate 25%.
#define LIMIT1 300.00 // The first 300.00.
#define LIMIT2 200.00 // 200 after the first 300.
int main(void)
{
int hours;
double tax;
double gross;
double taxes1=0,taxes2=0,taxes3=0;
double net;
double hold1=0,hold2=0,hold3=0;
printf("Please enter hours worked: ");
scanf_s("%i", &hours);
if(hours < OT)
gross=hours*BPR;
else
gross=((hours-OT)*OTPR+(OT*BPR));
if(gross > LIMIT2 && gross < LIMIT1)
taxes1=gross*RATE2, hold1=gross-taxes1;
if(gross > LIMIT1)
taxes2=gross*RATE1, hold2=gross-taxes2;
if(gross < LIMIT2)
taxes3=gross*RATE3, hold3=gross-taxes3;
if(gross > 0)
{
net=(hold1+hold2+hold3);
tax=(taxes1+taxes2+taxes3);
}
printf("Your Net Pay is %f\n", net);
printf("Your Gross Pay was %f\n", gross);
printf("Your Taxes paid are %f\n", tax);
return 0;
}
if 65 was put in for the hours variable the output would read:
Your Net pay is 828.750000
Your Gross Pay was 975.000000
Your Taxes paid are 146.250000
as you can see there are a lot of zeros I would love to have disappear please help ?

Use %.2f as the output format.

You can specify how many positions you want to display after the decimal point using . followed by the number of positions between % and f like so: printf("Your Net Pay is %.2f\n", net);. There are other formatting options that can be used with format specifiers and you can read more on them in the man pages by doing man printf since printf is not just a C function.

Related

Issue generating program output to .txt file

I am currently enrolled in college and taking a programming course. The program seems to function correctly, however in order to submit my lab I need to provide the output via .txt file. When I go to find the file, it comes up blank. I am very new to coding and would appreciate any help in the simplest form of understanding. I have attached my program below. I am referencing my books "Focus on Fundamentals of Programming with C" and "Problem Solving and Program Design in C"
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define SECS_PER_HOUR 3600 //1 hour = 60 minutes in an hour * 60 seconds in a minute =
3600 seconds in 1 hour
#define METERS_PER_MILE 1600
FILE *fp;
int main()
{
double distance, time, speed_mph, speed_mps;
fopen_s(&fp, "csis.txt", "w");
scanf("%lf", &distance);
scanf("%lf", &time);
speed_mph = distance / time;
speed_mps = (speed_mph * METERS_PER_MILE) / SECS_PER_HOUR;
printf("\nThe speed is %.2f miles per hour. \n", speed_mph);
printf(fp, "\nThe speed is %.2f miles per hour. \n", speed_mph);
printf("The speed is %.2f meters per second.", speed_mps);
printf(fp, "The speed is %.2f meters per second.", speed_mps);
fclose(fp);
return 0;
}
Use fprintf to write to the file.
printf("\nThe speed is %.2f miles per hour. \n", speed_mph);
fprintf(fp, "\nThe speed is %.2f miles per hour. \n", speed_mph);
printf("The speed is %.2f meters per second.", speed_mps);
fprintf(fp, "The speed is %.2f meters per second.", speed_mps);

Shipping calculator not returning correct results

The assignment is to write a shipping calculator using the following information.
Packages above 50 pounds will not be shipped. You need to write a program in C that calculates the shipping charge.
The shipping rates are based on per 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same rate as 900 miles or 1000 miles.
Here are the shipping charges -
Package Weight Rate per 500 miles shipped
Less than or equal to 10 pounds $3.00
More than 10 pounds but less than or equal to 50 pounds $5.00
If the shipping distance is more than 1000 miles, there is an additional charge of $10 per package shipped.
I originally started writing the program using double but wanted to used a trick I had seen on here to always force the program to round up for the shipping charges by adding 499 to distance then dividing by 500.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Shipping, packageWeight, packageDistance, packagePrice;
printf("Enter the weight of the package: \n");
scanf("%d", &packageWeight);
printf("The weight you have entered is %.2d\n", packageWeight);
if (packageWeight <= 10 )
Shipping = 3.00;
if (packageWeight <= 50 && packageWeight > 10 )
Shipping = 5.00;
if (packageWeight > 50.0) {
puts("Sorry, we only ship packages of 50 pounds or less.");
return 0;
}
printf("How far are you sending the package? \n");
scanf("%d", &packageDistance);
printf("The distance you entered is %.2d\n", packageDistance);
if (packageDistance <= 1000){
packagePrice = (packageDistance + 499 / 500) * Shipping;
printf("The shipping charge is %.2d \n", packagePrice);
system("pause");
}
if (packageDistance > 1000) {
packagePrice = (packageDistance + 499 / 500) * Shipping + 10.00;
printf("The shipping charge is %.2d \n", packagePrice);
system("PAUSE");
}
}
With a weight of 10 and distance of 501 the output should be a shipping charge of 6 but was 1503
Based on a a weight of 20 and a distance of 1001 the program should output a shipping charge of 25 but was 5000
You have mess with integers / doubles, output, expressions, etc.
Let me show you why you get 1503:
if (packageDistance <= 1000){
packagePrice = (packageDistance + 499 / 500) * Shipping;
printf("The shipping charge is %.2d \n", packagePrice);
system("pause");
}
Your packageDistance is 501 then if statement is true and you get inside.
Then you setup package price. First you get packageDistance which is 501. Then you add it with 499/500 which is 0 since 499 < 500 and you attempt to get integer part of division (as per C/C++ standard). The sum of 501 and 0 is 501.
Now, you multiply it with Shipping. The value you set is 3.0, but smart C convert it to integer 3. 501 * 3 is 1503 and you get the result.
Finally, you try to sent output as %.2d. This is nonsense and I am surprised C shows something at all, but I believe it just see d and ignores .2
This is what you have to do:
Understand your business logic (you do)
Be careful with integers and floating point numbers, read C documentation or send questions here if you do not understand something.
Make sure you use brackets when needed: a + b / c is not the same as (a + b) / c
Make sure you do not use integer division if you mean to use "regular" division.
Make sure you use %d for integers and %.2f for floating point numbers when appropriate.

Breaking an if statement inside a for-loop

How to get loop display to end after execution where gallons < 100 and hours < 24. I'm able to get the "The fish died after ... hours" but unsure how to stop the first printf() in the for loop from executing.
I've tried using a break statement indented in the if statement, but that only affects the for loop.
#include <stdio.h>
int main()
{
double add, vol = 500;
int hour;
printf("Please enter additional water added per hour: ");
scanf("%lf", &add);
for (hour = 1; hour <= 24; hour++)
{
vol = (vol * 0.90) + add;
printf("The volume is %.2f gallons after %d hours.\n", vol, hour);
if (hour <= 23 && vol < 100)
printf("The fish died after %d hours.", hour);
else if (hour == 24 && vol >= 100)
printf("Alas, The fish who lived.");
}
return 0;
}
Expected result:
Please enter additional water added per hour: 6
The volume is 456.00 gallons after 1 hours.
The volume is 416.40 gallons after 2 hours.
The volume is 380.76 gallons after 3 hours.
...
The volume is 103.33 gallons after 22 hours.
The volume is 99.00 gallons after 23 hours.
The fish died after 23 hours.
Actual result:
Please enter additional water added per hour: 6
The volume is 456.00 gallons after 1 hours.
The volume is 416.40 gallons after 2 hours.
The volume is 380.76 gallons after 3 hours.
...
The volume is 103.33 gallons after 22 hours.
The volume is 99.00 gallons after 23 hours.
The fish died after 23 hours. The volume is 95.10 gallons after 24 hours.
C is not Python, indentation is not significant syntactically. Compound statements must be enclosed in {..}:
if (hour <= 23 && vol < 100)
{
printf("The fish died after %d hours.", hour);
break ;
}
else if (hour == 24 && vol >= 100)
{
printf("Alas, The fish who lived.");
}
I suggest you always use {..} for conditional or loop blocks, even for single statements. It makes maintenance simpler.
However, break; is arguably a rather inelegant and poorly structured way of exiting a loop (along with continue). A better structured solution is to terminate the loop by the loop constraint alone. In this case this can be done by:
for( hour = 1;
vol > 100 && hour <= 24;
hour++)
There is the overhead of an additional test but in more complex code than this, with perhaps multiple breaks, it can become difficult to maintain, debug and comprehend.
You are still having a bit of problem figuring out your loop control. You are also inviting an Endless Loop if a matching failure occurs during input because you fail to check the return of scanf (probably one of the biggest pitfalls that new C programmers fall into). Try it. Type "one gallon" at your prompt for input and see what happens. Further, after checking the return, you have to handle 3-cases:
scanf can be used, if used correctly. This means you are responsible for checking the return of scanf every time.
(return == EOF) the user canceled input by generating a manual EOF by pressing Ctrl+d (or on windows Ctrl+z, but see CTRL+Z does not generate EOF in Windows 10 (early versions));
(return < expected No. of conversions) a matching or input failure occurred. For a matching failure you must account for every character left in your input buffer. (scan forward in the input buffer reading and discarding characters until a '\n' or EOF is found); and finally
(return == expected No. of conversions) indicating a successful read -- it is then up to you to check whether the input meets any additional criteria (e.g. positive integer, positive floating-point, within a needed range, etc..).
Onto your loop logic. Given your example and your question "Breaking an if statement inside a for-loop", I think I know where you want to go -- and you can get there with one of the simplest, least used and frankly, mandatory, expressions for nested loop control. The good ole goto statement. It still has a place in programming, and while it should not be overly used, there are a few circumstances, such as those above, and in fact you circumstance, where it is fine to use the goto to jump out of a loop and pass control a few lines down. (the limitation is a longjmp -- you don't want to use it to jump out of a function)
For example, if I understand what you are going for in fish-survival, you can clean up your logic and incorporate the scanf validations by putting the pieces together similar to the following:
#include <stdio.h>
int main (void) {
double add, vol = 500;
int hour;
for (;;) { /* loop continually until valid input is received */
int rtn; /* varible for scanf return - EVERY TIME */
/* there is no conversion taking place, fputs will do */
fputs ("Please enter additional water added per hour: ", stdout);
if ((rtn = scanf ("%lf", &add)) == EOF) { /* handle EOF case */
fputs ("(user canceled input)\n", stderr);
return 1;
}
else if (rtn == 0) { /* handle matching failure */
fputs ("error: invalid double input.\n", stderr);
/* empty all characters from stdin before next input */
for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}
}
else /* handle good input */
break;
}
for (hour = 0; hour < 24; hour++) { /* loop 0 < 24 */
vol = (vol * 0.90) + add; /* adjust output hour + 1 below */
printf ("The volume is %.2f gallons after %d hours.\n", vol, hour+1);
if (vol < 100) { /* don't forget the '\n' */
printf ("The fish died after %d hours.\n", hour + 1);
goto deadfish; /* jump to deadfish label */
}
}
puts ("Alas, The fish who lived."); /* again, no conversion */
deadfish:; /* good ole goto label */
return 0;
}
(note: generally loop in C will run 0 < n and you adjust the output as required above. While this is not mandatory, it is very much by convention, and accommodates arrays being zero-indexed in C, as well as the hours on a clock that run 0 -> 23, not 1 - > 24. The title of the movie "Zero Dark Thirty" is a good analogy (e.g. zero hour thirty minutes or 12:30 am.) You can't get there looping 1 <= 24, so those limits fail to model the concept of time correctly)
Notice above the use of the goto. It is used in conjunction with the vol < 100 check. If vol < 100 tests TRUE, then control is passed to the label associated with the goto statement. (the label being deadfish:; here). This provides several benefits here. Your for loop can simply track time -- the hours the tank is leaking up to 24. Note how the volume falling below 100 causes control to jump over the living fish output which would execute on normal loop exit.
While you may be able to get your desired dead fish output by incorporating the if (vol < 100) /* print dead fish */; break; else if ... it becomes very difficult to get your /* print live fish */ method on normal loop exit that way without checking the loop counter again as part of the else if clause. The goto provides an elegant solution to just that problem by allowing you to pass control over the /* print live fish */ case when the volume falls below the limit, while at the same time preserving the normal loop exit without having to test your loop counter variable a second time within the loop itself.
Example Use/Output
Make sure your program can handle a cat stepping on the keyboard for every user input. You must Validate every time, e.g.
$ ./bin/deadfish
Please enter additional water added per hour: one gallon
error: invalid double input.
Please enter additional water added per hour: 1
The volume is 451.00 gallons after 1 hours.
The volume is 406.90 gallons after 2 hours.
The volume is 367.21 gallons after 3 hours.
The volume is 331.49 gallons after 4 hours.
The volume is 299.34 gallons after 5 hours.
The volume is 270.41 gallons after 6 hours.
The volume is 244.37 gallons after 7 hours.
The volume is 220.93 gallons after 8 hours.
The volume is 199.84 gallons after 9 hours.
The volume is 180.85 gallons after 10 hours.
The volume is 163.77 gallons after 11 hours.
The volume is 148.39 gallons after 12 hours.
The volume is 134.55 gallons after 13 hours.
The volume is 122.10 gallons after 14 hours.
The volume is 110.89 gallons after 15 hours.
The volume is 100.80 gallons after 16 hours.
The volume is 91.72 gallons after 17 hours.
The fish died after 17 hours.
(cat on keyboard test passed...)
The closest dead fish to 100 gallons:
$ ./bin/deadfish
Please enter additional water added per hour: 6.53
The volume is 456.53 gallons after 1 hours.
The volume is 417.41 gallons after 2 hours.
The volume is 382.20 gallons after 3 hours.
The volume is 350.51 gallons after 4 hours.
The volume is 321.99 gallons after 5 hours.
The volume is 296.32 gallons after 6 hours.
The volume is 273.22 gallons after 7 hours.
The volume is 252.42 gallons after 8 hours.
The volume is 233.71 gallons after 9 hours.
The volume is 216.87 gallons after 10 hours.
The volume is 201.71 gallons after 11 hours.
The volume is 188.07 gallons after 12 hours.
The volume is 175.79 gallons after 13 hours.
The volume is 164.75 gallons after 14 hours.
The volume is 154.80 gallons after 15 hours.
The volume is 145.85 gallons after 16 hours.
The volume is 137.80 gallons after 17 hours.
The volume is 130.55 gallons after 18 hours.
The volume is 124.02 gallons after 19 hours.
The volume is 118.15 gallons after 20 hours.
The volume is 112.86 gallons after 21 hours.
The volume is 108.11 gallons after 22 hours.
The volume is 103.83 gallons after 23 hours.
The volume is 99.97 gallons after 24 hours.
The fish died after 24 hours.
(3 - 100th of a gallon dead)
The first live fish:
$ ./bin/deadfish
Please enter additional water added per hour: 6.54
The volume is 456.54 gallons after 1 hours.
The volume is 417.43 gallons after 2 hours.
The volume is 382.22 gallons after 3 hours.
The volume is 350.54 gallons after 4 hours.
The volume is 322.03 gallons after 5 hours.
The volume is 296.36 gallons after 6 hours.
The volume is 273.27 gallons after 7 hours.
The volume is 252.48 gallons after 8 hours.
The volume is 233.77 gallons after 9 hours.
The volume is 216.94 gallons after 10 hours.
The volume is 201.78 gallons after 11 hours.
The volume is 188.14 gallons after 12 hours.
The volume is 175.87 gallons after 13 hours.
The volume is 164.82 gallons after 14 hours.
The volume is 154.88 gallons after 15 hours.
The volume is 145.93 gallons after 16 hours.
The volume is 137.88 gallons after 17 hours.
The volume is 130.63 gallons after 18 hours.
The volume is 124.11 gallons after 19 hours.
The volume is 118.24 gallons after 20 hours.
The volume is 112.95 gallons after 21 hours.
The volume is 108.20 gallons after 22 hours.
The volume is 103.92 gallons after 23 hours.
The volume is 100.07 gallons after 24 hours.
Alas, The fish who lived.
(saved by 7 - 100th of a gallon -- I'll leave it to you to bisect further into the 1000th of a gallon if you need to)
Look things over and let me know if you have further questions. Put the goto into you C toolbox knowing it should not be over-used, but that it has a few uses where it can provide the optimal solution, and in the case of breaking nested loop, is the only tool in your toolbox that can do it.

Code is ignoring IF command in C, Issues with FOR

I had posted on here before, but I was never able to get the help I needed.
I'm working on a school project and I can not get my program to work properly.
The program should prompt the user to enter the number of gallons used and
the number of miles driven for each of the 3 tanks of gas. The program should
then calculate and display the miles per gallon obtained for each tank. Once
processing is complete for the 3 tanks, the program will calculate the overall
mileage(total gallons / total miles) and display a friendly "Goodbye" message.
The issue i am having is that I can not get it to display to OVERALL Millage. it ends after looping 3 times.
I know different loop statements need conditions to be met, but I cant get the FOR loop to work properly. Im getting really frustrated, cause I know this should not be this hard.
Code
#include <stdio.h>
int main(void)
{
int miles,i=3;
float gallons, mg, overall = 0, avg = 0;
while(i>0)
{
printf("Enter the gallons used: ");
scanf("%f", &gallons);
printf("Enter miles driven: ");
scanf("%d", &miles);
mg = miles/gallons;
printf("The miles/gallon for this tank was : %f\n", mg);
overall += miles;
avg += gallons;i--;
}
if(gallons == 0)
{
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
exit(0);
}
return 0;
}
If I read your code correctly, then what is preventing the overall mileage from being printed is the following final if statement:
if (gallons == 0)
If you remove it, then the overall mileage should print. Use this pattern:
while (i > 0)
{
// your while loop here
}
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
exit(0);
This if (if (gallons == 0) {})block is out of while loop.
First, you need to move the if loop inside while loop.
and this if condition should be for variable i as follow and not for gallons.
if (i == 0)
{
printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
}
In this case, after 3 iteration, value of i will be 0 so it will enter into the if block and calculate and print the overall miles/gallon.
Adding to Tim Biegeleisen's answer:
mg = miles/gallons;
What if gallons equals to 0? e.g. 0 miles for 0 gallons
This will lead to floating point exception.
A simple if-else can solve this problem!
if(!gallons)
mg = 0;
else
mg = miles/gallons;

Invalid operands to binary & (have 'float *' and 'float')

I can't figure out what I need to do to get my code to work, I've tried a few things but the same error keeps occurring. I'm not sure how else to proceed, the main issue of the code appears to be converting the hours and minutes into only hours. I know this is a very basic question, but I'm a beginner and I can't seem to get a solution.
// freezer.c
// Estimates the temperature in a freezer given the elapsed time since a power failure.
#include <stdio.h>
#include <math.h>
int main(void) {
float dec_time, // input - time in hours and minutes.
temperature, // output - temperature in degrees celsius
hours, // input for dec_time
minutes; // input for dec_time
/* Get the time in hours and minutes */
printf("hours and minutes since power failure: ");
scanf("%lf%lf", &hours &minutes);
/* Convert the time in hours and minutes into only hours in real number */
dec_time = hours + (minutes / 60.0);
// Using time via an equation to estimate the temperature
temperature = ((4 * dec_time * dec_time) / (dec_time + 2)) - 20;
// Display the temperature in degrees celsius
printf("Temperature in freezer %9.2f.\n", temperature);
return 0;
}
Any explanation anyone can give to give me insight on this would be greatly appreciated.
Edit: when I added the comma to the scanf() statement in the code on my computer, the primary compilation error in the title was resolved. I also changed the %lf to %f, but now when I key in a single digit to a.out, e.g. 3, the program does not compute until I key in q!.
Change scanf("%lf%lf", &hours &minutes) to scanf("%f%f", &hours, &minutes). No 'l', #melpomene add comma #Anton Malyshev.
Also recommend to check result to see if it is 2. (2 fields successfully scanned).
if (2 != scanf("%f%f", &hours, &minutes)) {
puts("Input error");
exit(1);
}
scanf("%lf%lf", &hours &minutes);
^ comma needed
You missed a comma ,.
Re-write as follows-
scanf("%f%f",&hours,&minutes); // make sure you use only %f and not %lf
You missed comma, that's how it should be: scanf("%lf%lf", &hours, &minutes)

Resources