将文件中的数字与控制台输入进行比较

Comparing numbers in a file to a console input

本文关键字:输入 比较 控制台 文件 数字      更新时间:2023-10-16

我正试图编写一个程序从控制台读取一个数字,然后从文件中读取任意数量的数字。然后,它应该打印出控制台编号在从文件读取的列表中出现的次数。我不知道怎么做第二部分,谁能帮我计算这个数字出现了多少次?到目前为止,我唯一拥有的代码是第一部分。

我对第二部分的想法是这样的:

 while(!inputfile.eof){
    if(inputfilenumber == consolenumber){
       counter = counter + 1;
  }

但是我似乎不能把这个付诸实践,因为我不确定它是如何工作的。

示例代码您可以使用它作为参考。

int input;
int count = 0;
cin>>input;
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (myReadFile.is_open()) 
{
  while (!myReadFile.eof())
   {          
     myReadFile >> output;
     int val = atoi(output);
     if(val == input)
      {
          cout<<"found input"<<input;
          count++;
      }
   }
}
cout<<"count ="<<count;
myReadFile.close();