Hi, I have a problem that wants to be calculated only by while loop. (no other loop, no goto, break)

Develop a program that will input the miles driven and gallons used for each tankful.
The program should calculate and display the miles per gallon obtained for each tankful. After processing
all input information, the program should calculate and print the combined miles per gallon
obtained for all tankfuls. Here is a sample input/output dialog:

Enter the gallons used (0 to end): 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875

Enter the gallons used (0 to end): 10.3
Enter the miles driven: 200
The miles / gallon for this tank was 19.417475

Enter the gallons used (0 to end): 5
Enter the miles driven: 120
The miles / gallon for this tank was 24.000000

Enter the gallons used (0 to end): 0
The overall average miles/gallon was 21.601423

Here is my attempt:

#include <stdio.h>

int main(void) {
    int miles;
    float gallons, mg, overall = 0, avg = 0;

    while(gallons != 0) {
      printf("Enter the gallons used (0 to end): ");
      scanf("%f", &gallons);
      printf("Enter miles driven: ");
      scanf("%d", &miles);

      mg = miles/gallons;

      printf("The miles/gallon for this tank was : %f\n", mg);
      overall += mg;
      avg++;

    }

    printf("The overall miles/gallon was: %f\n", overall/avg);

  return 0;
}

Although the output is correct, but when i enter 0 to exit the loop, then overall miles shows something like this: -1.#IND00
And when I tried with by -1, then it calculates this with other inputs.

Something like that:

Enter the gallons used (-1 to end): 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875
Enter the gallons used (-1 to end): -1
Enter the miles driven: -1
The overall average miles/gallon was 11.710938

I use gcc compiler(4.7.2)

Recommended Answers

All 6 Replies

I think the problem constraint is to use only while loop for iteration not any othyer loop.
In that case you can modify the logic as below

#include <stdio.h>
int main(void) {
    int miles;
    float gallons = -1, mg, overall = 0, avg = 0;
    while(gallons != 0) {
      printf("Enter the gallons used (0 to end): ");
      scanf("%f", &gallons);
      if (gallons == 0) {
        printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
        exit(0);
        }

      printf("Enter miles driven: ");
      scanf("%d", &miles);
      mg = miles/gallons;
      printf("The miles/gallon for this tank was : %f\n", mg);
      overall += mg;
      avg++;
    }
      return 0;
}

The solution you given wrong at some point.
If i enter 1st input like
gallon = 12.8
miles 287
output = 22.421875
then 2nd input
gallon = 0
then it doesn't stop immediately, it asks for miles driven also and at the end,
it print 11.210938

instead of 22.42875

Also replace the last two lines in the while loop by below

      overall += miles;
      avg += gallons;

Final code looks something like below

#include <stdio.h>
int main(void) {
    int miles;
    float gallons = -1, mg, overall = 0, avg = 0;
    while(gallons != 0) {
      printf("Enter the gallons used (0 to end): ");
      scanf("%f", &gallons);
      if (gallons == 0) {
        printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
        exit(0);
        }

      printf("Enter miles driven: ");
      scanf("%d", &miles);
      mg = miles/gallons;
      printf("The miles/gallon for this tank was : %f\n", mg);
      overall += miles;
      avg += gallons;
    }
      return 0;
}

The outout is correct
Please check for typo.

Here is the output

Enter the gallons used (0 to end): 12.8
Enter miles driven: 287
The miles/gallon for this tank was : 22.421875
Enter the gallons used (0 to end): 0

The overall miles/gallon was: 22.421875

Thanks for your solution. It works

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.