0

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;
1
  • 4
    Note the else; line. The ; is an empty statement, terminating the if statement. The block that follows the else; is unconditionally executed (it's not part of the if statement). Commented Jul 14 at 5:02

2 Answers 2

4

It didn't skip the second half of the loop because you didn't include any code that would do that.

break can be used to exit a loop.

If we add the necessary check and remove the unnecessary ones, we get the following:

while ( 1 ) {
    printf( "Enter the gallons used (-1 to end):\n" );

    // Exit the loop if we reached EOF.
    int g;
    if ( scanf( "%d", &g ) != 1 )
        break;

    // Exit the loop if the user provided `-1`.
    if ( g == -1 )
        break;

    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

The while loop doesn't end until the end of the block is reached, so you should add a break if (g == -1), so when g == -1, it'll immediately print the output, and not ask you for the miles driven.

Here's my fixed copy of the code:

#include <stdio.h>
int main() {
    double TotalGallons;
    double TotalMiles;
    double mpg;
    double tmpg;
    int m;
    int g;

    g = 0;
    TotalGallons = 0;
    TotalMiles = 0;


    while (1) {
        printf("Enter the gallons used (-1 to end):\n");
        if (scanf("%d", &g) != 1) {
            break;
        }
        if (g == -1) {
            break;
        }
        TotalGallons = ( TotalGallons + g );
        printf("Enter the miles driven:\n");
        if (scanf("%d", &m) != 1) {
            break;
        }
        TotalMiles = ( TotalMiles + m);

        mpg = ( m / g );
        printf("The miles / gallon for this tank was %lf\n", mpg);

    }
    TotalMiles = ( TotalMiles + 0);
    tmpg = ( TotalMiles / TotalGallons );
    printf("The overall average miles/gallon was %lf\n", tmpg);
}

4 Comments

You should check the return value from scanf().
Updated that, thanks!
1 of 2 instances updated :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.