为什么C ++无法正确阅读文本?即使我输入下图所示的名称之一,它也会写“错误名称”。在下面查看屏幕截图
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream data_base;
data_base.open("database.txt", ios::out);
string name, a;
int b, c, d, e, test=0;
system ("cls");
cout<<"enter name "<<endl;
cin>>name;
while (data_base >> a >> b >> c >> d >> e){
if (name == a) test=1;
}
if (test!=1)
cout<<"wrong name"<<endl;
return 0;
}
答案 0 :(得分:-2)
尝试以下操作:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream data_base;
data_base.open("database.txt", ios::out);
string name, a;
int b, c, d, e, test = 0;
system ("cls");
cout << "enter name ";
cin >> name;
while (data_base >> a >> b >> c >> d >> e)
{
if (name == a)
{
test = 1;
break;
}
}
if (test!=1)
cout << "wrong name" << endl;
return 0;
}
让我知道它是否有效。