Thread: Eof

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    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);
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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....

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    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); 
    }
    Last edited by bigtamscot; 11-23-2001 at 06:34 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM