7stud, the OP is using a version of get that takes a single char as a paramiter. The normal way of doing this is:
Code:
char next;
while(inStream.get(next) && next != '\n') {
if(next == '#') cout << " I'm doing hash ";
else cout << next;
}
This avoids subtle problems that appear to be the OP's difficulties. Namely this
Code:
while (! inStream.eof())
// eof() is not set if the file was not
// opened or has some other error
// eof() should only be used after
// file processing has finished to veryfy
// that the entire file was consumed.
{
while(next != '\n')
{
if(next=='#')
cout<<NUMBER_OF_CHAPTERS;
else
cout<<next;
inStream.get(next);
// this line is never reached if next == '\n'
// this is the only line that consumes
// characters so the file never reaches
// eof.
// This is your bug.
}
}
There are some other bugs in the use of ignore, I find this utility function is usefull enough that it should have been encluded in the standard library
Code:
inline
bool eat(std::istream &is, char term) {
char ch;
while(is.get(ch)) {
if(term == ch) return true;
}
return false;
}
use eat(cin,'\n') whenever you want to throw away the current line in the buffer. After asking your question and after asking if you want to ask another question. The other problem is where you re-open your answer file you do not reset number of answers to zero. after the first seven anwers it will repeat the entire file an then loop forever. This is where eof() is used.
Code:
if(!inStream.good()) {
if(inStream.eof()) {
inStream.close();
inStream.open("A:\\answerfile.txt");
} else {
cout << "Problem with answerfile" << endl;
return 2;
}
}