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
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
Comment