输入不断要求更多

问题描述 投票:0回答:1

上下文:我正在尝试用 C++ 编写一个小程序,要求用户创建一个帐户然后登录。创建帐户时,它会要求输入角色、用户名和密码。要选择角色,您必须输入一个整数 1-3。当它向我询问这个角色时,问题就出现了。我输入一个数字,单击 Enter,仍然让我输入另一个数字,而不是进入下一部分。所以我总共要输入2个数字,但程序仍然继续。创建帐户后,我尝试登录。它还要求输入角色、用户名和密码。在登录中它工作正常,但它不允许我登录。我 99% 确定它一定是因为这个问题。但也可以是其他任何东西。我不是专家,我才刚刚开始编程。

#include <iostream>
#include <vector>


using namespace std;

struct User // struct User stores username and password
{
    string username;
    string password;
    string role;
};

vector<User> users; // vector User stores user data

void createAccount() // function to create account
{
    User user;

    cout << "Select your role: " << endl;
    cout << "1. Tier I 2. Tier II 3. Tier III" << endl;

    int roleChoice; // variable for role selection
    cin >> roleChoice; // user selects role by inputing an integer

    switch (roleChoice) // switch enables 3 possible roles
    {
    case 1:
        user.role = "Tier I";
        break;
    case 2:
        user.role = "Tier II";
        break;
    case 3:
        user.role = "Tier III";
        break;
    default:
        cout << "Invalid role selected. Try again." << endl; // invalid input
        return;
    }

    cin >> user.role;
    cout << "Enter a user name: ";
    cin >> user.username;
    cout << "Enter a password: ";
    cin >> user.password;
    users.push_back(user); // push_back user adds a user to the vector User
    cout << "Account created successfully!\n";

}

bool login(string& loggedInUser)
{
    string username, password, role;
    cout << "Select your role:\n";
    cout << "1. Tier I 2. Tier II 3. Tier III" << endl;

    int roleChoice; // variable for role selection
    cin >> roleChoice; // user selects role by inputing an integer

    switch (roleChoice) // switch enables 3 possible roles
    {
    case 1:
        role = "Tier I";
        break;
    case 2:
        role = "Tier II";
        break;
    case 3:
        role = "Tier III";
        break;
    default:
        cout << "Invalid role selected. Try again.\n"; // invalid input
        return false;
    }

    cout << "Enter your username: ";
    cin >> username;
    cout << "Enter your password: ";
    cin >> password;

    for (const User& user : users) // loop searches for information that matches
    { // const in order to avoid any changes

        // verifies that the information inputed is the same
        if (user.role == role && user.username == username && user.password == password)
        {
            loggedInUser = username;
            cout << "Login succesful. Welcome, " << loggedInUser << " (" << role << ")\n ";
            return true; // if information is correct, user logs in
        }
    }

    cout << "Login failed. Invalid role, username, or password.\n";
    return false;
}


int main()
{
    string loggedInUser;
    int choice;
    do // user chooses to create account, login, or exit
    { 
        cout << "1. Create account\n2. Login\n3. Exit\n";
        cin >> choice;

        switch (choice)
        {
        case 1:
            createAccount();
            break;
        case 2:
            if (login(loggedInUser))
            {
                cout << "Welcome, " << loggedInUser << "!\n";

                int loggedInChoice;
                do {
                    std::cout << "1. Perform action 1\n2. Perform action 2\n3. Logout\nEnter your choice: ";
                    std::cin >> loggedInChoice;
                    switch (loggedInChoice)
                    {
                    case 1:
                        // Implement action 1.
                        std::cout << "Action 1 performed.\n";
                        break;
                    case 2:
                        // Implement action 2.
                        std::cout << "Action 2 performed.\n";
                        break;
                    case 3:
                        std::cout << "Logged out. Goodbye!\n";
                        break;
                    default:
                        std::cout << "Invalid choice. Please try again.\n";
                        break;
                    }
                } while (loggedInChoice != 3);
            }
            break;
        case 3:
            cout << "Goodbye!\n";
            break;
        default:
            cout << "Invalid choice. Try again.\n";
            break;
        }

        
    } while (choice != 3);

    return 0;

}
c++ input switch-statement
1个回答
0
投票

在代码的这一部分中:

void createAccount() // function to create account
{
    User user;

    cout << "Select your role: " << endl;
    cout << "1. Tier I 2. Tier II 3. Tier III" << endl;

    int roleChoice; // variable for role selection
    cin >> roleChoice; // user selects role by inputing an integer

    switch (roleChoice) // switch enables 3 possible roles
    {
    case 1:
        user.role = "Tier I";
        break;
    case 2:
        user.role = "Tier II";
        break;
    case 3:
        user.role = "Tier III";
        break;
    default:
        cout << "Invalid role selected. Try again." << endl; // invalid input
        return;
    }

    cin >> user.role;
    cout << "Enter a user name: ";
    cin >> user.username;
    cout << "Enter a password: ";
    cin >> user.password;
    users.push_back(user); // push_back user adds a user to the vector User
    cout << "Account created successfully!\n";

}

您已经要求进行角色选择,然后根据用户的输入进行分配。

然后你输入一个

 cin >> user.role;
,正如你所猜测的,它会第二次询问用户该选项。

并且由于(例如)您在第一次尝试时按了 1,因此

user.role
的值将是 I 级。

但是由于您在第二次尝试时再次按了 1,因此

user.role
的值将为 1。因为您通过第二次输入为其分配了 1。这就是你的程序中出现整个错误的原因..

告诉我是否有效!!

© www.soinside.com 2019 - 2024. All rights reserved.