如何通过在C ++中再次询问用户名来查找密码? [英] How can I find password by asking username again in C++?

查看:108
本文介绍了如何通过在C ++中再次询问用户名来查找密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的尝试:



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

bool LoginPage :: Login()
{
string username,password,name,pin;
cout<< 输入用户名: ;
cin>>用户名;
cout<< 输入密码: ;
cin>>密码;
ifstream in(newuser+ username +。txt);
getline(in,name);
getline(in,pin);


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

void LoginPage :: login_intro()
{
start:
system(cls);
cout<< IUT的真实情况;
cout<< \t\t\t1.Register\\\
\t\t\t2.Login\\\
;
int a;
cin>>一个;
if(a == 1)
{
reg:
system(cls);
string username,password,password1;
cout<< \ n选择用户名:; cin>>用户名;
cout<< \ n选择密码:; cin>>密码;
for(int i = 0; i!= 50; i ++)
{
cout<< \ n确认密码:; cin>>密码1;
if(密码!=密码1)cout<< 密码不匹配!\ n;
else if(password == password1)i = 49;
}

ofstream new_user(newuser+ username +。txt,ios :: app);
new_user<<用户名<< endl<<密码;
new_user.close();
system(cls);
转到开始;
}
否则if(a == 2)
{
system(cls);
CHECKPOINT:
bool status = Login();
if(!status)
{
cout<< \ n不正确的用户名或密码\ n;
cout<< 1.再试一次。\\ nn2。忘记密码?\ n3。没有帐号,注册\\n;
asd:
cin>>一个;
开关(a)
{
案例1:
转到CHECKPOINT;
休息;
案例2:
//我必须在这里做点什么
break;
案例3:
goto reg;
休息;
默认值:
cout<< 请输入正确的值\ n;
转到asd;
}
_getch();系统( CLS);
转到CHECKPOINT;
}
其他cout<< 您已成功登录\ n;
睡眠(700);
system(cls);
}
else if(a!= 1 || a!= 2)
{
cout<< 请输入正确的值\ n;
转到开始;
}
}

解决方案

这只是正确编写逻辑的问题。首先,我将创建一个包含所有用户信息(名称,密码,PIN等)的类,并编写方法来接受它们的输入并验证它们。然后退后一步,仔细想想你正在尝试做什么,并使用没有gotos的循环重写代码。 Gotos在C ++中是一个非常糟糕的主意,因为当你跳转到对象的析构函数时,它们会导致各种令人讨厌的问题,比如内存泄漏。您可以使用break并继续控制循环的执行流程。 Break退出循环并继续前进到循环的末尾,这将导致执行for循环的迭代语句。


你应该避免使用goto语句,因为它们是对可读性和程序流程不利。你可以改为调用函数。



我建议做一个循环:

  bool  password =  false ; 

执行 {
// 您的代码
密码= true ; // 成功选中后
} while (密码== false );



当密码不正确时,用户需要再次输入密码。



请查看 Learn C ++ 教程,因为我在您的代码中看到了很多知识差距。


I created a database like this(this is actually one part of my big program). It is working perfectly. But I need my program to find password by asking username again. It is in the switch case.How can I do it? Please help!

What I have tried:

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

解决方案

This is just a issue of properly writing your logic. To begin, I would make a class that contains all of the user's information (name, password, PIN, etc...) and write methods to accept entry of them and to validate them. Then take a step back, think carefully about you are trying to do, and rewrite the code using loops without gotos. Gotos are a really bad idea in C++ because they can cause all kinds of nasty problems like memory leaks when you jump around an object's destructor. You can use break and continue to control the execution flow of the loop. Break exits a loop and continue advances to the end of the loop which causes the iteration statement(s) of a for loop to be executed.


You should avoid the goto statements, because they are bad for the readability and program flow. You can call a function instead.

I would suggest a do loop:

bool password = false;

do {
  //your code
  password = true;//when checked successfully
} while( password == false );


When the password is incorrect the user only need to enter the password again.

Take a look in to the Learn C++ tutorial, because I see in your code a lot of knowledge gap.


这篇关于如何通过在C ++中再次询问用户名来查找密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆