Beginner – Problems with EOF I think!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkeon
    New Member
    • Nov 2006
    • 2

    Beginner – Problems with EOF I think!

    Hi,

    I'm pretty new to C++ I've been doing a course for a few months. I've been trying to design a simple console application that reads in floats from a text file and returns the average, minimum and maximum figures. I can get all the functions to run separately but when I try them together the returned variables are empty. I think it's the EOF marker that is the problem but I haven't got a clue how to sort it out. The books and the Internet aren't very clear. Any help would be greatly appreciated.
    Cheers, Monk

    I'm using Visual C++ 2005 Express Edition.

    ## Source Code ##

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    bool openInputFile(i fstream &pfin);
    float findAverageTime (ifstream &pfin, char fileName[]);
    float findMinimumTime (ifstream &pfin, char fileName[]);
    float findMaximumTime (ifstream &pfin, char fileName[]);


    bool openInputFile(i fstream &pfin, char fileName[])//parameters passed by reference
    {
    cout << "Enter input filename: ";
    cin >> fileName;

    pfin.open (fileName);

    if (pfin.is_open() ) return true;
    else return false;

    }// end of openInputFile(i fstream & pfin)

    float findAverageTime (ifstream &pfin, char fileName[])
    {
    float time;
    int counter = 0;
    float total = 0;
    float averageTime;

    pfin >> time;

    while (!pfin.eof())
    {
    total = total + time;
    counter++;

    pfin >> time;
    }// end of while

    averageTime = total / counter;

    pfin.close();

    return averageTime;

    }// end of findAverageTime (ifstream &pfin, char fileName[])


    float findMinimumTime (ifstream &pfin, char fileName[])
    {
    float minimumTime;
    float time;

    pfin >> time;
    minimumTime = time;

    while (!pfin.eof())
    {
    if (time < minimumTime)
    {
    minimumTime = time;
    }// end of if

    pfin >> time;
    }// end of while

    return minimumTime;
    }

    float findMaximumTime (ifstream &pfin, char fileName[])
    {
    float maximumTime;
    float time;

    pfin >> time;
    maximumTime = time;

    while (!pfin.eof())
    {
    if (time > maximumTime)
    {
    maximumTime = time;
    }// end of if

    pfin >> time;
    }// end of while

    return maximumTime;
    }


    void main()
    {
    char fileName[40];
    float averageTime = 0;
    float minimumTime = 0;
    float maximumTime = 0;

    ifstream fin;

    if (openInputFile( fin, fileName) == false)
    {
    cout << "UNABLE TO OPEN THE INPUT FILE " << fileName << " : " << endl;
    cout << "PROGRAM TERMINATED" << endl;
    system ("pause");
    return;// ends function
    }// end of if


    //averageTime = findAverageTime (fin, fileName);
    minimumTime = findMinimumTime (fin, fileName);
    //maximumTime = findMaximumTime (fin, fileName);

    cout << "File Name: " << fileName << endl;
    cout << "Average Time = " << averageTime << endl;
    cout << "Minimum Time = " << minimumTime << endl;
    cout << "Maximum Time = " << maximumTime << endl;

    system("pause") ;

    }// end of main
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    I changed your code to make it work.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #define  MAX_PATH 260
    
    using namespace std;
    
    bool OpenInputFile(ifstream &pfin, char szFileName[])//parameters passed by reference
    {
       cout << "Enter input File Name : ";
       cin >> szFileName;   
    
       pfin.open (szFileName);
    
       // check whether the file is opened successfully.
       if (pfin.is_open()) 
       {
          return true; 
       }
       else 
       {
          return false;
       }
    
    }// end of OpenInputFile(ifstream & pfin)
    
    float FindAvgTime(ifstream &pfin)
    {
       float 
          fAvgTime = 0,
          fTotalTime = 0,
          fFetchedTime = 0;
       int 
          nCount = 0;    
    
       // To reset the file pointer to the first position.
       pfin.clear();
       pfin.seekg(0);   
       while (!pfin.eof())
       {
          pfin >> fFetchedTime;
          fTotalTime = fTotalTime + fFetchedTime;
          nCount++;      
       }// end of while   
    
       fAvgTime = fTotalTime / nCount;
       return fAvgTime;
    
    }// end of FindAvgTime(ifstream &pfin)
    
    
    float FindMinTime(ifstream &pfin, float a_fMaxTime)
    {
       float 
          fMinTime = 0,
          fFetchedTime = 0;
    
       // Set the min time as max time.
       fMinTime = a_fMaxTime;
    
       // To reset the file pointer to the first position.   
       pfin.clear();
       pfin.seekg(0);
       while (!pfin.eof())
       {
          pfin >> fFetchedTime;
          if (fFetchedTime < fMinTime)
          {
            fMinTime = fFetchedTime;
          }// end of if
          
       }// end of while
    
       return fMinTime;
    }
    
    float FindMaxTime(ifstream &pfin)
    {
       float 
          fMaxTime = 0,
          fFetchedTime = 0;
       
       // To reset the file pointer to the first position.   
       pfin.clear();
       pfin.seekg(0);
       while (!pfin.eof())
       {
          pfin >> fFetchedTime;
          if (fFetchedTime > fMaxTime)
          {
             fMaxTime = fFetchedTime;
          }// end of if      
       }// end of while
    
       return fMaxTime;
    }
    
    void main()
    {
       char 
          szFileName[MAX_PATH];
       float 
          fAvgTime = 0,
          fMinTime = 0,
          fMaxTime = 0;   
       ifstream 
          fin;
    
       if (OpenInputFile(fin, szFileName) == false)
       {
          cout << "UNABLE TO OPEN THE INPUT FILE " << szFileName << " : " << endl;
          cout << "PROGRAM TERMINATED" << endl;
          system ("pause");
          return;// ends function
       }// end of if
    
    
       fAvgTime = FindAvgTime(fin);   
       fMaxTime = FindMaxTime(fin);
       fMinTime = FindMinTime(fin, fMaxTime);
    
       cout << "File Name: " << szFileName << endl;
       cout << "Average Time = " << fAvgTime << endl;
       cout << "Minimum Time = " << fMinTime << endl;
       cout << "Maximum Time = " << fMaxTime << endl;
    
       // Close the input file.
       fin.close();
    
       system("pause");
    
    }// end of main
    Regards,
    M.Sivadhas.

    Comment

    • monkeon
      New Member
      • Nov 2006
      • 2

      #3
      Thanks very much that's really great of you. Thanks for commenting in what I needed to change.
      Thanks Again

      Comment

      Working...