Set multiple usernames and passwords in a file?

Aug 14, 2013 at 11:02am
Hi everyone, I want to make a program that asks you to set a username and password (save it into a file) and after that, each time that the program is opened ask you to provide the username and password. The main problem is to handle unlimited (at least more than 5) users at a time(in a single file).
Aug 14, 2013 at 11:17am
Here a code:
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string user, pass, passConf, name;
void LogIn(), Register();

void LogIn()
{
 	 cout << "Username: ";
 	 cin >> user;
 	 cout << "Password: ";
 	 cin >> pass;
 	 if (/*I REALLY DONT KNOW WHAT TO DO HERE*/)
 	 {
		 //code
	 }
}

void Register()
{
 	 cout << "Set your Username: ";
 	 cin >> user;
 	 cout << "Set your Password: ";
 	 cin >> pass;
 	 cout << "Confirm your Password: ";
 	 cin >> passConf;
 	 cout << "Enter your name: ";
 	 cin >> name;
 	 if (pass == passConf)
 	 {
		 //code
	 }
}

int main(int argc, char *argv[])
{
 	int req;
 	cout << "Please log in or register to acess this application:\n1.Log in\n2.Register"
 	cin >> req;
 	switch()
 	{
		case 1:
			 LogIn();
			 break;
	    case 2:
			 Register();
			 break;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Aug 14, 2013 at 2:10pm
I'd suggest that at the beginning of the program you open the fstream, read user, pass into a map.

in LogIn() you can easily find the user and match against the password.

in Register() you need to check if the user exists (if so you tell the user he can't use that name). If everything is ok write the newly entered values line by line at the end of the file.

look here:

http://www.cplusplus.com/reference/fstream/fstream/?kw=fstream

on how to use the several open flags
Aug 14, 2013 at 2:15pm
in Register() you need to check if the user exists (if so you tell the user he can't use that name). If everything is ok write the newly entered values line by line at the end of the file.
Ok but how to write and read it? How to separate the lines and understand it? That's my problem
Aug 14, 2013 at 5:57pm
closed account (jwkNwA7f)
Write to the file:
1
2
3
4
5
6
7
8
9
ofstream file ("anything.txt", ios::app)
//ios::app add add the end of the file
if(file.is_open())
{
    file << name << endl;
    file << user << endl;
    file << pass << endl;
    file.close();
}
(anything.txt)
name that user entered
username that user entered
password that user entered

Read from file:
1
2
3
4
5
6
7
8
9
10
11
string name, user, pass;
ifstream getfromfile ("anything.txt")
if(file.is_open())
{
    while(getfromfile.good())
    {
          getline(getfromfile, name);
          getline(getfromfile, user);
          getline(getfromfile, pass);
     }
}


Also, you can add the names, etc. to a vector to store all of them once you get them from the file.
Hope this helped!
Aug 14, 2013 at 7:12pm
This might not be the most efficient way, but it does work.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string user;
string pass;

int LoginCheck (string user, string pass)
{
    ifstream file;
    string username, password;
    int n=0;
    file.open("users.txt");
    if (file.is_open())
    {
        while (!file.eof())
        {
            file >> username >> password;
            n++;
            if (user==username && pass==password)
                return n;
        }
    }
    else
    {
        cout << "file not open" << endl;
    }
    return 0;
}

void Register()
{
    ifstream file;
    ofstream newuser;
    string username, password, passwordconfirm;
    file.open("users.txt", ios::app);
    newuser.open("users.txt", ios::app);
    bool uservalid=false;
    while (!uservalid)
    {
        cout << "Username: ";
        cin >> username;
        cout << "Password: ";
        cin >> password;
        cout << "Confirm password: ";
        cin >> passwordconfirm;
        int m=0;
        int k=0;
        while (file >> user >> pass)
        {
            m++;
            if (username!=user)
                k++;
        }
        if (m==k && password==passwordconfirm)
            uservalid=true;
        else if (m!=k)
            cout << "There is already a user with this username." << endl;
        else
            cout << "The passwords given do not match." << endl;
    }
    newuser << username << " " << password << endl;;
    file.close();
    newuser.close();
}

int main()
{
    int loginattempts=0;
    ifstream userfile;
    userfile.open("users.txt");
    string userset, passset;
    if (!userfile.is_open())
    {
        cout << "file not found" << endl;
    }
    else
    {
        cout << "1.Login \n2.Register" << endl;
        int option;
        cin >> option;
        if (option==1)
        {
            while (LoginCheck(user, pass)==0)
            {
                loginattempts++;
                cout << "Username: ";
                cin >> user;
                cout << "Password: ";
                cin >> pass;
                if (LoginCheck(user, pass)!=0)
                    cout << "Welcome " << user << "." << endl;
                else if (loginattempts==3)
                {
                    cout << "Maximum login attempts exceeded." << endl;
                    break;
                }
                else
                {
                    cout << "Invalid username/password combination" << endl;
                }
            }
            userfile.close();
        }
        else if (option==2)
        {
            Register();
        }
    }
    int a;
    cin >> a; // this has no purpose other than stopping the program closing automatically
    return 0;
}


My text file for the users.txt looks like:
user1 pass1
user2 pass2
user3 pass3
user4 pass4
user5 pass5
user6 pass6
user7 pass7
user8 pass8
user9 pass9
user10 pass10
user11 pass11
user12 pass12

Last edited on Aug 14, 2013 at 7:14pm
Aug 15, 2013 at 4:39am
Thanks manudude03 but a little bit of comment would be useful.

Last edited on Aug 15, 2013 at 4:43am
Topic archived. No new replies allowed.