For some reason my throws are not handled. Would appreciate your help.
main()
Code:
int main()
{
displayinfo();
try
{
FileHandling operations;
operations.ReadFile("data.txt"); //here I have the problem
operations.CalculateValues();
operations.printdata();
}
catch (int code)
{
cerr<<"error code number: "<<NoSuchFile<<endl;
exit(1);
}
return 0;
}
The method
Code:
void FileHandling::ReadFile(const string& filename) throw(int)
{
int numberOfRows;
ifstream infile(filename.c_str());
if(infile.fail())
throw NoSuchFile; //this one is not handled
infile>>numberOfRows;//get the number of records in the file
if(!numberOfRows)
throw numberOfRows;
for (int i=0;i<numberOfRows;i++) //cycle through whole file
{
if(!infile.eof()) //check not eof()
{
infile.ignore(); //have to ignore \n before getline
const int MaxLine = 80;
char str[MaxLine];
infile.getline(str, MaxLine);
if(infile.fail())
throw ErrGetLine;
if (str[0]!='#') //if line is not comment
{
numRecords++;
double number=atof(str);
vec.push_back(number);
}//end of if
}
else
throw WrongNumberOfLinesInHeader;
}//end of for
}//getdata
and the header file, just in case
Code:
#ifndef __FILE_HANDLING__
#define __FILE_HANDLING__
#include <vector>
#include <iostream>
using namespace std;
enum ERRORS
{
NoSuchFile,
numberOfRows,
WrongNumberOfLinesInHeader,
ErrGetLine
};
class FileHandling
{
private:
double numRecords, sum, average, minimum, maximum;
vector<double> vec;
public:
FileHandling();
void ReadFile(const string& filename) throw(int);
void CalculateValues();
void printdata() const;
};
#endif