This might work. I'm not sure how to get it to recognize eof with my borland compiler though. I tried <ctrl>d and <ctrl>c.
Code:
int main(void)
{
int i, sum = 0;
cout << "Enter an int: " ;
cin >> i;
while ( !cin.eof()) {
if (cin.fail())
{
cin.clear();
cin.ignore();
}
else
{
cout << " i=" << i << endl;
sum += i;
}
cout << "Enter an int: " ;
cin >> i;
}
cout << "The sum of the numbers entered is: " << sum << endl;
return 0;
}
To answer your second question, you can use something like:
Code:
ifstream in(filename);
if (!in.is_open())
{
cout << "File not found:" << filename << endl;
return 1;
}
Or:
Code:
ifstream in(filename);
if (!in)
{
cout << "File not found:" << filename << endl;
return 1;
}