繁体   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