0

I had posted on here before, but I was never able to get the help I needed. I'm working on a school project and I can not get my program to work properly.

The program should prompt the user to enter the number of gallons used and the number of miles driven for each of the 3 tanks of gas. The program should then calculate and display the miles per gallon obtained for each tank. Once processing is complete for the 3 tanks, the program will calculate the overall mileage(total gallons / total miles) and display a friendly "Goodbye" message.

The issue i am having is that I can not get it to display to OVERALL Millage. it ends after looping 3 times. I know different loop statements need conditions to be met, but I cant get the FOR loop to work properly. Im getting really frustrated, cause I know this should not be this hard.

Code

#include <stdio.h>
int main(void) 
{
    int miles,i=3;
    float gallons, mg, overall = 0, avg = 0;
    while(i>0) 
    {
        printf("Enter the gallons used: ");
        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 += miles;
        avg += gallons;i--;
    }

    if(gallons == 0) 
    {
        printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
        exit(0);
    }
    return 0;
}
10
  • 5
    if (gallons == 0) ...emmm.why? Commented Feb 19, 2017 at 15:25
  • 2
    Can you sort out the formatting of the code Commented Feb 19, 2017 at 15:25
  • 2
    The condition if (gallons == 0) is never true, hence that print statement never happens. Commented Feb 19, 2017 at 15:26
  • 6
    there isn't a for loop in your code Commented Feb 19, 2017 at 15:26
  • 1
    Use a debugger. Learning to use a debugger is as valuable as learning how to code. Learning how to code comes from using a debugger. Commented Feb 19, 2017 at 15:30

3 Answers 3

4

If I read your code correctly, then what is preventing the overall mileage from being printed is the following final if statement:

if (gallons == 0)

If you remove it, then the overall mileage should print. Use this pattern:

while (i > 0)
{
    // your while loop here
}

printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
exit(0);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much. I am having a really hard time wrapping brain around loops in this class. That worked. I figured it was something simple. but I have been working on this for a week and was getting irritated.
@MikeDahl For everyone's sake, maybe you could comment why you had if (gallons == 0) in there. Maybe you were checking for the loop completing, or maybe you wanted to check for divide by zero?
I had it in there because I'm a moron, and I'm still trying to understand all of this.
@MikeDahl No issues, your question was way better than what is typical here.
I'm guessing there is a lot of dumb questions here?
|
0

This if (if (gallons == 0) {})block is out of while loop. First, you need to move the if loop inside while loop. and this if condition should be for variable i as follow and not for gallons.

if (i == 0) 
{
    printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
}

In this case, after 3 iteration, value of i will be 0 so it will enter into the if block and calculate and print the overall miles/gallon.

Comments

0

Adding to Tim Biegeleisen's answer:

mg = miles/gallons;  

What if gallons equals to 0? e.g. 0 miles for 0 gallons
This will lead to floating point exception.

A simple if-else can solve this problem!

if(!gallons)
    mg = 0;  
else
    mg = miles/gallons;

2 Comments

Since it will be a floating point calculation, it'll probably give an infinity as the result, not a crash. It's curious but these days, one of the very few ways of generating an FPE (floating point exception) is integer division by zero.
@JonathanLeffler Thank you! On my terminal, I was performing integer division which crashed the program on divisor being 0. So the wrong statement on my part.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.