将文件中的数字与控制台输入进行比较
Comparing numbers in a file to a console input
我正试图编写一个程序从控制台读取一个数字,然后从文件中读取任意数量的数字。然后,它应该打印出控制台编号在从文件读取的列表中出现的次数。我不知道怎么做第二部分,谁能帮我计算这个数字出现了多少次?到目前为止,我唯一拥有的代码是第一部分。
我对第二部分的想法是这样的:
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();
相关文章:
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 使用-L和-L标志与将库文件作为输入进行比较
- 通过与输入进行比较来安排输出
- 将私有对象变量与用户输入的变量进行比较
- 输入迭代器的相等性比较
- 将用户输入的术语与 txt 文件中的列表进行比较
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- 如何使用文件填充数组并将其与用户输入C 进行比较
- 从类中查看 rand() 并将用户输入与计算机选择进行比较.石头,纸,剪刀游戏
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 在比较特定输入中的程序的数组中出错
- 将最大最小值的数据与可变数量的输入进行比较 C++
- c++ 输入一个数字来确定其他数字,然后比较它们
- 比较输入的C弦与文件读取c-string
- 将用户输入与矢量中存储的值进行比较
- C++ - 读取和比较用户输入
- 将结果与用户输入-SQLITE3 C 进行比较
- 正在检查用户的有效输入并与文本文件进行比较
- C++如何将用户输入与具有严格排序的字符串列表进行比较