如果我有一个用户记录在一行内如何找到登录的数据

C++ if i have a user records within a line how can find the data to login

本文关键字:一行 何找 数据 登录 用户 有一个 记录 如果      更新时间:2023-10-16

下面的代码不从文件中获取数据。它总是显示You have entered wrong data!!!,我需要从一行中选择用户名和密码的代码。

我的记录是这样的:username password name id Contactnumber

if(choose ==2)//teacher
    {   
        string line=" ";
        ifstream read("teacher.txt");
        cout <<" Enter Username: "; cin >> username;
        cout <<" Enter Password: "; cin >> password;
        bool found = false;
        while (getline(read,line))
        {
            stringstream ss(line);
            ss>>un>>pw;
            if (un == username && pw == password)
            {
                cout<<"You have logged in as Teacher!"<<endl;
                found = true;
                break;
                //system("pause");
            }
        }
        if(!found)
        {
            cout<<"nYou have entered wrong data!!!"<<endl;
        }
        read.close();
    }
cin >> username;
cin >> password;

对象cin表示标准输入(默认为键盘)。文件"teacher.txt"在代码中由对象read表示。要从这个文件中读取,你应该这样写:

read >> username;
read >> password;

顺便说一下,read是函数的好名字,但不是对象的好名字。