Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
}
Posted
Updated 23-Apr-18 20:20pm
Comments
Patrice T 24-Apr-18 2:49am    
"But I need my program to find password by asking username again."
This make no sense to me, can you explain and give details on context ?
Member 13780562 26-Apr-18 4:02am    
kinda if you forgot your password it should display your password and username. It is like searching

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.
 
Share this answer
 
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:
C++
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900