Original Post
I need to check and see if a user-entered number is in a file. My TA said to do it like this:
int IDnumber;
int IDcheck;
while (IDnumber != 0)
{
in_stream.open("input.txt");
cout << "Enter ID number (0 to exit): ";
cin >> IDnumber;
//exit loop upon recieving zero
if (IDnumber == 0)
break;
while (! in_stream.eof())
{
in_stream >> IDcheck;
if (IDcheck == IDnumber)
cout << "YES it''s there.";
break;
}
in_stream.close();
}
If the number they entered is in the file, the loop continues, closes and reopens the file, and sets the cursor back at the beginning upon reopening. The only problem is that if the number isn''t there (and thus in_stream.eof() returns true), when the loop restarts and reopens the file, the cursor is still at the end of the file. Is there a way I can put it back at the beginning (and/or a better way than closing and reopening it)?
Thanx