How would I alter this program in order to use the eof member function on the stream cin??
This program will terminate as soon as anything but an integer is entered.Code:int main(void) { int i, sum = 0; cout << "Enter an int: " ; while ( cin >> i ) { cout << " i=" << i << endl; sum += i; cout << "Enter an int: " ; } cout << "The sum of the numbers entered is: " << sum << endl; return 0; }
I've tried many different things, but when I use eof, the program doesn't recognize that only integers have to be entered, i.e. it takes in characters as well. I keep getting either an infinite loop or the program never terminates.
I also have a question about the fail() member function. If I have
is there anyway that instead of calling the fail() member function on the stream, to put the "not" operator in front of the stream name? I've tried this as well, but I can't understand how to use the "not" operator with the stream name in order to make sure that the file is indeed opened.Code:if (!infile.fail())
Any help would be appreciated as always.



LinkBack URL
About LinkBacks



