#include int main () { cout << "Enter a number: "; int num; cin >> num; while (!cin.eof()) { cout << "You entered: " << num << endl; cout << "\nnext number: "; cin >> num; } cout << "End of File" << endl; return 0; } /* Enter a number: 4 You entered: 4 next number: 5 You entered: 5 next number: End of File */ //--------------------------------------------------------- #include int main () { cout << "Enter a number: "; int num; while (cin >> num) { cout << "You entered: " << num << endl; cout << "\nnext number: "; } cout << "Error on input" << endl; return 0; } /* Enter a number: 5 You entered: 5 next number: 6 You entered: 6 next number: Error on input */ //--------------------------------------------------------- #include int main () { cout << "Enter a number: "; int num; while (!(cin >> num).eof()) { cout << "You entered: " << num << endl; cout << "\nnext number: "; } cout << "End of File" << endl; return 0; } /* Enter a number: 4 You entered: 4 next number: 3 You entered: 3 next number: End of File */ //--------------------------------------------------------- #include int main () { cout << "Enter an ascii number: "; char number[10]; char c; while (cin >> c) { cin.putback(c); cin >> number; cout << "You entered: " << number << endl; cout << "\nnext ascii number: "; } cout << "Error on input (EOF?)" << endl; return 0; } /* Enter an ascii number: 4 You entered: 4 next ascii number: 5 You entered: 5 next ascii number: */ //--------------------------------------------------------- #include int main () { char s[100]; char c; cout << "Enter a string: "; while (true) { cin.get (s, 100); // cin.get (c); if (cin.eof()) { cout << "end of file" << endl; return 0; }; cout << "You entered:" << s << ":" << endl; cout << "next string: "; } cout << "End of File" << endl; return 0; } /* Enter a string: this is a test You entered:this is a test: next string: You entered:: next string: You entered:: next string: You entered:: .................. interruption -- terminating This does not work on sundog, falun or the MAC What is the problem? cin.get (s,100) has read up to the \n cin.eof() does not succeed, but the \n is not removed. Therefore infinite loop reading the NULL string. */ //--------------------------------------------------------- #include #include int main () { char s[100]; ofstream TestOutput ("output.data", ios :: out); ifstream TestInput ("input.data", ios :: in); if (!TestOutput) cout << "output.data not attached\n"; if (!TestInput) cout << "input.data not attached\n"; cout << "Enter a string: "; TestOutput << "Enter a string: "; while (true) { cin.getline (s, 100, '\n'); TestInput.getline (s, 100, '\n'); if (TestInput.eof()) { cout << "end of file" << endl; TestOutput << "end of file" << endl; TestOutput.close(); return 0; }; cout << "You entered: \"" << s << "\"" << endl; cout << "next string: "; TestOutput << "You entered: \"" << s << "\"" << endl; TestOutput << "next string: "; TestOutput.flush(); } cout << "End of File" << endl; TestOutput << "End of File" << endl; TestOutput.close(); return 0; } /* Enter a string: This a test You entered: "This is a string" next string: And another You entered: "And this is another" next string: // a CR on standard input You entered: "" next string: // another CR on standard input end of file // should have found EOF on input.data sooner */ /* input.data This is a string And this is another */ /* output.data Enter a string: You entered: "This is a string" next string: You entered: "And this is another" next string: You entered: "" // not so on input.data next string: end of file // finally found the EOF */ /* This example illustrates that the structure of a file can be: First Line\n Other lines\n Last Line\n \nEOF Clearly you have to watch for this, since it is possible to construct files without two \n\n at the end. That is, finishing Last Line\nEOF */