简体   繁体   中英

I'm having trouble ending a loop in C

I'm in school learning C. (I am not asking for anyone to write this for me).

Assignment
This program will calculate the miles per gallon MPG for you for three tanks of gas after you have entered the gallons used and miles driven.

I can get my program to start a loop, but I can't figure out how to make it end the loop after 3 runs and give me the Average MPG in 3 tanks. Running the program give me the average, but will keep asking forever.

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

Try this small changes. Use an iterator to get average of 3 tanks.

Modify like

i=0;
while(i < 3) {
i++;

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

Sorry : I did not see how is gallons being assigned/initialized in your code, I saw float galons and while (gallons != 0) and then thought that gallons was at some point the result of a computation.

This answer is still useful in my opinion.

Don't use float values to check conditions, float s are not accurate because their machine representation cannot be, so gallons != 0 will probably hardly ever be true, use int instead and your loop control will work correctly. Only use float for the average value.

But in fact, because your specific problem can be solved with a for loop, you should use

for (int i = 0 ; i < 3 ; ++i)

instead, that way you know that it will only loop 3 times.

SIDE NOTE : learn more about scanf() and why you MUST check the value that it retuns in programs like yours.

Your program, as written, does not stop at 3 tanks. It will continuously ask for tanks until you answer 0 to the number of gallons used.

To make it read at most three tanks, replace while (gallons != 0) with for (int i = 0; i < 3; i++) . That will make the main loop run three times only.

But then it won't print the overall average. It will simply quit after running three times. The code that shows the overall average is inside that if test that checks if you typed 0 gallons. Remove that if test and move the printf statement which shows the overall average near the end of the program, right before the return statement. This way it will run after the for loop runs 3 times.

float gallons = -1;

It doesn't make any sense; And you need to notice one thing that is

while(gallons!=0){
  //code
 }

You are asking the user to enter the value if gallons to input and your not changing it's value so this value will always be true in while and loop will go infinite.

If you need to run the loop three times then you can do it by using variable. `

int i=3;
while(i>0){//code 
i--;
}

Here I have edited your program ;

#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);
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;i--;
    }
 return 0;
 }

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM