I need guidence to solve a problem i'm having with a program (Exercise), When I enter -1 why does the "Enter miles used" show up. It should just print " The overall average miles / gallon was" and if no data is entered print "No Data entered"
PLEASE I dont want the answer just direction so I can learn for meself.
Code:
#include <stdio.h>
/* fuction main begins program execution*/
int main(void)
{
/* intialization phase */
int counter = 0; /* number of tanful entered/ intialize loop counter */
int miles = 0; /* miles value */
float total = 0; /* sum of tankful/ intialize total */
float gallons; /* gallons value */
float tankful; /* number with decimal point for average */
float average;
/* process phase */
/* get gallons used from user */
printf("Enter gallons used, -1 to Exit: "); /* prompt for input */
scanf("%f", &gallons); /* read gallons from user */
printf(" Enter miles used: "); /* prompt for input */
scanf("%d", &miles); /* read miles from user*/
tankful = miles / gallons; /* calculate average */
printf("The miles / gallons for this tank was %f", tankful);
/* loop while sentinel value not yet read from user */
while(gallons != -1) {
total = total + tankful; /* add average to total */
counter = counter + 1; /* increment counter */
/* get next gallonful from user */
printf("\nEnter gallons used, -1 to Exit: "); /* prompt for input */
scanf("%f", &gallons); /* read gallons from user */
printf(" Enter miles used: "); /* prompt for input */
scanf("%d", &miles); /* read miles from user */
tankful = miles / gallons;
printf("The mile / gallons for this tank was %f", tankful);
} /* end while */
/* termination phase */
/* if user entered at least one tankful*/
if(counter != 0) {
/* calculate average of all the tankfuls entered*/
average = (float) total / counter; /* avoid truncation*/
/* display average with six digits of precision*/
printf("The overall average miles / gallon was %f\n", average);
} /* end if */
else { /* if no tankfuls were entered, output message*/
printf("\nNo data entered\n");
} /* end else */
return (0); /* indicate program ended ok */
} /* end fuction main */
Thank for your time
Nurofen