簡體   English   中英

(C++) 從文件中讀取用戶名和密碼 output 問題

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

我的注冊登錄程序在output期間遇到了一些問題:

  1. 我必須先注冊一個新用戶並通過(即使我已經將以前的用戶名和密碼存儲在我試圖從中檢索的文本文件中),之后只有我可以使用以前的用戶名和密碼登錄,並且在我關閉后重復此操作調試 window 並再次開始調試(如果我在運行程序時直接選擇登錄,它將 output "invalid username or password")

  2. 當從新注冊的用戶名注銷時,程序跳轉到 int main() 並顯示“1. Register ....”但從以前的用戶名注銷時,它跳轉到 void login() 並顯示“用戶名:”

*注意:最后一個 function 尚未完成,但我認為它不會影響它(?)(在我添加void accountPage()之前程序運行良好)*我不應該使用指針加上我c++ 非常新

代碼有點長,但它只是很多簡單的功能,如果有人能在任何地方指出我的錯誤,我將不勝感激

#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 << " ";
    }
        ```

像這樣:

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;

    } 
}

或像這樣:

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

由於這是一項學校作業,我沒有費心自己測試代碼。 這只是為了讓你更進一步,我只做了很小的改變。 您的錯誤點是有故障的 function 在系統中輸入用戶和密碼之前調用 checkcredentials 。 如果這解決了您的問題,請標記為解決方案。

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

顯然,您的程序中存在更多錯誤。 但這會讓你走得更遠。 現在必須做我自己的工作;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM