繁体   English   中英

编译器看不到第一行?

[英]compiler doesn't see the first line?

我正在尝试搜索和匹配来自文本文件内容的输入,文件内容为:

Manager
Mohab
123456789
Employee
Sarah
987654321.

它不会读取第一行,并且在有条件的情况下搜索其他用户会输出第二行!

这是我的代码:

    cout << "Username : ";
    cin >> usrname;
    cout << "Password : ";
    cin >> password;
    fstream myfile;
    myfile.open("Data.txt");

        if (myfile.is_open())
        {
            while ( getline (myfile,line) )
            {
                myfile >> culmn1 >> culmn2 >> culmn3;
                if(usrname==culmn1 && password==culmn2)
                {
                    cout << culmn1 << culmn2 <<culmn3;
                    //cout << "Logedin Successfuly\n" ;
                }
                else cout << "Wrong Username or Password!\n";
            }

            myfile.close();
        }
        else cout << "Unable to open the file!\n";

预先感谢您的帮助:)

这个 :

getline (myfile,line)

将消耗流myfile的一行。

您读取文件的方式不正确。 像这样

       while (myfile >> culmn1 >> culmn2 >> culmn3)
        {
            if(usrname==culmn1 && password==culmn2)

由于某些原因,您使用了getline >>,getline读取第一行,>>读取第二行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM