为什么只能读取第一行?

时间:2018-11-07 14:10:40

标签: c++ file-io

我是C ++的初学者。这是学校作业的一部分。 这是登录部分。

我需要从一个包含许多用户PIN的外部文件中读取PIN。

我需要将用户刚刚输入的密码与文件中存储的密码进行比较。如果引脚匹配,则用户成功登录。如果不是,则用户将再尝试2次再次输入PIN。

我现在面临的问题是我无法从第一行以外的其他行读取数据。

当我输入其他正确的PIN码时,程序将始终显示错误的PIN码。仅当我输入第一行时,它才会显示正确的PIN码。

请帮助我找出问题所在。谢谢。

069906

777329

143003

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

1 个答案:

答案 0 :(得分:1)

答案很简单:所有可能的代码路径都会在第一次迭代中导致中断while循环,因此它读取的内容不仅仅只是input中的第一行。

此外,由于上述原因,在else if (userpin != pin)内,您的for循环将始终检查相同的pin

示例解决方案:

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
}