title: C++ Login Program: How to add Forget Password function
body: <p>I recently created a simple C++ Login Program (check code below). However, I am not fully convinced unless I can solve the following issues I have in mind. I really need somebody's help with the following:</p>
  
  <ol>
  <li><p>If I run the program for the first time, obviously I must not login successfully because there's no existing account, what can I do so that if I choose login and enter a username and password the program will output "Account does not Exist" </p></li>
  <li><p>I also want the program to detect if I entered a wrong username or password to an existing account</p></li>
  <li><p>How can I add forget password function?</p></li>
  </ol>
  
  <p>Thank you so much.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;cstdlib&gt;
  #include&lt;fstream&gt;
  
  using namespace std;
  
  int choice;
  bool cinfail;
  int confirmation;
  string username, password, password2;
  
  void MainMenu();
  
  void writetofile(string username){
       ofstream writefile;
       string file = username+".txt";
       writefile.open(file.c_str());
       writefile &lt;&lt; password;
       writefile.close();
       MainMenu();
  }
  
  void login(){
       system("cls");
       cout&lt;&lt;"Username: "&lt;&lt;endl;
       cin&gt;&gt;username;
       cout&lt;&lt;"Password: "&lt;&lt;endl;
       cin&gt;&gt;password;
  
       if((username == username) &amp;&amp; (password == password2)){
            cout&lt;&lt;"SUCCESSFUL LOGIN!";
       }
       else{
            cout&lt;&lt;"INVALID USERNAME OR PASSWORD!"&lt;&lt;endl;
       }
  }
  
  void RegisterPassword(){
       cout&lt;&lt;"Please enter the password: "&lt;&lt;endl;
       cin&gt;&gt;password;
       cout&lt;&lt;"Please reenter your password: "&lt;&lt;endl;
       cin&gt;&gt;password2;
  
       if(password == password2){
           cin.clear();
           cin.ignore(10000, '\n');
           writetofile(username);
           exit(1);
       }
       else{
            cout&lt;&lt;"Sorry, invalid password. Try again."&lt;&lt;endl;
            RegisterPassword();
       }
       system("cls");
  }
  
  void registerme(){
       system("cls");
       cout&lt;&lt;"REGISTER ACCOUNT"&lt;&lt;endl;
       cout&lt;&lt;"Please enter your username: "&lt;&lt;endl;
       getline(cin, username);
       cout&lt;&lt;"\nUsername - \"" &lt;&lt;username&lt;&lt; "\"\nConfirm? \n\n[1] Yes\n[2] No"&lt;&lt;endl;
       cin&gt;&gt;confirmation;
  
       if(confirmation == 1){
           RegisterPassword();
       }
       else{
           cout&lt;&lt;"Sorry, invalid input. Try again"&lt;&lt;endl;
           cin.clear();
           cin.ignore(10000, '\n');
           registerme();
       }
  }
  
  void exit(){
       exit(0);
  }
  
  void MainMenu(){
       cout&lt;&lt;"SIMPLE LOGIN PROGRAM by RZAM\n[1] Login\n[2] Register\n[3] Exit"&lt;&lt;endl;
       cin&gt;&gt;choice;
  
       do{
            cinfail = cin.fail();
            cin.clear();
            cin.getline(10000,'\n');
  
            }while(cinfail == true);{
                 switch(choice){
                        case 1:
                             login();
                             break;
  
                        case 2:
                             registerme();
                             break;
  
                        case 3:
                             exit();
                 }
            }
  }
  
  main(){
  MainMenu();
  }
  </code></pre>