简体   繁体   English

为什么只能读取第一行?

[英]Why it can only read the first line?

I'm a beginner of C++. 我是C ++的初学者。 This is a part of the assignment at school. 这是学校作业的一部分。 This is the login part. 这是登录部分。

I need to read the PIN from an external file which consist of many users's PIN in it. 我需要从包含许多用户PIN的外部文件中读取PIN。

I need to compare the pin which user just typed in with the pin stored in file. 我需要将用户刚刚输入的图钉与文件中存储的图钉进行比较。 If the pin is matched, then the user is successfully login. 如果引脚匹配,则用户成功登录。 If not then the user has 2 more attempts to type in the PIN again. 如果不是,则用户有两次尝试再次输入PIN。

The problem I'm facing now is I can't read the data from the other lines except the first line. 我现在面临的问题是我无法从第一行以外的其他行读取数据。

When I typed in the PIN of other lines which are correct, the program will always show me wrong PIN. 当我输入其他正确的PIN码时,程序将始终显示错误的PIN码。 Only if I typed in the first line, it will just show correct PIN. 仅当我输入第一行时,它才会显示正确的PIN码。

Please help me to find out the problem. 请帮助我找出问题所在。 Thank you. 谢谢。

069906 069906

777329 777329

143003 143003

069021 069021

void olduser ()
{
int userpin,a;
cout << "*************************************************************" << 
endl << endl;
input.open("userdata.txt");
cout << "Please enter your PIN." << endl;
cin>>userpin;

if(input.is_open())
    {   
    while(input >> pin) 
    {

     if(userpin == pin) //compare the pin typed in with the pin in file
        {   
        cout << "You have entered the correct PIN." << endl <<endl;
        cout << "You have successfully login." << endl <<endl;
        cout << "Redirecting......" << endl;
        system("pause");
        break;
            }
    else if (userpin != pin)
        {
         for(a=1;a<=2;a++)
          { 
           cout << "You have entered the wrong PIN." << endl;
           cout << "Please try again." << endl;
           cin >> userpin;

           if(userpin == pin)
           {    
             cout << "You have entered the correct PIN." << endl <<endl;
             cout << "You have successfully login." << endl <<endl;
             cout << "Redirecting......" << endl;
             system("pause");
             break;
             }

            }           
        system("cls");
        cout << "The PIN you have entered has no match." <<endl <<endl; 
        cout << "Please try again later. Thank you." << endl << endl;
        cout << "Exiting IVM......" << endl;
        system("pause");
        break;                      
        }   
        }
        }
   else
    {
        cout << "Unable to open file." << endl;

     }  

    }

int attempts = 0;
   while (attempts < 3)
   {
    bool logged = false;
    if (input.is_open())
    {
        while (input >> pin)
         {
            if (userpin == pin)
            {
            //if the PIN is found in the external file

            cout << "You have successfully login." << endl << endl;
            cout << "Redirecting to main menu......" << endl;
            system("pause");
            logged = true;
            mainmenu();
            }
        }
    }
   else
   {
    //If the external file cannot be opened
    cout << "Unable to open file." << endl;
    break;
   }
     if(logged) break; 
     {
    //if the PIN is not found in the external file
   cout <<"The PIN is not matched. Please try again." << endl;
   cin>>userpin;
   }
    attempts++;
    }
   if(attempts == 3)
   {
   //the login is unsuccessful after using up 2 attempts
    cout <<"Your PIN is not matched. Please try again next time." <<endl 
   << endl;
    }

Answer is simple: All your code possible paths lead to break while loop in its first iteration, so it won't read more than just fist line from input . 答案很简单:您的所有代码可能的路径都会在第一次迭代while导致while循环中断,因此它不仅从input读取第一行而已。

Also, because of above, inside your else if (userpin != pin) , your for loop will always check for the same pin . 同样,由于上述原因,在else if (userpin != pin)for循环将始终检查相同的pin

Example solution: 解决方案示例:

int loginAttempts = 0;
while (loginAttempts < 3)
{
    //Prepare file to reading
    bool logged = false;
    if (input.is_open())
    {
        while (input >> pin)
        {
            if (userpin == pin)
            {
                //Handle successful login attempt
                logged = true;
                break;
            }
        }
    }
    else
    {
        //Handle file openning failure
    }
    if(logged) break;
    //Handle unsuccessful login attempt
    loginAttempts++;
}
if(loginAttempts == 3)
{
    //Handle unsuccessful login
}

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

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