0

My code is a Shop that I am trying to build, it works(Register system working) until I input my User and Password(Login System), after inputting my Username, the program asks me to re-login and it keeps doing it(Pretty sure it is because it is a while true loop). (This is a project, so there are separate different files with constructors and classes.)

Here is part of my Code:

while(true)
{
    cout << "Would you like to register or login?" << endl;
    string answer = "";
    cin >> answer;

    if(answer == "register" || answer == "Register")
    {
        cout << "What would be your designated username?: " << endl;
        string newUser;
        cin >> newUser;
        for(int i = 0; i < 20; i++)
        {
            if(customers[i] -> username != newUser)
            {
                cout << "what would be your designated password?: " << endl;
                string newPass;
                cin >> newPass;
                customers[lastRegisteredID] = new Customer(newUser, newPass);
                lastRegisteredID++;
                break;
            }
        }

        //^Register Part.
    }

    if(answer == "login" || answer == "Login")
    {
        cout << "Your username: " << endl;
        string UserAttempt;
        cin >> UserAttempt;
        for(int j = 0; j < 20; j++)
        {
            if(customers[j] -> username == UserAttempt)
            {
                cout << "Username Found!" << endl;
                tempCustomer = customers[j];

                cout << "Your password: " << endl;
                string PassAtempt;
                cin >> PassAtempt;

                if(tempCustomer -> password == PassAtempt)
                {
                    cout << "Password correct \n Successfully logged in." << endl;
                    loggedin = true;
                    break;
                }
            }
        }
    }

    //^Login part.
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

2 Answers2

0

The problem you're asking about comes (as the comments of your question already said) from the fact, that your break; only get's you out of the for loop (the innermost loop), not the while loop. The easiest way to fix this would be to replace your while(true) with while(!loggedin). Notice also, that there are some other problems in the code you've posted. As I don't know if these bugs are also in your final code, I will only list them (the few I've found):

  • There's a memory leak in your regestration system: You don't delete your old customer
  • The way your users are registered/saved in the array is propably different from what you intended. Try to register two users with different user names, then register more users with the same user name, and check what happens ;)
Bizzarrus
  • 1,194
  • 6
  • 10
  • Thank you, I will see how i will deal with those other problems. – Blacklight1098 Feb 06 '18 at 22:20
  • Also, I replace the `while(true)` with `while(!loggedin)` and it still tells me to input register or login again. – Blacklight1098 Feb 06 '18 at 22:48
  • @Blacklight1098 if replacing the while doesn't fix the problem, there's something outside of the code you've posted that causes problems. From the code you've posted, replacing the while will stop the while loop to repeat as soon as someone has successfully logged in, because the `loggedin` variable is set to true, making the condition in the loop to break, so it won't repeat again - unless something else sets the variable back to false, or causes additional repeats. – Bizzarrus Feb 07 '18 at 01:18
  • I found out what broke the program at my Programming school, But thanks to those who tried to help me. `lastregisteredID` did not have an actual integer number assigned to it so it was NUL or whatever it was defaulted to, so I had to put in `lastregisteredID = 0;` to fix out the fact it was not saving the registered customers. In the end, the problem was it was not saving the registered customers cause there was no ID being created. – Blacklight1098 Feb 12 '18 at 22:39
0

You are not breaking out of your while loop after performing a successful login operation.

Also, your code crashes if fewer than 20 customers have registered.

Also, your code allows multiple users to register using the same username and even the same password.

Try something more like this instead:

Customer* customers[20];
int numCustomers = 0;
bool loggedin = false;

Customer* findCustomer(const std::string &user)
{
    for(int i = 0; i < numCustomers; ++i)
    {
        if (customers[i]->username == user)
            return customers[i];
    }
    return NULL;
}

...

while (true)
{
    std::cout << "Would you like to register or login?" << std::endl;
    std::string answer;
    std::cin >> answer;

    std::transform(answer.begin(), answer.end(), ::tolower);

    if (answer == "register")
    {
        std::cout << "What would be your designated username?: " << std::endl;
        string newUser;
        std::cin >> newUser;

        Customer *cust = findCustomer(newUser);
        if (cust)
        {
            std::cout << "That username is already taken!" << endl;
            continue;
        }

        if (numCustomers >= 20)
        {
            std::cout << "Too many users are registered!" << endl;
            continue;
        }

        std::cout << "what would be your designated password?: " << std::endl;
        std::string newPass;
        std::cin >> newPass;

        customers[numCustomers] = new Customer(newUser, newPass);
        ++numCustomers;

        continue;
    }

    if (answer == "login")
    {
        std::cout << "Your username: " << std::endl;
        std::string UserAttempt;
        std::cin >> UserAttempt;

        std::cout << "Your password: " << std::endl;
        std::string PassAttempt;
        std::cin >> PassAttempt;

        Customer *cust = findCustomer(UserAttempt);
        if ((cust) && (cust->password == PassAttempt))
        {
            std::cout << "Successfully logged in" << std::endl;
            loggedin = true;
            break;
        }

        std::cout << "Not logged in!" << std::endl;
        continue;
    }

    std::cout << "Unknown command! Try again" << std::endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770