How to stop a while loop

前端 未结 5 1759
醉梦人生
醉梦人生 2021-01-14 14:41

This while loop never ends. For example, when i enter a wrong password it will keep on going to the \"incorrect password\" part over and over again.

Logo();
         


        
相关标签:
5条回答
  • 2021-01-14 14:47

    First, read the correct usernames and passwords from the file and save them in memory. That way, if the username/password validation fails the first time, you don't have to reopen or seek-to-beginning-in the file before checking on the next username/password typed by the user....

    std::map usernames_passwords; std::string username, password;

    if (ifstream in("UsernamePassword.txt"))
        while (in >> username >> password)
            usernames_passwords[username] = password;
    else
    {
        std::cerr << "Unable to open username/password file\n";
        exit(EXIT_FAILURE);
    }
    

    Then prompt for login details until they're valid:

    bool logged_in = false;
    while (!logged_in &&
           std::cout << "\n\n\n    Please enter username: " &&
           std::cin >> username &&
           std::cout << "    Please enter password: " &&
           std::cin >> password)
    {
        // look for a match in the earlier-read login details...
        auto it = usernames_passwords.find(username);
        logged_in = it != std::end(usernames_passwords) && it->second == password;
    }
    
    // the while loop could also exit because "cin >>" failed, indicating EOF
    // that could be because the user typed e.g. ^D (UNIX) or ^Z (Windows), or
    // input was from a redirected file or pipe or over a network connection...
    
    if (!logged_in)
    {
        std::cerr << "error reading login details from stdin\n";
        exit(EXIT_FAILURE);
    }
    
    ...ok - we know the username/password are good - do whatever else...
    
    0 讨论(0)
  • 2021-01-14 14:57

    It keeps on an infinite loop because you never ask if you reached the end of the file, so, if the file does not contain a username/password combination that matches the pair entered by the user, when the end of the file is reached, the line:

    inFile >> username >> password; 
    

    Fails and username and password will contain the last entries seen on UsernamePassword.txt and the loop goes forever.

    The following implementation of your program will test for the eof in the inFile file object:

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    int main() { 
        std::ifstream inFile;    
    
        std::string user, pass;
        std::string username, password;
    
        inFile.open("UsernamePassword.txt");
        if (!inFile) {
            std::cout << "Unable to Open File";
        } else {
            std::cout << std::endl << std::endl << std::endl;
            std::cout << "           Please enter username: ";
            std::cin >> user;
            std::cout << "           Please enter password: ";
            std::cin >> pass;
    
            while (username != user && !inFile.eof()) {
                inFile >> username >> password;
    
                if (user == username && pass == password) {
                    std::cout << std::endl;
                    std::cout << "Welcome to CherryLunch!" << std::endl;
    
                    // Equivalent to the 'pause' command in linux
                    system("read -p 'Press any key to continue...' key");
                    // Equivalent to the 'cls' command in linux
                    system("clear");
    
                    // MainMenu();
                } else {           
                    std::cout << std::endl;
                    std::cout << "           Invalid Username or Password!" << std::endl << std::endl;
    
                    system("read -p 'Press any key to continue...' key");
                    system("clear");
                }
            }
        }
        inFile.close(); 
    }
    
    0 讨论(0)
  • 2021-01-14 15:01

    move the cout and cin statements inside the while loop:

    else 
    {
    
        while (username != user)
        {
            cout << endl << endl << endl;
            cout << "           Please enter username: ";
            cin >> user;
            cout << "           Please enter password: ";
            cin >> pass;
            inFile >> username >> password;
    
            if (user == username && pass == password)
            {
                cout << endl;
                cout << "Welcome to CherryLunch!" << endl;
    
                system("pause");
                system("cls");
    
                MainMenu();
    
            }
            else
            {           
                cout << endl;
                cout << "           Invalid Username or Password!" << endl << endl;
                system("pause");
                system("cls");
            }
        }
    }
    inFile.close(); 
    
    0 讨论(0)
  • 2021-01-14 15:11

    You are trying to check for the username and password from the user and compare with all the usernames and passwords in the file.
    Your approach seems fine. But you are printing "Invalid username..." after each comparison, not comparing all the usernames in the file. Hence move this output outside else block and place it in the while loop.

    infile checks each line separately.

    And also check the end of file.If the username and password are not found till the end of file, then print "Invald username" and provide the user to enter another set of name and password

    Hope this helps!!

    0 讨论(0)
  • 2021-01-14 15:14

    The while loop is infinite because you never allow the user to input a new password or username. When the if statement fails, it will return to loop header (where it will still be wrong) and continue onwards.

    Give the user a chance to enter in a new user/pass combo and then the loop can still be finite (provided the user eventually provides the correct credentials).

    0 讨论(0)
提交回复
热议问题