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%.