Im doing a c++ course at uni and one of my questions is to read in an external file, input a number (like 64) then the program will search the file for the number and output how many times the number the user input comes up in the file.

here is my code;

#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
using namespace std;

int main()
{
    int input, count=0, hold;
    ifstream inFile("Problem2.txt");
    
    cout << "Enter a number ";
    cin >> input;
    
    if (!inFile) {
        cout << "Unable to open file";
        return(-1);
    }
    
    int number;
    inFile >> number;
    while(!inFile.eof())
    {
     inFile >> number;
     if(number == input){
      count++;}
    }
    cout << count;
    cin >> hold;
    inFile.close();
}

the input file im using is:

64
64
92
64
64
55
56
77
75
66
25
35
39
90
19
23
44
45
88
85
15
27
32
44
68
8
0

If i input 64 as my input number it shoud tell me that 64 is in the file 4 times but it only outputs 3, and if i input 0 it should come up once but it comes up twice .I havn't got much experience in c++ so I don't really know what im doing 100%.

int number;
    inFile >> number;
    while(!inFile.eof())
    {
     inFile >> number;

You're overwriting the first number in the file. You haven't counted whether the first line in the file is the number that the user entered. That's why your program only reported 3 occurrences of 64.

Your second problem is how you're reading your file. The issue with using .eof() to find out if you're at the end of the file is that it only reports an EOF after you've already had an unsuccessful read. So the last line in the file will end up being read twice: the first time correctly, the second time it will read it again and give you an EOF.

So, I recommend using the following method instead:

while (inFile >> number)
{
  if (number == input)
  {
    /* etc */
  }
}

Then remove the line before the while loop: inFile >> number; , which will solve your first problem.

In one of the last lines I come across the following instruction: cin >> hold; If you're putting that instruction there to prevent the program from automatically closing: it's better to use cin.get(); instead ...
Please remember to also delete the variable declaration of 'hold' (somewhere in the first lines of your code) ...

Thank you, I asked my instructor how to fix it and he told me the .eof was correct. Thanks again, can't believe it was that simple.

>I asked my instructor how to fix it and he told me the .eof was correct.
Well, there's nothing 'wrong' with eof(), you just need to understand that it will only indicate an EOF after an unsuccessful read. So, for example, you could modify your code to look like:

inFile >> number;
while ( !inFile.eof() )
{
  if (number == input)
    count++;

  inFile >> number;
}

This works because it checks for an EOF after you've tried reading from the file. If there's no EOF, it goes ahead and processes the input, and then attempts another read.

Of course, if your instructor told you that your use of eof() was correct, then they really are an idiot.

And to prove that he's an..., try this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char line[80];
    ifstream inFile("x.x");
    
    while (!inFile.eof())
    {
        inFile >> line;
        cout << line << ' ';
    }
    inFile.close();
    return 0;
}

Use this file:

ONE
two
Three

What just got output?

This article has been dead for over six months. Start a new discussion instead.