I have this homework program to calculate mpg. The -1 is the sentinel value which is supposed to skip the questions asking the user for gas and miles when inputted. At the moment, the program continues to ask them, and then does leave the loop moving onto the total statement.
What am I doing wrong? How can I get it to skip the miles question and move straight onto stating the totals?
Thanks
double TotalGallons;
double TotalMiles;
double mpg;
double tmpg;
int m;
int g;
g = 0;
TotalGallons = 0;
TotalMiles = 0;
if (g != -1)
{
while (g != -1) {
printf("Enter the gallons used (-1 to end):\n");
scanf("%d", &g);
TotalGallons = (TotalGallons + g);
printf("Enter the miles driven:\n");
scanf("%d", &m);
TotalMiles = (TotalMiles + m);
mpg = (m / g);
printf("The miles / gallon for this tank was %lf\n", mpg);
}
}
else;
{
TotalMiles = (TotalMiles + 0);
tmpg = (TotalMiles / TotalGallons);
printf("The overall average miles/gallon was %lf\n", tmpg);
}
return 0;
else;
line. The;
is an empty statement, terminating theif
statement. The block that follows theelse;
is unconditionally executed (it's not part of theif
statement).