Hi! I am new to c++ and i want to do a login system for a program i am working on.
I want it to use a file. The code below is all I can think of right now. I don't know how i will read individual usernames and password to verify if the one entered by the user is valid so i used the "~" symbol after every user and password and used getline with a delimiter "~" to read the file and cross check. I also want to have the option to try again.
Some syntax errors:
Line 34: denzel2 is undefined. Did you mean user2?
Line 50: denzel is undefined. Did you mean user? This conflicts with user decalred at line 44.
Some logic errors I see:
1) reg() doesn;t check that if the username is already in use.
2) You should declare user2 and pass2 local within reg(). THis way they will be closed on exit from reg(). Not a problem now, but if you were to add a loop in main, you would be attempting to open these files multiple times.
3) Ditto for user and pass1 within login()
4) Your loops at lines 49 and 52 assume there are always exactly 1000 entries in in both files. You should be reading until getlne returns false.
5) Your loop at line 57 assumes exactly 1000 record were read from both files. What are you going to be comparing if your user/password file have only 5 entries when x >= 5?
6) Line 60 is going to be executed for every iteration of the loop. YOu probably meant to include lnes 59-60 in {}.
7) If you have a successful login at line 59, you want to return out of the function immediately. As it is now, you will continue the loop and fall through to lines 63.
With regard to trying again, logon should return a bool. You can then loop in main calling login() until it returns true or max tries is exceeded.
With regard to having a series of data for each user, consider using a struct. Each piece of user data is a member of the struct. reg() is then responsible for adding the struct to your file (you wouldn't need two files). Login() would then read in an array of structs and serach the array for a match on username and password or whatever ele you add to the struct.
Thanks for the help. Yes i mean user2 and user sorry for that. Uhhm i can't think of any solution for the 1000 since i just assume that there are only 1000 or less users registered. Also at line 59 that's my problem. i don't know how to return out of the function. should i make my login() a bool type then return a y or what. i am sorry i'm such a noob. And i'm not yet introduced to structs so if you can link some guides or something it would be nice. Thanks again.