【问题标题】:(C++) Read username and password from a file output problem(C++) 从文件输出问题中读取用户名和密码
【发布时间】:2021-04-01 06:23:50
【问题描述】:

我的注册登录程序在输出过程中遇到了一些问题:

  1. 我必须先注册一个新用户并通过(即使我已经将以前的用户名和密码存储在我试图从中检索它的文本文件中),之后只有我可以使用以前的用户名和密码登录,并且重复关闭调试窗口后重新开始调试(如果我在运行程序时直接选择登录,会输出“无效的用户名或密码”)

  2. 当从新注册的用户名注销时,程序跳转到 int main() 并显示“1. 注册....” 但是从以前的用户名中注销,它会跳转到 void login() 并显示“用户名:”

*注意:最后一个功能尚未完成,但我认为它不会影响它(?)(在我添加void accountPage()tho 之前程序运行良好) *我不应该使用指针,而且我对 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 << " ";
    }
        ```

【问题讨论】:

  • 注册函数中省略了什么?当您写入文件时,您没有用户值并通过。我必须说,奇怪的练习
  • @PhilipStuyck 省略号还在吗?我现在编辑了代码,你能看到添加的代码吗?
  • 在循环中第一个while循环是错误的。那时,如果您从一个新程序开始,inuser 和 inpass 是空的。你需要一个 do while 循环。
  • do { cout &lt;&lt; "\nUsername: "; cin &gt;&gt; inUser; cout &lt;&lt; "Password: "; cin &gt;&gt; inPass; if (CheckCredentials(inUser, inPass) == true) { cout &lt;&lt; "\nLogin sucessful!" &lt;&lt; endl; cout &lt;&lt; "Welcome, " &lt;&lt; inUser &lt;&lt; endl; cout &lt;&lt; endl; accountPage(); // Redirects user to their account page after successfully logged in } else cout &lt;&lt; "\nInvalid username or password. " &lt;&lt; endl; } while (CheckCredentials(inUser, inPass) != true); 喜欢这样吗?因为我试过这个,但我遇到了同样的问题
  • 我不会读那个。但还是错了

标签: c++ registration


【解决方案1】:

像这样:

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

由于这是一项学校作业,我没有费心自己测试代码。 这只是为了让你更进一步,我只做了很小的改变。 您的错误点在于,在系统中输入用户和密码之前,错误的函数会调用 checkcredentials 。 如果这解决了您的问题,请标记为解决方案。

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

显然,您的程序中存在更多错误。但这会让你走得更远。 现在必须做我自己的工作;-)

【讨论】:

  • 我试过了,只能读取和验证最后一个用户名和密码。我的void CheckCredentials() 有问题吗?
  • 看起来像。我认为注册功能甚至存在错误。许多人都有尽头。在编辑器中打开文件。并使用调试器或使用更多 cout 语句,在其中输出显示您在哪里以及某些变量的值是什么的内容。在检查凭证中执行此操作以找到您的问题。
  • 最好在 readcredentials 中读取整行。并进行一些解析以获取用户和密码。不确定您的代码对 endl 做了什么。
  • 好的,我先排查问题,谢谢推送
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2018-01-10
  • 2019-06-18
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
相关资源
最近更新 更多