By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

Beginner Problems with EOF I think!

P: 2
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(ifstream &pfin);
float findAverageTime(ifstream &pfin, char fileName[]);
float findMinimumTime(ifstream &pfin, char fileName[]);
float findMaximumTime(ifstream &pfin, char fileName[]);


bool openInputFile(ifstream &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(ifstream & 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
Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


sivadhas2006
100+
P: 142
Hi,

I changed your code to make it work.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. #define  MAX_PATH 260
  6.  
  7. using namespace std;
  8.  
  9. bool OpenInputFile(ifstream &pfin, char szFileName[])//parameters passed by reference
  10. {
  11.    cout << "Enter input File Name : ";
  12.    cin >> szFileName;   
  13.  
  14.    pfin.open (szFileName);
  15.  
  16.    // check whether the file is opened successfully.
  17.    if (pfin.is_open()) 
  18.    {
  19.       return true; 
  20.    }
  21.    else 
  22.    {
  23.       return false;
  24.    }
  25.  
  26. }// end of OpenInputFile(ifstream & pfin)
  27.  
  28. float FindAvgTime(ifstream &pfin)
  29. {
  30.    float 
  31.       fAvgTime = 0,
  32.       fTotalTime = 0,
  33.       fFetchedTime = 0;
  34.    int 
  35.       nCount = 0;    
  36.  
  37.    // To reset the file pointer to the first position.
  38.    pfin.clear();
  39.    pfin.seekg(0);   
  40.    while (!pfin.eof())
  41.    {
  42.       pfin >> fFetchedTime;
  43.       fTotalTime = fTotalTime + fFetchedTime;
  44.       nCount++;      
  45.    }// end of while   
  46.  
  47.    fAvgTime = fTotalTime / nCount;
  48.    return fAvgTime;
  49.  
  50. }// end of FindAvgTime(ifstream &pfin)
  51.  
  52.  
  53. float FindMinTime(ifstream &pfin, float a_fMaxTime)
  54. {
  55.    float 
  56.       fMinTime = 0,
  57.       fFetchedTime = 0;
  58.  
  59.    // Set the min time as max time.
  60.    fMinTime = a_fMaxTime;
  61.  
  62.    // To reset the file pointer to the first position.   
  63.    pfin.clear();
  64.    pfin.seekg(0);
  65.    while (!pfin.eof())
  66.    {
  67.       pfin >> fFetchedTime;
  68.       if (fFetchedTime < fMinTime)
  69.       {
  70.         fMinTime = fFetchedTime;
  71.       }// end of if
  72.  
  73.    }// end of while
  74.  
  75.    return fMinTime;
  76. }
  77.  
  78. float FindMaxTime(ifstream &pfin)
  79. {
  80.    float 
  81.       fMaxTime = 0,
  82.       fFetchedTime = 0;
  83.  
  84.    // To reset the file pointer to the first position.   
  85.    pfin.clear();
  86.    pfin.seekg(0);
  87.    while (!pfin.eof())
  88.    {
  89.       pfin >> fFetchedTime;
  90.       if (fFetchedTime > fMaxTime)
  91.       {
  92.          fMaxTime = fFetchedTime;
  93.       }// end of if      
  94.    }// end of while
  95.  
  96.    return fMaxTime;
  97. }
  98.  
  99. void main()
  100. {
  101.    char 
  102.       szFileName[MAX_PATH];
  103.    float 
  104.       fAvgTime = 0,
  105.       fMinTime = 0,
  106.       fMaxTime = 0;   
  107.    ifstream 
  108.       fin;
  109.  
  110.    if (OpenInputFile(fin, szFileName) == false)
  111.    {
  112.       cout << "UNABLE TO OPEN THE INPUT FILE " << szFileName << " : " << endl;
  113.       cout << "PROGRAM TERMINATED" << endl;
  114.       system ("pause");
  115.       return;// ends function
  116.    }// end of if
  117.  
  118.  
  119.    fAvgTime = FindAvgTime(fin);   
  120.    fMaxTime = FindMaxTime(fin);
  121.    fMinTime = FindMinTime(fin, fMaxTime);
  122.  
  123.    cout << "File Name: " << szFileName << endl;
  124.    cout << "Average Time = " << fAvgTime << endl;
  125.    cout << "Minimum Time = " << fMinTime << endl;
  126.    cout << "Maximum Time = " << fMaxTime << endl;
  127.  
  128.    // Close the input file.
  129.    fin.close();
  130.  
  131.    system("pause");
  132.  
  133. }// end of main
  134.  
  135.  
  136.  
Regards,
M.Sivadhas.
Nov 17 '06 #2

P: 2
Thanks very much that's really great of you. Thanks for commenting in what I needed to change.
Thanks Again
Nov 17 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.