【发布时间】:2019-03-26 08:21:42
【问题描述】:
如何获取用户的输入并将其与文件中的数据进行比较?
Myfile.txt 包含以下数据
Louise Ravel
Raven
Wings
Crosses and Bridges
Bjarne
在我的程序中
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream file("Myfile.txt");
std::string name;
std::cout<<"Enter the name to compare with the data: ";
std::getline(std::cin,name);
return 0;
}
现在,一旦用户输入,我想将输入的字符串与MyFile.txt 中可用的数据进行比较,如果找到匹配的字符串,则只需打印"Match Found"
我试过这个,但没有用。
while(file>>name)
{
if(file==name)
{
cout<<"Match Found";
}
}
我该怎么做?
【问题讨论】:
-
你试过什么?什么没用?堆栈溢出不是家庭作业写作服务。
-
我不知道该怎么做?我试过使用
while(file>>name) { if(file==name) { cout<<"Match Found"; } },但没有用 -
Edit 你的问题包括该代码。你试过调试吗?
-
我几乎是编程初学者,所以对调试一无所知
-
学习调试远比学习编码重要,见ericlippert.com/2014/03/05/how-to-debug-small-programs
标签: c++ string file-handling