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!

#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:

scanf  ("%.2f", &number_gallons);

Just use %f.

Don't use this:

void main(void )

The function main should return an int.

This is undefined:

fflush (stdin);

Try never to use it.

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?

Nevermind about my follow up question regarding decimal places. I have figured it out! Thanks so much for the help.....:-)

>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.

>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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.