I am trying to write a program which will ask the user to input an integer, and will then proceed to search a predefined text file (containing integers only) for the number of occurrences of that specific integer, printing the result. Here is what I have coded so far (doesn't work when I run it), but I don't know if I'm going in the right direction here, or if I should be trying to read all the integers from the file into an array, or even do something else entirely. I should add that this is for homework, and I am just looking for pointers, not a full solution. I have tried everything I can think of, and haven't been making much progress. Could someone point me in the right direction? Thanks in advance.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
int x, y;
int sum1 = 0;
ifstream infile ("file.txt");
if (infile)
cout << "File successfully opened" << endl;
else
{
cout << "Can't open file" << endl;
exit (EXIT_FAILURE);
}
cout << "Input number: ";
cin >> x;
int number;
while(!infile.eof()) //this is where my problems start
{
infile >> number;
}
while(number = x) //doesn't seem to be working, not sure if I should even be using
//a while statement
{
sum1++;
}
cout << sum1++ << " " ;
}