简体   繁体   中英

(C++) Read username and password from a file output problem

my program of registration and login encountered a few problems during output:

  1. I have to register a new user and pass first(even if i alrd have previous usernames and passwords stored in the text file im trying to retrieve it from), after that only i can login using previous username and passwords and this repeats after i close the debug window and start debugging again (if i directly choose to login upon running the program, it will output "invalid username or password")

  2. when logging out from a newly registered username, the program jumps to the int main() AND DISPLAY "1. Register...." but logging out from previous usernames, it jumps to void login() and display "Username:"

*note: the last function isn't complete yet but i think it doesn't affect it (?) (the program worked fine before i added the void accountPage() tho) *i am not supposed to use pointers plus i'm very new to c++

the code is a bit long but its just a lot of simple functions, i would rly appreciate it if someone can point out my mistake anywhere

#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>

using namespace std;

//Global Variables
int Choice1;
int mobile, ic;
string user, pass, name, inUser, inPass;

//Function Prototypes
void register_user();
void login();
void bookRoom();
bool CheckCredentials(string, string);
void accountPage();

int main() 
{


    cout << "Welcome to Cozy Homes!\n";
    cout << "Operating hours: 11am - 10pm Tuesday - Sunday\n\n\n\n";

    do {
        cout << "\n1. Register\n";
        cout << "2. Log In\n";
        cout << "3. Exit\n";
        cout << "Please enter a number:";
        cin >> Choice1;

        if (Choice1 == 1)
        {
            register_user(); 
        }

        else if (Choice1 == 2)
        {
            login();
        }

        else if (Choice1 == 3)
        {
            cout << "Exiting now...\n";
            return 0;
        }

        else if (Choice1 < 1 || Choice1 > 3)
        {
            cout << "Please choose a number from the menu!" << endl;
        }

    } while (Choice1 != 3);

    system("pause");
    return 0;
}

//Register page
    void register_user()
    {
        cin.ignore();
        cout << "\n\n\n" << "New Username: ";
        getline(cin, user);
        cout << endl;
        cout << "New Password: ";
        getline(cin, pass);
        cout << endl;
        cout << "Full name: ";
        getline(cin, name);
        cout << endl;
        cout << "Mobile Number: ";
        cin >> mobile;
        cout << endl;
        cout << "Ic Number (without \" - \"): ";
        cin >> ic;
        cout << endl;
        cout << "Registered Successfully!" << endl;
        cout << endl;

        //Store username and password in login file
        ofstream l("login.txt", ios::app);
        if (!l.is_open()) {
            cout << "could not open file \n";
        }


        l << user << " " << pass << endl;
        l << endl;
        l.close();

        //Store other details in customer file
        ofstream c("customer.txt", ios::app);
        if (!c.is_open()) {
            cout << "could not open file \n";
        }

        c << user << endl;
        c << pass << endl;
        c << name << endl;
        c << mobile << endl;
        c <<  ic << endl;
        c << '\n';
    
        c.close();

    }

    //Log in page
    void login() 
    {
        do
        {
            cout << "\nUsername: ";
            cin >> inUser;
            cout << "Password: ";
            cin >> inPass;


            if (CheckCredentials(inUser, inPass) == true)
            {
                cout << "\nLogin sucessful!" << endl;
                cout << "Welcome, " << inUser << endl;
                cout << endl;
                accountPage(); // Redirects user to their account page after successfully logged in
            }
            else
            cout << "\nInvalid username or password. " << endl;

        } while (CheckCredentials(inUser, inPass) != true);
    }
    
    //Validate their username and password
    bool CheckCredentials(string inUser, string inPass)
    {
        string u;
        string p;

        bool status = false;

        ifstream f;
        f.open("login.txt");

        if (!f.is_open())
        {
            cout << "Unable to open file!\n";
        }
        else if (f)
        {
            while (!f.eof())
            {
                f >> u >> p;
                if (inUser == u && inPass == p)
                {
                    status = true;
                }
                else
                {
                    status = false;
                }
            }
        }

        f.close();
        return status;
    }
    
    //Account Page
    void accountPage()
    {
        int Choice2;

        do
        {
            cout << "1. Profile\n";
            cout << "2. Book a Room\n";
            cout << "3. Cancel Booking\n";
            cout << "4. Logout\n";

            cout << "Please enter a number: ";
            cin >> Choice2;

            if (Choice2 == 1)
            {

            }
            else if (Choice2 == 2)
            {
                
            }
            else if (Choice2 == 3)
            {

            }
            else if (Choice2 == 4)
            {
                cout << "Logging out.....\n\n\n\n";
                cout << endl;
            }
        } while (Choice2 != 4);
    }

    //Booking page
    void bookRoom() {
        cout << " ";
    }
        ```

Like this:

void login() 
{
    bool loggedin = false;
    while(!loggedin)
    {
        cout << "\nUsername: ";
        cin >> inUser;
        cout << "Password: ";
        cin >> inPass;


        if (CheckCredentials(inUser, inPass) == true)
        {
            loggedin = true;
            cout << "\nLogin sucessful!" << endl;
            cout << "Welcome, " << inUser << endl;
            cout << endl;
            accountPage(); // Redirects user to their account page after successfully logged in
        }
        else
        cout << "\nInvalid username or password. " << endl;

    } 
}

or like this:

void login() 
{
    bool loggedin = false;
    do 
    {
        cout << "\nUsername: ";
        cin >> inUser;
        cout << "Password: ";
        cin >> inPass;


        if (CheckCredentials(inUser, inPass) == true)
        {
            loggedin = true;
            cout << "\nLogin sucessful!" << endl;
            cout << "Welcome, " << inUser << endl;
            cout << endl;
            accountPage(); // Redirects user to their account page after successfully logged in
        }
        else
        cout << "\nInvalid username or password. " << endl;

    } 
    while(!loggedin)
}

As this is a school assignment I did not bother to test the code myself. It is just meant to get you futher and I did only minimal changes. The point of your error is that the faulty function calls checkcredentials before a user and pasword is entered in the system. If this solves your problem please mark as solution.

    l << user << " " << pass << endl;
    l << endl; <---- creates an empty line in your file. Is this intentional ?
    l.close();

Clearly there is more bugs in your program. But this will get you further. Have to do my own job now;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM