944,144 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2211
  • C RSS
Oct 16th, 2005
0

Help please with incorrect gas mileage calculation

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tippytoe is offline Offline
5 posts
since Oct 2005
Oct 16th, 2005
0

Re: Help please with incorrect gas mileage calculation

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

[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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tippytoe is offline Offline
5 posts
since Oct 2005
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

Nevermind about my follow up question regarding decimal places. I have figured it out! Thanks so much for the help.....:-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tippytoe is offline Offline
5 posts
since Oct 2005
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tippytoe is offline Offline
5 posts
since Oct 2005
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 17th, 2005
0

Re: Help please with incorrect gas mileage calculation

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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: AIM ShellExecute Help!!
Next Thread in C Forum Timeline: call a function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC