Hi,
I'm working on a program that should do the following:
Prompt a user to enter the amount of gallons used
Prompt a user to enter the amount of miles driven
Give the average miles / gallon
This program should repeat this process until the user enters -1 as the sentinal value, after which the program gives the overall miles / gallon.
I've messed the code up quite a bit, from trying to get it to work. What happens is the first prompt prints okay, but after I enter a number (12.8 in this case), the program does nothing. So if I hit enter, it prints the same line again but with the next prompt on the same line. If I hit enter again, I go into a loop. Any help is appreciated! Here's my code:
Code:
#include <stdio.h>
int main()
{
float average1, average2, gallons;
int counter, miles;
counter = 0;
printf( "Enter the gallons used, -1 to end: " );
scanf( "%f\n", &gallons );
printf( "Enter the miles driven, -1 to end: ");
scanf( "%d", &miles );
while ( gallons != -1 ){
printf( "Enter the gallons used, -1 to end: " );
scanf( "%f", &gallons );
printf( "Enter the miles driven, -1 to end: ");
scanf( "%d", &miles );
while ( gallons != -1 ){
printf( "Enter the gallons used, -1 to end: " );
scanf( "%f", &gallons );
printf( "Enter the miles driven, -1 to end: ");
scanf( "%f", &miles );
}
if ( counter != 0 ) {
average1 = ( float ) miles / gallons;
printf( "The average miles / gallon is %.6f", average1 );
}
else
printf( "No information was entered\n" );
return 0;
}