This a program i write for practice. The problem I'm facing is inside the while statement. Is it okay to divide, in this case, the miles(which is inside the the while) with gall(outside the while). I tried to compile it and the answer given was 0.00000
P/S. This program is not complete.

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
    double miles,
           gall,
           x;
           
    printf("Enter the gallons used (-1 to end)> ");
    scanf("%lf", &gall);
    
    while (gall!=-1){
          printf("Enter the miles driven> ");
          scanf("%d", &miles);
          x = miles/gall;
          printf("The miles/gallon for this tank was %f.\n", x);
          }
          
    if (gall== -1){
               printf("Invalid\n");
    } else { 
           printf("The overall average miles/gallon was ");
           }
          
    system("PAUSE");
    return 0;
    
}

Recommended Answers

All 2 Replies

scanf("%d", &miles);

You need to read double here. But you are reading integer. Or at least change the type of miles here to integer. You might actually expect some meaningful result.

And you have an infinite loop in your code. Your program will keep requesting for miles every time. It donst even come of loop. Since you read gall ,just once and miles every time. Perhaps you should change the while condition to check miles != -1 .

ssharish

Thanks for tips! will work out on it now

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.