# Eof

• 11-23-2001
Dangerous Dave
Eof
The problem is the WHILE loop and the condition which is needed to end the loop. it is currently set to EOF but this does not seen to be giveing me any response.

THanks.

#include<stdio.h>

/* Exercise 3.17, Deitel & Deitel

Because of the high price of gasoline, drivers are concerned with
the mileage obtained by their automobiles. One driver has kept track
of several tankfuls of gasoline by recording miles driven and gallons
used for each tankful. Develop a C 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 information, the program should calculate and print
the combined miles per gallon obtained for all tankfuls. */

void main()
{
float gallons, miles;
float total_gallons = 0.0, total_miles = 0.0;
float average;

/* Input the gallons used for the first tank */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
fflush(stdin);

while (gallons != EOF)
{
/* Add gallons to running total of gallons used*/
total_gallons = total_gallons + gallons;

/* Input the miles driven for the current tank */
printf("Enter the miles driven: ");
scanf("%f", &miles);
fflush(stdin);

/* Add miles to the running total of miles driven */
total_miles = total_miles + miles;

/* Calculate and print the miles/gallon */
printf("The miles/gallon for this tank was %.2f\n\n", miles/gallons);

/* Input the gallons used for the next tank */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
fflush(stdin);
}

/* Calculate and display the average miles/gallon */
average = total_miles / total_gallons;
printf("The overall average miles/gallon was %.2f\n", average);
}
• 11-23-2001
Fordy
Code:

```#include<stdio.h> void main() { float gallons, miles; float total_gallons = 0.0, total_miles = 0.0; float average; while (1) { printf("Enter the gallons used (-1 to end): "); scanf("%f", &gallons); fflush(stdin); if(gallons == -1) break; total_gallons = total_gallons + gallons; printf("Enter the miles driven: "); scanf("%f", &miles); fflush(stdin); if(miles == -1) break; total_miles = total_miles + miles; printf("The miles/gallon for this tank was %.2f\n\n", miles/gallons); } average = total_miles / total_gallons; printf("The overall average miles/gallon was %.2f\n", average); }```
EOF is usually used in file IO........It does equate to -1, but there is no need to use it here..... keep it simple....
• 11-23-2001
bigtamscot
just to correct the fordy's routine, this is more approriate.
Code:

```#include<stdio.h> void main() { float gallons, miles; float total_gallons = 0.0, total_miles = 0.0; float average; printf("Enter the gallons used (-1 to end): "); scanf("%f", &gallons); fflush(stdin); while (gallons != -1) { total_gallons += gallons; printf("Enter the miles driven: "); scanf("%f", &miles); fflush(stdin); total_miles += miles; printf("The miles/gallon for this tank was %.2f\n\n", miles/gallons); printf("Enter the gallons used (-1 to end): "); scanf("%f", &gallons); fflush(stdin); } average = total_miles / total_gallons; printf("The overall average miles/gallon was %.2f\n", average); }```