| | |
Help please with incorrect gas mileage calculation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2005
Posts: 5
Reputation:
Solved Threads: 0
I only get a result of "1.00" for each mileage calc and also for the overall average, regardless of numbers input. Can anyone see where the problem(s) is in my program below? Thanks in advance!
C Syntax (Toggle Plain Text)
#include <stdio.h> void main(void ) { /* Variable Declarations */ /* ---------------------- */ float number_gallons; float number_miles; float overall; float total_gallons = 0; //accumulators float total_miles = 0; float result; int i; //counter printf ("This program will calculate the miles per gallon for 3 tanks of gas.\n \n"); for (i = 1; i <=3; i=i + 1) { printf ("Enter the number of gallons used for tank #%i: ", i); scanf ("%.2f", &number_gallons); fflush (stdin); printf ("Enter the number of miles driven: "); scanf ("%.2f", &number_miles); fflush (stdin); result = number_miles/number_gallons; printf ("***The miles per gallon for this tank is %.2f \n \n", result); total_gallons = total_gallons + number_gallons; total_miles = total_miles + number_miles; } // end for loop /* Calculate overall average miles per gallon */ overall = total_miles / total_gallons; printf ("Your overall average miles per gallon for three tanks is %.2f \n \n", overall); } // end main
This is not a valid format specifier: Just use %f.
Don't use this: The function main should return an int.
This is undefined: Try never to use it.
scanf ("%.2f", &number_gallons);Don't use this:
C Syntax (Toggle Plain Text)
void main(void )
This is undefined:
C Syntax (Toggle Plain Text)
fflush (stdin);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2005
Posts: 5
Reputation:
Solved Threads: 0
[QUOTE=Dave Sinkula]This is not a valid format specifier: Just use %f.
Thanks so much for the help! I set it to be just %f and it worked. EXCEPT, my instructors' notes want the results to be formatted with two decimal places, not the usual 6. His notes said to format it like I had %.2f, and the book I have offers no assistance on this topic. How would you format the floating point result to output with 2 decimal places?
scanf ("%.2f", &number_gallons);Thanks so much for the help! I set it to be just %f and it worked. EXCEPT, my instructors' notes want the results to be formatted with two decimal places, not the usual 6. His notes said to format it like I had %.2f, and the book I have offers no assistance on this topic. How would you format the floating point result to output with 2 decimal places?
>His notes said to format it like I had %.2f
Just for other people's reference, your instructor was talking about output with printf, not input with scanf. scanf doesn't use the precision modifier because there's really no point.
>I have figured it out!
Did you figure out the other two complaints about your broken code? They're both somewhat important because they make the entire program unpredictable. By the way:
>fflush (stdin);
Remove these entirely. There's no point in even attempting to "flush" stdin unless you're mixing input schemes that conflict, like scanf and getchar. Since scanf is smart enough to clean up after itself for most common usage, you're adding a broken construct for no reason. Rather than just throw fflush(stdin) after every request for input, why not try to learn why such a thing would be needed? That way you can learn that it's wrong, and ways to avoid the problem without using something that's wrong.
Just for other people's reference, your instructor was talking about output with printf, not input with scanf. scanf doesn't use the precision modifier because there's really no point.
>I have figured it out!
Did you figure out the other two complaints about your broken code? They're both somewhat important because they make the entire program unpredictable. By the way:
>fflush (stdin);
Remove these entirely. There's no point in even attempting to "flush" stdin unless you're mixing input schemes that conflict, like scanf and getchar. Since scanf is smart enough to clean up after itself for most common usage, you're adding a broken construct for no reason. Rather than just throw fflush(stdin) after every request for input, why not try to learn why such a thing would be needed? That way you can learn that it's wrong, and ways to avoid the problem without using something that's wrong.
I'm here to prove you wrong.
•
•
Join Date: Oct 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>His notes said to format it like I had %.2f
Just for other people's reference, your instructor was talking about output with printf, not input with scanf. scanf doesn't use the precision modifier because there's really no point.
Yes, that is what I figured out. I had the format specifier of %.2f in the scanf statement.. When I removed it and kept it only in the printf statement than it ran correctly.
>I have figured it out!
Did you figure out the other two complaints about your broken code? They're both somewhat important because they make the entire program unpredictable. By the way:
I understand what you are saying and appreciate the sound advice, but I have to keep it that way or the instructor will deduct points.
>fflush (stdin);
Remove these entirely. There's no point in even attempting to "flush" stdin unless you're mixing input schemes that conflict, like scanf and getchar. Since scanf is smart enough to clean up after itself for most common usage, you're adding a broken construct for no reason. Rather than just throw fflush(stdin) after every request for input, why not try to learn why such a thing would be needed? That way you can learn that it's wrong, and ways to avoid the problem without using something that's wrong.
!I understand what you are saying however the instructor was clear that he wanted us to use the fflush statement after every scanf statement in this program. If I remove it I will lose points.
Thanks
![]() |
Similar Threads
- Don't pump gas on May 15th (Geeks' Lounge)
- What kind of car do you drive? (Geeks' Lounge)
Other Threads in the C Forum
- Previous Thread: AIM ShellExecute Help!!
- Next Thread: call a function
| Thread Tools | Search this Thread |
#include * adobe ansi array arrays asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework function getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv research reversing scripting segmentationfault sequential single socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






