-1

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:

  1. 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"

  2. I also want the program to detect if I entered a wrong username or password to an existing account

  3. How can I add forget password function?

Thank you so much.

#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>

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 << password;
     writefile.close();
     MainMenu();
}

void login(){
     system("cls");
     cout<<"Username: "<<endl;
     cin>>username;
     cout<<"Password: "<<endl;
     cin>>password;

     if((username == username) && (password == password2)){
          cout<<"SUCCESSFUL LOGIN!";
     }
     else{
          cout<<"INVALID USERNAME OR PASSWORD!"<<endl;
     }
}

void RegisterPassword(){
     cout<<"Please enter the password: "<<endl;
     cin>>password;
     cout<<"Please reenter your password: "<<endl;
     cin>>password2;

     if(password == password2){
         cin.clear();
         cin.ignore(10000, '\n');
         writetofile(username);
         exit(1);
     }
     else{
          cout<<"Sorry, invalid password. Try again."<<endl;
          RegisterPassword();
     }
     system("cls");
}

void registerme(){
     system("cls");
     cout<<"REGISTER ACCOUNT"<<endl;
     cout<<"Please enter your username: "<<endl;
     getline(cin, username);
     cout<<"\nUsername - \"" <<username<< "\"\nConfirm? \n\n[1] Yes\n[2] No"<<endl;
     cin>>confirmation;

     if(confirmation == 1){
         RegisterPassword();
     }
     else{
         cout<<"Sorry, invalid input. Try again"<<endl;
         cin.clear();
         cin.ignore(10000, '\n');
         registerme();
     }
}

void exit(){
     exit(0);
}

void MainMenu(){
     cout<<"SIMPLE LOGIN PROGRAM by RZAM\n[1] Login\n[2] Register\n[3] Exit"<<endl;
     cin>>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();
}
EmDroid
  • 5,918
  • 18
  • 18
Verax
  • 1
  • 1
  • 2
  • 1. Is this a homework problem? I remember doing something like this is school 2. You would need to store username and passwords. Maybe in the text file. When registering user, add it 3. When logging in, you need to lookup the stored username, passwords from file – user1451348 Nov 20 '18 at 16:34
  • 4
    These aren't C++ questions. They are design questions. The answers are up to you. It is like asking, "How what do I paint a painting of?" Well, some people paint happy trees, others paint rectangles, some just get monkeys to throw paint at a canvas. They are all paintings, technically. The question is open ended and is really, "What are some common mechanisms for authentication?", and that warrants an entire book. – Christopher Pisz Nov 20 '18 at 16:35
  • If the question is, "How do I parse key/value pairs from a file?", that is a different question. Another question might be "How do I add/remove an element from a key/value collection?" and you might have a third in there somewhere related to searching efficiently. – Christopher Pisz Nov 20 '18 at 16:38
  • If you register enough users, you *will* encounter something this site is named after: stack overflow! `MainMenu` calling `registerme` calling [two more] calling `MainMenu`. – Aconcagua Nov 20 '18 at 16:47
  • Hi @user1451348 good day! actually this is not a homework, I am new to programming and I'm using C++ language. This is for my own programming collection :) – Verax Nov 20 '18 at 16:50
  • thanks @ChristopherPisz , but as of now I can't really understand what parse or key/value you're referring to, I started coding for a week now, all I want is my code to solve my 3 problems :) – Verax Nov 20 '18 at 16:52
  • @Verax OK, getting back to stack overflow: Each function call requires a bit of space on stack of the thread the call occured on, for parameters passed, local variables and the return value (in some special cases, these can be reused, keyword [tail call optimisation](https://stackoverflow.com/questions/310974/what-is-tail-call-optimization)), and as you do recursion (indirectly), you will slowly consume up your stack - until none is left any more (the point the stack overflow occurs). So (unless implementing an explicitly recursive algorithm) get rid of these recursions and use loops instead. – Aconcagua Nov 20 '18 at 16:57
  • @Aconcagua wow, that was very informative, thanks man! – Verax Nov 20 '18 at 17:02
  • @Verax Don't be mad with me if this sounds harsh - but there's too much going wrong within your code to write a single answer for. I personally recommend starting over right from the start, going step by step, implementing one function at a time and not go on until you've finished *and tested* the previous one. It is a bit early for file IO while you struggle yet with the basics. I recommend a top down approach: start with main function, introducing a loop: `int main() { for(;;) { mainMenu(); } }`. First function most likely would be user registration, as you cannot do anything without users. – Aconcagua Nov 20 '18 at 17:10
  • Instead of dealing with file IO, start over by just storing user data in a [`std::map`](https://en.cppreference.com/w/cpp/container/map), that will be much easier. For next functions, you might prefill the map with some dummy data you can operate on (before entering the loop in main function), so you don't have to enter new data all the time. Try login function next. Forgetting password? Well, you probably rather mean: *forgotten* password. That will be a bit trickier. If user has forgotten password, he needs to receive it or a new one by some means. – Aconcagua Nov 20 '18 at 17:15
  • In a very first go, I'd just let the user enter his name and then output the pasword to console. Sure, *absolutely none* of security. But it's a first go. Then consider: How would you address him/her in reality? You need additional information, such as e-mail address. Then if user selects "forgot PW", you just could send it to the e-mail address. Or generate a new one. – Aconcagua Nov 20 '18 at 17:18
  • Now (very) long story, short conclusion: Everything step by step. If you encounter new problems (and you *will*...) - these will be more concrete ones - feel free to ask new questions here! – Aconcagua Nov 20 '18 at 17:21
  • Final tip: Don't use global variables if you don't really need them. If you follow my tips above, *at most* the map should be global, anything else you'll be able to cover with local variables. – Aconcagua Nov 20 '18 at 17:26

1 Answers1

-2

For any forgotten passwords we would usually send an email asking if it's a legitimate request. You can't do that, maybe save the users date of birth when they login or a secret question. They can use this to enter a new password.

For detecting the wrong password to an existing account:

  1. Read all usernames into an array
  2. Check the entered username against all of them
  3. If the username exists, and the password is wrong you can alert the user.
Reece Ward
  • 213
  • 2
  • 9
  • Hello @Reece Ward thank you for the answer, however I don't quite understand your 2 and 3. Could you elaborate or provide snippets maybe? I would really appreciate it. Thanks! – Verax Nov 20 '18 at 16:55