-
Loop will not complete
ok all I'm a new c student. So I need eveeryone who post not to use advanced functions I haven't been instructed in.
here is the homework exercise:
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, the porgram should calculate and print the combined miles per gallon from all tankfuls.
I've got the program to work on the first inputs and it diplays my mpg and then asks for next gallon input. if I do -1 it keeps on asking for next input and never ends giving me a grand total mpg for one or more tankfuls.
here is my code:
Code:
/* miles per gallon on each tank full */
#include <stdio.h>
int main()
{
int miles; /* miles driven */
float mpg, gallons, total; /* miles per gallon, gallons used, total of all mpg's */
int counter; /* the counter */
counter = 0;
mpg = 0;
total = 0; /* intitilizing all non user input veriables */
printf ("Enter the gallons used (-1 to end):");
scanf ("%f", &gallons); /* user input of the gallons used */
printf ("Enter the miles driven:");
scanf ("%d", &miles ); /* user input of the miles driven */
mpg = miles / (float) gallons; /* formula to obtian miles per gallon based on inputs */
printf ("the miles/gallon for this tank was: %.2f\n" , mpg); /* result on first avrage before moving to next prompt*/
while ( gallons != -1) { /* loop until the input of -1 @ gallons prompt */
total = total + mpg;
counter++;
printf ("Enter the gallons used (-1 to end):");
scanf ("%f", &gallons);
printf ("Enter the miles driven:");
scanf ("%d", &miles );
printf ("the miles/gallon for this tank was: %.2f\n" , mpg);
} /* end while */
if ( counter != 0 ) {
mpg = ( float ) total / counter; /* grand total */
printf( "The average miles per gallon is %.2f",mpg );
}/* end if */
else { /* if no input but -1 was entered */
printf ("No information entered");
} /* end else */
return (0);
}
/* end main */
Remember I'm new and very limited in shortcuts so please use the most basic forms of programming in helping me find the code's problems.
Thanks all,
Joseph
-
Don't post two identical threads: http://cboard.cprogramming.com/showthread.php?t=84790
Your code would ask for the miles driven even if -1 gallons were entered. You should put a test for -1 gallons right after you read the gallons from the user. The break statement might be useful in this case.
You also need to calculate mpg in the loop.
-
i did the mpg in the loop fixed some of it and i've exited the program with a total but its way off
any ideas here is the new code
Code:
/* miles per gallon on each tank full */
#include <stdio.h>
int main()
{
int miles; /* miles driven */
float mpg, gallons, total; /* miles per gallon, gallons used, total of all mpg's */
int counter; /* the counter */
counter = 0;
mpg = 0;
total = 0; /* intitilizing all non user input veriables */
printf ("Enter the gallons used (-1 to end):");
scanf ("%f", &gallons); /* user input of the gallons used */
printf ("Enter the miles driven:");
scanf ("%d", &miles ); /* user input of the miles driven */
mpg = miles / (float) gallons; /* formula to obtian miles per gallon based on inputs */
printf ("the miles/gallon for this tank was: %.2f\n" , mpg); /* result on first avrage before moving to next prompt*/
while ( gallons != -1) { /* loop until the input of -1 @ gallons prompt */
total = (float)total + (float)mpg;
counter = counter + 1;
printf ("Enter the gallons used (-1 to end):");
scanf ("%f", &gallons);
printf ("Enter the miles driven:");
scanf ("%d", &miles );
mpg = miles / (float) gallons;
printf ("the miles/gallon for this tank was: %.2f\n" , mpg);
} /* end while */
if ( counter != 0 ) {
mpg = ( float ) total / counter; /* grand total */
printf( "The average miles per gallon is %.2f", total );
}/* end if */
else { /* if no input but -1 was entered */
printf ("No information entered\n");
} /* end else */
return (0);
}
/* end main */
thanks,
joe
-
You still need to do something immediately after the scanf() if the user enters -1 for the gallons. Perhaps something like this:
Code:
for(;;) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons == -1) break;
/* ... */
}
You also have repeat code before and inside your while loop which could be reduced to one instance of the code.
All of your float casts that I can see are superfluous.
-
thank you DWK found the problem it was in the last if function on the fomula it should be
Code:
if ( counter != 0 ) {
total = ( float ) total / counter; /* grand total */
printf( "The average miles per gallon is %.2f", total );
}/* end if */
not like before
Code:
if ( counter != 0 ) {
mpg = ( float ) total / counter; /* grand total */
printf( "The average miles per gallon is %.2f", total );
}/* end if */
changed that and the program functions perfect.
thanks again,
joe
-
I see two problems, Joseph. First is getting out of the while loop, and the second is your arithmetic on total mpg.
Your code needs this added right after the user input of the gallons used:
Code:
if ( gallons < 0 ) break;
Then your arithmetic. You have total being a sum of total + mpg. Which is no good. You need to have a variable totalmiles, to keep a total of all the miles travelled, AND you need a totalgallons to keep a sum of the gallons that were used.
Intermediate mpg's, should be ignored after they've been figured, and shown to the user. You just want the totalmiles traveled, and totalgallons of gas used.
THEN the figuring for this is no sweat.
Pretty simple, eh? :)
And welcome to the forum, Joseph.
Adak
-
-
Thanks Adak :-)
that extra break code makes for a smooth program.
Joseph