Chao Met
Chao Met

Reputation: 13

Why it can only read the first line?

I'm a beginner of 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.

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.

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. Only if I typed in the first line, it will just show correct PIN.

Please help me to find out the problem. Thank you.

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

Upvotes: 0

Views: 136

Answers (1)

Rhathin
Rhathin

Reputation: 1174

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.

Also, because of above, inside your else if (userpin != pin), your for loop will always check for the same 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
}

Upvotes: 1

Related Questions