0

I want to read a file and see if there are either 1 or 2 numbers in it. and if those 1 or 2 of the numbers are not in the file, then I want to ask the user to enter them.

I am using the following code:

ifstream fin;

while (!fin.eof())
{
    fin >> x;
    counter++;

    fin >> y;
    counter++;
}

switch (counter)
{
case 0:
    cout << " Enter the 1st number";
    cin >> x;
    break;
case 1:
    cout << " Enter the 2nd number";
    cin >> y;
    break;
default:
    break;
}

But if for example, one of the numbers are not in the file, I get an uninitialized value instead.

5gon12eder
  • 21,864
  • 5
  • 40
  • 85
Mostafa Bahri
  • 920
  • 1
  • 13
  • 19
  • Your `{}` brackets were broken, I've submitted an edit that I think is what you intended. (waiting for peer review). I hope it is right. – Baldrickk Oct 21 '14 at 15:18
  • Are you sure the error message uses the word "contain"? –  Oct 21 '14 at 15:21
  • Your `switch` probably does not do what you want. In the 0 case, you should ask the user for two inputs, no? But you'll always `break` out after asking for the first input. I think two `if`s would be more appropriate here but a `switch` with fall-through would also work. – 5gon12eder Oct 21 '14 at 15:26
  • 1
    `while (!fin.eof())` [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Oct 21 '14 at 15:26

1 Answers1

2

You are trying to read both variable from the file even if you don't know if they are present.

I would suggest to check for eof each time you want to read a variable and to check for fail to ensure the reading was OK.

if( !fin.eof() )
{
    fin >> x;
    if( !fin.fail() )
    {
        counter++;
    }
}
if( !fin.eof() )
{
    fin >> y;
    if( !fin.fail() )
    {
        counter++;
    }
}

This way, counter will only be incremented if the variable has been correctly read in the file.

You can read more about eof, fail and other iostates.

Vincent Robert
  • 33,284
  • 13
  • 77
  • 115
  • 2
    Why not simply `if(fin >> x)`? – πάντα ῥεῖ Oct 21 '14 at 15:28
  • 1
    While `if(fin >> x)` does seem more elegant, checking specifically for the flag that you're looking for, I feel, helps your code be more **explicit**. In this case, anyone who reads this piece of code (and knows what the `failbit` represents) will be able to tell that the author is checking for failed extraction due to **end-of-file** or **invalid value**. – Nard Oct 21 '14 at 15:51
  • `fin >> x` is highly unlikely to ever return `false`. The `>>` operator in this context always returns a reference to `fin`, irrespective of the success or failure of the actual input operation. – dgnuff Oct 21 '14 at 16:01
  • tnx. it worked.given the case there is no number in the file, we see that though there is nothing there but it gets into the first while loop. meaning, !file.eof() was true. why??? – Mostafa Bahri Oct 21 '14 at 17:10
  • You might have an empty line or spaces that are not numbers but not an empty file either. That(s why I added the `fail` test, because your file may contain something else other than numbers. – Vincent Robert Oct 22 '14 at 08:15