input, read and output

I want to read one number from the console and then read in an arbitrary list of numbers from a txt file and then print how many times the console number occurs in the file. I want to assume that the last number in the file is 0.

This is what I have so far but I feel its incorrect, ive spent 6 hours on this and am totally stuck. I can read the file but am unsure of how to input a number into the console, read the file and then output how many times that number appears :(

#include <fstream>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;


main() {

const char* filename = "p2.txt";
std::ifstream inFile(filename);



if(!inFile) {
cout << endl << "Failed to open file " << filename;
return 1;
}

long n = 0;
while(!inFile.eof()) {


inFile >> n;
cout << std::setw(10) << n;
}
cout << endl;
system("pause");
return 0;

}

You can fetch a value from the keyboard like this:

1
2
int x;
std::cin >> x;


http://www.cplusplus.com/reference/iostream/cin/

Topic archived. No new replies allowed.