C ++。如何通过在切换案例中再次询问用户名来查找密码?

问题描述 投票:-3回答:1

我创建了一个这样的数据库(这实际上是我的大项目的一部分)。它工作得很好。但我需要我的程序再次询问用户名来查找密码。它在开关盒中。我该怎么做?

class LoginPage
{
private:
public:
bool Login();
void login_intro();
};

bool LoginPage::Login()
{
string username, password, name, pin;
cout << "enter username: ";
cin >> username;
cout << "enter password: ";
cin >> password;
ifstream in("newuser" + username + ".txt");
getline(in, name);
getline(in, pin);


if (name == username&&pin == password)  return true;
else return false;
}

void LoginPage::login_intro()
{
start:
system("cls");
cout << "\t\t\tQUIZLET of IUT\n\n";
cout << "\t\t\t1.Register\n\t\t\t2.Login\n";
int a;
cin >> a;
if (a == 1)
{
reg:
    system("cls");
    string username, password, password1;
    cout << "\nSelect username: "; cin >> username;
    cout << "\nSelect password: "; cin >> password;
    for (int i = 0; i != 50; i++)
    {
        cout << "\nConfirm password: "; cin >> password1;
        if (password != password1) cout << "Passwords do not match!\n";
        else if (password == password1) i = 49;
    }

    ofstream new_user("newuser" + username + ".txt", ios::app);
    new_user << username << endl << password;
    new_user.close();
    system("cls");
    goto start;
}
else if (a == 2)
{
    system("cls");
CHECKPOINT:
    bool status = Login();
    if (!status)
    {
        cout << "\nIncorrect username or password\n";
        cout << "1. Try again\n2. Forgot password?\n3. Don't have an account, register\n";
        asd:
        cin >> a; 
        switch (a)
        {
        case 1:
            goto CHECKPOINT;
            break;
        case 2:
            //I have to do something here
            break;
        case 3:
            goto reg;
            break;
        default:
            cout << "Please enter a proper value\n";
            goto asd;
        }
        _getch(); system("cls");
        goto CHECKPOINT;
    }
    else cout << "You have successfully logged in\n"; 
    Sleep(700);
    system("cls");
}
else if (a != 1 || a != 2)
{
    cout << "Please enter a proper value\n";
    goto start;
}
}
c++ login
1个回答
0
投票

你没有告诉我们你想在交换机上做什么,所以这里有两个可能的答案:

如果要显示密码,请执行相同操作

getline(in, name)
getline(in, pin)

你在Login()和打印针。

如果要编写新密码,请执行与注册相同的操作,但请使用以下命令打开文件

ofstream new_user("newuser" + username + ".txt");

(意思是没有ios :: app)并且只需将用户名和新密码写入其中。

© www.soinside.com 2019 - 2024. All rights reserved.