简体   繁体   English

登录系统C ++:输入我的字符串User&Pass之后,要求我重新输入它们

[英]Login System C++: After entering my string User & Pass, It asks me to re-input them

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

The problem you're asking about comes (as the comments of your question already said) from the fact, that your break; 您要问的问题(正如您对问题的评论所说)来自事实,您break; only get's you out of the for loop (the innermost loop), not the while loop. 只会使您脱离for循环(最里面的循环),而不会进入while循环。 The easiest way to fix this would be to replace your while(true) with while(!loggedin) . 解决此问题的最简单方法是将while(true)替换为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 ;) 尝试用不同的用户名注册两个用户,然后用相同的用户名注册更多用户,并检查会发生什么;)

You are not breaking out of your while loop after performing a successful login operation. 执行成功的login操作后,您不会脱离while循环。

Also, your code crashes if fewer than 20 customers have registered. 另外,如果注册的客户少于20个,您的代码也会崩溃。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++/C 中是否有 function 要求用户通过打开文件资源管理器 window 到 select 文件来输入文件? - Is there a function in C++/C that asks the user for a file as input by opening a file explorer window for them to select the file? 尝试将用户输入传递给字符串(C++) - Trying to pass user input into string (C++) 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入? - How to re-input a user's input in case he didn't input anything or inputted wrong? 为什么我的代码不允许我将值输入到字符串变量中,而其他用户输入其他变量? C++ - Why is my code not allowing me to input values into a string variable with other user inputs for other variables? C++ 如何使用涉及变量的新值连续使用do-while循环(由用户重新输入)? - How to use do-while loop continuously with a new values for the variables involved (re-input by user)? 输入输入后,C ++ Cin保持在线 - C++ cin stay on line after entering input 如何在 C 或 C++ 中包装 arguments 并将它们传递给系统或执行* - How to wrap arguments in C or C++ and pass them to system or exec* 收到NaN输入后,C ++程序再次要求输入,然后退出 - After receiving NaN input C++ program asks for input again then quits 将带空格的字符串作为系统参数传递给C ++ - Pass string with spaces as system argument c++ C ++:如何在不使用全局变量的情况下通过系统传递用户输入? - C++: How to pass user input through the system without using global variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM