C ++无法正确读取文件

时间:2018-10-25 15:55:13

标签: c++ windows c++11 visual-c++ c++14

为什么C ++无法正确阅读文本?即使我输入下图所示的名称之一,它也会写“错误名称”。在下面查看屏幕截图

enter image description here

#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;
}

1 个答案:

答案 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;
}

让我知道它是否有效。

相关问题