我正在制作一个简单的程序,它读取文件和用户的值,然后计算该值在文件中出现的次数。到目前为止,我已经做到了,它编译得很好,但是当你输入一个数字时,那里什么也没有发生。我难住了。对不起,如果这是非常基本的,但我无法超越这一点。
这就是我到目前为止所拥有的。
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
int hold,searchnumber, counter=0;
int main()
{
cout << "This program reads the contents of a file to discover if a number you enter exists in it, and how many times. \n";
cout << "What number would you like to search for? \n";
cout << "Number : ";
cin >> searchnumber;
ifstream infile("problem2.txt");
if(!infile)
{
cout << "Can't open file problem2.txt";
exit(EXIT_FAILURE);
}
int sum=0,number;
infile >> number;
while (!infile.eof())
{
if (number == searchnumber);
counter = counter += 1;
}
{
cout << "The number " <<searchnumber << " appears in the file " << counter <<" times! \n";
cin >> hold;
}
infile.close();
}