Login system help

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
ifstream user; 
ofstream user2;
ifstream pass1;
ofstream pass2;
void reg();
void login();
int main()
{
	char choice;
	cout<<"Do you have an account ? (y/n): ";
	cin>>choice;
	if (choice=='y')
		login();
	else
		reg();
	
}


void reg()
{
	user2.open("denzel.txt",ios_base::app);
	pass2.open("denzel2.txt",ios_base::app);
	string user,pass;
	cout<<"Enter your user name: ";
	cin>>user;
	cout<<"\n Enter your paswood: ";
	cin>>pass;
	denzel2<<user<<"~";
	pass2<<pass<<"~";
	
	
}
void login()
{
	system("cls");
	user.open("denzel.txt",ios_base::app);
	pass1.open("denzel2.txt",ios_base::app);
	string user,pass,user1[1000],pass2[1000];
	cout<<"Username: " ;
	cin>>user;
	cout<<"Password: ";
	cin>>pass;
	for (int x=0;x<1000;x++){
		getline(denzel,user1[x],'~');
	}
	for (int x=0;x<1000;x++){
		getline(pass1,pass2[x],'~');
	
}

	for (int x=0;x<1000;x++){
		if(user==user1[x] && pass==pass2[x])
			cout<<"Login Successful!";
			system("pause");
	}

cout<<"Wrong username and password";
system("pause");
}


another thing, I want to have a series of data for each user and i don't have any idea how to do so.
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.
Last edited on
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.
Topic archived. No new replies allowed.