Strange that it changes anything - what you should do to fix it would be to chage "int code" to "ERRORS code" in the catch statement.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well, I copied it over incorrectly from my example to his code.
What I should have added was
And the enum does not need the "myerrors" instantiation as well. Good catch.Code:if(infile.fail()) { int myerror = NoSuchFile ; throw myerror ; }
Todd
Sure, you can also cast an enum to integer:
But what's the point of that, when the "correct" thing to do is to actually use the enum that is defined in the first place.Code:throw static_cast<int>(NoSuchFile);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Actually, enum's and int's are not the same - they are very similar and can easily be converted back and forth, but enum's are distinctly different types from int's - even if the processor doesn't make much of a difference, the compiler thinks of them differently.
But the point of declaring an enum is to differentiate it from an int, so that's why the compiler sees it differently.
In plain C, the difference is even smaller.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
OK, thanks for the in-depth!! (must get back to work...)
Are you saying that the only changes you made to your original code was to rename the numberOfRows enum and changethe catch to "catch (ERRORS code)"? What is the difference in behavior when you run the two versions?
I can't believe it. Damn. A few moments ago everything worked just fine. I made some changes and now when it is supposed to throw an exeption I get some memory leak and dbgdel.cpp is showing up!!! :-((
I think I'll just add the whole code...I really appreciate your help guys.
Header
class CPPCode:#ifndef FILE_HANDLING__ #define FILE_HANDLING__ #include <vector> #include <iostream> using namespace std; enum ERRORS { NoSuchFile, BadNumberOfRows, WrongNumberOfLinesInHeader, ErrGetLine }; class FileHandling { private: int numRecords; double sum, average, minimum, maximum; double* vec; public: FileHandling(); ~FileHandling(); void ReadFile(const string& filename) throw(int); void CalculateValues(); void printdata() const; //static const int NoSuchFile=1; //static const int BadNumberOfRows=2; //static const int WrongNumberOfLinesInHeader=3; //static const int ErrGetLine=4; }; #endif
main CPPCode:#include "FileHandling.h" #include <fstream> using namespace std; FileHandling::FileHandling() { numRecords=0; sum=0; average=0; minimum=0; maximum=0; //zero all class variables } FileHandling::~FileHandling() { delete vec; } void FileHandling::ReadFile(const string& filename) throw(int) { int numberOfRows=0; ifstream infile(filename.c_str()); if(infile.fail()) throw NoSuchFile; //The problem infile>>numberOfRows;//get the number of records in the file infile.ignore(); //have to ignore \n before getline if(!numberOfRows) throw BadNumberOfRows; vec = new double[numberOfRows]; //assign maximal needed space for array. The loop won't acceed this value anyway for (int i=0;i<numberOfRows;i++) //cycle through whole file { if(!infile.eof()) //check not eof() { const int MaxLine = 80; char str[MaxLine]; infile.getline(str, MaxLine); if(infile.fail()) throw ErrGetLine; if (str[0]!='#') //if line is not comment { double number=atof(str); vec[numRecords]=number; numRecords++; }//end of if } else throw WrongNumberOfLinesInHeader; }//end of for }//getdata void FileHandling::CalculateValues() { minimum=vec[0]; maximum=vec[0]; for(int counter=0; counter<numRecords; counter++) //sum up whole vector { sum+=vec[counter]; if (vec[counter]<minimum) minimum=vec[counter]; if (vec[counter]>maximum) maximum=vec[counter]; } average=sum/numRecords; }//CalculateValues void FileHandling::printdata() const { cout<<"**** FILE AUDIT RESULTS ***"<<endl <<"Total records:"<< numRecords <<endl <<"Sum: "<<sum<<endl<<"Average of the records: "<< average<<endl <<"Minimum value found: "<<minimum<<endl<<"Maximum value found: "<<maximum<<endl; }//printdata
An attached working file should of course be called data.txtCode:#include "FileHandling.h" void displayinfo(){ cout << "----------------------------------------" << endl; cout << endl<<endl; } int main() { displayinfo(); try { FileHandling operations; operations.ReadFile("data.txt"); operations.CalculateValues(); operations.printdata(); } //catch (int code) catch (ERRORS code) { cerr<<"error code number: "<<code<<endl; exit(1); } return 0; }
otherwise one of the errors occurs.
Perhaps this:
Code:public: FileHandling(); ~FileHandling(); void ReadFile(const string& filename) throw(int); void CalculateValues(); void printdata() const; //static const int NoSuchFile=1; //static const int BadNumberOfRows=2; //static const int WrongNumberOfLinesInHeader=3; //static const int ErrGetLine=4; };
Again you are saying you would throw int's. Either don't use a throw specification at all or be honest about it. The compiler won't forgive.Code:void FileHandling::ReadFile(const string& filename) throw(int)
Concerning error specifications, you are using new which might throw std::bad_alloc, so the specs are wrong.
Instead of allocating memory with new you should use a std::vector, or otherwise you'll leak the allocated memory when the function throws (or handle that clean-up manually - your choice).
Last edited by anon; 01-21-2008 at 05:01 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Do you still get the same issue if you remove the throw specifications?
A couple notes on your code. First, you should still change the if (!numberOfRows) part. It will work better now that you have initialized the variable, but it isn't as correct as it could be.
Second, you should probably be using the string class instead of C style strings unless you have a specific reason for limiting lines to 80 characters or less. The string class is generally safer and easier to use than C style strings, and in this case you wouldn't have to limit line length to 80 characters.
Third, you should use the standard vector container rather than a dynamic array of doubles for your vec array, unless your class/instructor requires you not to. The reason is that the vector container is safer and easier to use than a plain dynamic array. For example, your code will crash if you create a FileHandling object but don't call ReadFile, or if you try to copy a FileHandling object. If you used a vector, then you probably wouldn't have either of those bugs.
The problem is caused while performing the throws...I can't check otherwise because I have no other way to control errors rather than try-catch...
About the other issues, thank you, and yes these are the requirements of this stupid exarcise. I'm not familiar with catch=throw becuase I usually work with win32 and now I'm taking c++ course and should learn all the console abilities.
The throw specification is the part at the end of the function declaration:I just meant to remove those, since those aren't mandatory and you can still use try/catch/throw without them (and many would suggest that you not use them).Code:void ReadFile(const string& filename) throw(int);