-3

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?

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œur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Because your post is mostly code and doesn't provide enough details. – François Andrieux Apr 23 '18 at 18:39
  • i knew that's why i wrote smth at the end – Dadakhon Ortikov Apr 23 '18 at 18:40
  • 3
    The lack of consistent indentation and of comments, along with the generous use of `goto` make this code harder to read than it has to be. – François Andrieux Apr 23 '18 at 18:40
  • but it is working fine – Dadakhon Ortikov Apr 23 '18 at 18:42
  • 2
    The polite and helpful thing to do when asking a question is to eliminate unneeded code and provide easy to read code to make it easier for the people you are asking help from. Second, I'm not sure what exactly the problem you are facing is since you were able to implement asking the user to provide a user name once already. Third, consider that when writing code the compiler doesn't care about readability but other people (and your future self) do. Write for other programmers, not for the compiler. – François Andrieux Apr 23 '18 at 18:45
  • I just need to reread the database and display and the problem is here 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"; – Dadakhon Ortikov Apr 23 '18 at 18:53
  • 1
    Helpful reading: [Spaghetti code](https://en.wikipedia.org/wiki/Spaghetti_code). Read, understand, and resolve the issues with your code, and odds are very good that adding the functionality you want to your code will become trivial. – user4581301 Apr 23 '18 at 19:22
  • Adding to @user4581301, Just because "it works fine" does not mean it's good code. Using `goto` statements like that is bad practice and will cause you more trouble than the effort you think you saved by using them. You might want to learn from a [good book](https://stackoverflow.com/q/388242/9254539) instead of whatever taught you to use `goto`s like that. Also, fix your indenting. – eesiraed Apr 24 '18 at 03:44

1 Answers1

0

You didn't tell us what you want to do in the switch, so here are 2 possible answers:

If you want to show the password just do the same

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

you do in Login() and print pin.

If you want to write new password do the same thing you do in registration, but open the file with

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

(meaning without the ios::app) and just write the username and the new password into it.

Zv0n
  • 31
  • 5
  • you didn't understand me. In "Forgot password?" section i should ask the user again his username and display his password, coz he already registered and i have his entries in my database.I don't need to assign new password – Dadakhon Ortikov Apr 23 '18 at 19:04
  • Just ask again and open the file "newuser" + user + ".txt", do getline twice and do cout << pin; – Zv0n Apr 23 '18 at 19:26
  • it seems like i can't open a file within switch – Dadakhon Ortikov Apr 27 '18 at 17:16