1

I am creating a function that checks the user's username and password. When I enter the username and password correctly, the program outputs the correct string. Then, when I enter the incorrect username OR password, the correct while loop is output. However, when I try to enter the correct username after the while loop, I get an infinite loop of my while loop contents.

What is causing this infinite loop when entering the username a second time?

Here is the function I am working on:

void CheckUserPermissionAccess()
{

    int userPassInput;
    string usernameInput;

    string user1 = "Bob Jones";
    string user2 = "Sarah Davis";
    string user3 = "Amy Friendly";
    string user4 = "Johnny Smith";
    string user5 = "Carol Spears";


    int BobPass = 9874;
    int SarahPass = 3478;
    int AmyPass = 2991;
    int JohnnyPass = 5503;
    int CarolPass = 2814;

    cout << "Enter your username: " << endl;
    getline(cin, usernameInput); 
    cout << "Enter your password: " << endl;
    cin >> userPassInput;


    if (usernameInput == user1 && userPassInput == BobPass)
    {
        cout << "password accepted" << endl;
        cout << endl;
    }
    else if (usernameInput == user2 && userPassInput == SarahPass)
    {
        cout << "password accepted" << endl;
        cout << endl;
    }
    else
    {
        while (usernameInput != user1 || userPassInput != BobPass)
        {
            cout << "Invalid Username or Password. Please try again" << endl;
            cout << "Enter your username: " << endl;
            getline(cin, usernameInput);
            cout << "Enter your password: " << endl;
            cin >> userPassInput;
        }
    }

    return;

}

Here is a small part of the infinite loop that gets displayed after entering the username a second time:

Invalid Password. Please try again
Enter your username: 
Enter your password: 
Invalid Password. Please try again
Enter your username: 
Enter your password: 
Invalid Password. Please try again
Enter your username: 
Enter your password: 
Invalid Password. Please try again
Enter your username: 
Enter your password: 
Invalid Password. Please try again
Enter your username: 
Enter your password: 
Invalid Password. Please try again
Zane Brown
  • 31
  • 4
  • 1
    Because your previous `enter` press is in the input as `\n` as well but you never ignore or clear it it. See [this post](https://stackoverflow.com/a/21567292/4123703). – Louis Go Feb 17 '21 at 05:23

2 Answers2

1

You need to ignore the rest of the current line after the usage of std::cin, up to EOF or '\n' – so that the earlier input is cleared. This problem can be easily fixed by adding two lines:

#include <limits>

void function() {
   .
   while(.) {
       cin.clear();
       cin.ignore(numeric_limits<streamsize>::max(), '\n');
       .
       .
   }
}

I've got these output:

Enter your username: 
Bob
Enter your password: 
123

Invalid Username or Password. Please try again
Enter your username: 
Bob Jones
Enter your password: 
9874
Rohan Bari
  • 6,523
  • 3
  • 9
  • 30
1

Answer according to your code:

#include<bits/stdc++.h>
#include<limits>
using namespace std;

void CheckUserPermissionAccess()
{

    int userPassInput;
    string usernameInput;

    string user1 = "Bob Jones";
    string user2 = "Sarah Davis";
    string user3 = "Amy Friendly";
    string user4 = "Johnny Smith";
    string user5 = "Carol Spears";


    int BobPass = 9874;
    int SarahPass = 3478;
    int AmyPass = 2991;
    int JohnnyPass = 5503;
    int CarolPass = 2814;

    cout << "Enter your username: " << endl;
    getline(cin, usernameInput); 
    cout << "Enter your password: " << endl;
    cin >> userPassInput;


    if (usernameInput == user1 && userPassInput == BobPass)
    {
        cout << "password accepted" << endl;
        cout << endl;
    }
    else if (usernameInput == user2 && userPassInput == SarahPass)
    {
        cout << "password accepted" << endl;
        cout << endl;
    }
    else
    {
        
        while (usernameInput != user1 || userPassInput != BobPass)
        {
            cout << "Invalid Username or Password. Please try again" << endl;
            cout << "Enter your username: " << endl;
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            getline(cin, usernameInput);
            cout << "Enter your password: " << endl;
            cin >> userPassInput;
        }
    }

    return;

}


int main(){
    CheckUserPermissionAccess();
}

You can also read this article for your better understanding.

Dharman
  • 21,838
  • 18
  • 57
  • 107