I'm a complete novice to C programming and also new to this type of online discussion so bear with me, help me and correct me if I'm wrong anywhere. Here is the code:
Code:
//this program calculates the average miles travelled per gallon of fuel.
#include <stdio.h>
//start of main
int
main (void)
{
float gallons, miles, average = 0.0, counter = 0.0; //declaration/initialisation
printf ("\t\t\t%s\n", "CALCULATE MILES PER GALLONS FOR YOUR CAR"); //message
//start of while loop
while (gallons != -1.0)
{ //condition to end program
printf ("%s", "Enter the gallons used(-1 to end): "); //prompt
scanf ("%f\n", &gallons); //input
if (gallons != -1.0)
{ //condition to take further input; start of if
printf ("%s", "Enter the miles driven: "); //prompt
scanf ("%f", &miles); //input
average += miles / gallons; //summation of all averages
counter++; //incrementing counter
printf ("%s%0.2f\n\n", "The miles/gallon for this tank was ", average); //printing current average
} //end of if
} //end of while
if (counter != 0.0)
{ //start of if
printf ("%s%0.2f", "The overall average miles/gallon was ", average / counter); //printing final average
} //end of if
else
puts ("Invalid Input..");
} //end of main
I know this maybe a repeat question but this program compiles without a syntax error and runs upto second printf function and than hangs or something which I'm unable to understand, what kind of error is this? I don't find any logical error in the code. Thank You.