#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include"person.h"
using weightGain::person;
using std::getline;
using std::cerr;
using std::string;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::istream;
using std::cout;
string get_string(istream& s){
char a[50];
s.get(a, 49, ',');
if(s.peek()== EOF){
cout << "End of file Reached\n";
exit(1);
}
if(s.peek()!= ','){
cout << "Comma was missing before the end of file was reached\nExiting Program\n";
exit(1);
}
s.ignore();
return a;
};
for(;;){
last = get_string(inputPerson);
first = get_string(inputPerson);
inputPerson >> weight;
person newPerson(last, first, weight);
newPerson.weightGain();
outputPerson << newPerson;
}
return 0;
}
Here is my code I am just wondering is it necessary to error check the ofsream file? If it is then is there a way to do it at the same times as the input file or should I just run the checks twice. Also is there any other checks anyone would suggest?
The other error checking functions are used while reading the file. If !file is false then that's it. You can safely start reading the file.
Also, bad() doesn't check that the file is "bad" (whatever you thought that means). It checks all error conditions and returns true if any of them is true.
Okay I took it out for now. How should I use .bad() properly? I looked it up in the documentation on this site but it does not give a real good explanation