Help please with incorrect gas mileage calculation

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2005
Posts: 5
Reputation: tippytoe is an unknown quantity at this point 
Solved Threads: 0
tippytoe tippytoe is offline Offline
Newbie Poster

Help please with incorrect gas mileage calculation

 
0
  #1
Oct 16th, 2005
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!


  1.  
  2. #include <stdio.h>
  3.  
  4.  
  5. void main(void )
  6. {
  7.  
  8. /* Variable Declarations */
  9. /* ---------------------- */
  10.  
  11. float number_gallons;
  12. float number_miles;
  13. float overall;
  14. float total_gallons = 0; //accumulators
  15. float total_miles = 0;
  16. float result;
  17. int i; //counter
  18.  
  19. printf ("This program will calculate the miles per gallon for 3 tanks of gas.\n \n");
  20.  
  21. for (i = 1; i <=3; i=i + 1)
  22. {
  23. printf ("Enter the number of gallons used for tank #%i: ", i);
  24. scanf ("%.2f", &number_gallons);
  25. fflush (stdin);
  26.  
  27. printf ("Enter the number of miles driven: ");
  28. scanf ("%.2f", &number_miles);
  29. fflush (stdin);
  30.  
  31. result = number_miles/number_gallons;
  32.  
  33. printf ("***The miles per gallon for this tank is %.2f \n \n", result);
  34.  
  35. total_gallons = total_gallons + number_gallons;
  36. total_miles = total_miles + number_miles;
  37. } // end for loop
  38.  
  39. /* Calculate overall average miles per gallon */
  40.  
  41. overall = total_miles / total_gallons;
  42.  
  43. printf ("Your overall average miles per gallon for three tanks is %.2f \n \n", overall);
  44.  
  45.  
  46. } // end main
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help please with incorrect gas mileage calculation

 
0
  #2
Oct 16th, 2005
This is not a valid format specifier:
scanf  ("%.2f", &number_gallons);
Just use %f.

Don't use this:
  1. void main(void )
The function main should return an int.

This is undefined:
  1. fflush (stdin);
Try never to use it.
"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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 5
Reputation: tippytoe is an unknown quantity at this point 
Solved Threads: 0
tippytoe tippytoe is offline Offline
Newbie Poster

Re: Help please with incorrect gas mileage calculation

 
0
  #3
Oct 17th, 2005
[QUOTE=Dave Sinkula]This is not a valid format specifier:
scanf  ("%.2f", &number_gallons);
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 5
Reputation: tippytoe is an unknown quantity at this point 
Solved Threads: 0
tippytoe tippytoe is offline Offline
Newbie Poster

Re: Help please with incorrect gas mileage calculation

 
0
  #4
Oct 17th, 2005
Nevermind about my follow up question regarding decimal places. I have figured it out! Thanks so much for the help.....:-)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help please with incorrect gas mileage calculation

 
0
  #5
Oct 17th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 5
Reputation: tippytoe is an unknown quantity at this point 
Solved Threads: 0
tippytoe tippytoe is offline Offline
Newbie Poster

Re: Help please with incorrect gas mileage calculation

 
0
  #6
Oct 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help please with incorrect gas mileage calculation

 
0
  #7
Oct 17th, 2005
Please direct your instructor here so that I can inform him that he's a flaming idiot and needs to learn the topic before trying to teach it. The last thing we need is more new programmers who have no idea how to program because they were taught by moron teachers.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 173
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Help please with incorrect gas mileage calculation

 
0
  #8
Oct 17th, 2005
When I learned C in school our instructor insisted on fflushing after every scanf(), sort of a bathroom thing (student humor). He was a typical puplic school teacher, not very bright. Maybe this comes out of some Herbie Schield book in the library.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC