Multiple users registration problem

Hi guys please can anyone help me i created this program it is a simple login and registration program in c++ it is working fine but it has a small problem that it only work for 1 user when i register an account and then try to login it works fine but when i create a 2nd account and then try to login the 2nd account it don't login it only login for the 1st account which has been store in a data file it store the all data of the 2nd account like password and username but it doesn't login can anyone help me in this problem.

Here is my 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
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
#include<iostream>
#include<fstream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
	string name, password,inName, inPassword,registerName, registerPassword;
	Menue:
		system("color 1f");
		system("cls");
		string select;
		cout<<"Enter 1 for Create account\n";
		cout<<"\nEnter 2  for Login account\n";
		cout<<"\nEnter 3  for Exite\n\n";
		cin>>select;
		if(select=="1")
		{
			system("cls");
			system("color 2f");
			cout<<"\t\t\t\t\t\tCreate Account";
			std::ofstream create_account;
        	create_account.open("data.txt",ios::out|std::ios::app);
            cout<<"\n\n\n"<<"New Username: "; 
			cin>>registerName;
            cout<<"\nNew Password: ";
            cin>>registerPassword;
            create_account<<"Name is         "<<registerName<<'\n';
            create_account<<"password is\t"<<registerPassword<<'\n'<<'\n';
			create_account.close();
			system("cls");
			system("color 3f");
			cout<<"\t\t\t\t\t\tAccount Created";
			Sleep(5*400);
			goto Menue;
		}
		else
		{
			if(select=="2")
			{
				Login:
					system("cls");
					system("color 4f");
					cout<<"\t\t\t\t\t\tLogin";
					std::ifstream Login;
        			Login.open("data.txt",ios::out|std::ios::app);
            		Login.ignore(16,'\n');
					getline(Login,name, '\n');
					Login.ignore(11,'\t');
            		getline(Login,password, '\n');
            		Login.close();
            		cout<<"\n\n\n"<<"Enter Username: ";
            		cin>> inName;
            		cout<<"Enter Password: ";
            		cin>> inPassword;
            		if (inName==name && inPassword==password)
            		{
            			system("cls");
						system("color 5f");
                		cout<<"\t\t\t\t\t\tLogin Successful\n"<<"\n\n"<<"Welcome, "<<inName<<"\n";
                		system("pause");
                		goto Menue;
            		}
            		else
            		{
            			system("color 6f");
						system("cls");
						cout<<"\t\t\t\t\t\tincorrect name or password\n";
						Sleep(5*400);
						goto Login;
					}
			}
			else
			{
				if(select=="3")
				{
					system("color 8f");
					system("cls");
					cout<<"\n\n\n\n\n\n\n\t\t\t\t\t\tGood Bye";
					Sleep(5*400);
					return 0;
				}
				else
				{
					system("color 9f");
					system("cls");
					cout<<"\t\t\t\t\t\tInvalid Selection";
					Sleep(5*400);
					goto Menue;
				}
			}
		}
}
Last edited on
Hello adeel zamann,

While I take a look at your program maybe you could work on this:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Hello adeel zamann,

Here is your code with code tags and some blank lines:
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <fstream>
#include <limits>  // <--- Added.
#include <string>  // <--- Added.
#include <windows.h>
#undef min
#undef max
#include <cstdlib>  // <--- Changed.

using namespace std;

int main()
{
    constexpr int SLEEP_TIME{ 2000 };  // <--- Added.

    string name, password, inName, inPassword, registerName, registerPassword;

Menue:

    system("color 1f");
    system("cls");

    string select;  // <--- Should be an "int" or a "char", but not a "string".

    //cout << "Enter 1 for Create account\n";
    //cout << "\nEnter 2 for Login account\n";
    //cout << "\nEnter 3 for Exite\n\n";
    cout << "\n"
        " 1. for Create account\n"
        " 2. for Login account\n"
        " 3. for Exite\n"
        " Enter choice: ";
    cin >> select;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    if (select == "1")
    {
        system("color 2f");
        system("cls");

        cout << "\n\t\t\t\t\t\tCreate Account";

        std::ofstream create_account("data.txt", std::ios::app);
        //create_account.open("data.txt", std::ios::app);

        cout << "\n\n\n" << "New Username: ";
        std::getline(cin, registerName);

        cout << "\nNew Password: ";
        std::getline(cin, registerPassword);

        create_account << "Name is " << registerName << '\n';
        create_account << "password is\t" << registerPassword << '\n' << '\n';

        create_account.close();

        system("color 3f");
        system("cls");

        //cout << "\n\t\t\t\t\t\tAccount Created";
        std::cout << "\n\n" << std::string(48, ' ') << "Account Created\n";

        Sleep(SLEEP_TIME);  // <--- Changed.

        goto Menue;
    }
    else
    {
        if (select == "2")
        {

        Login:
            
            system("color 4f");
            system("cls");

            cout << "\t\t\t\t\t\tLogin";

            std::ifstream Login;

            Login.open("data.txt", ios::out | std::ios::app);

            Login.ignore(16, '\n');

            getline(Login, name, '\n');

            Login.ignore(11, '\t');

            getline(Login, password, '\n');

            Login.close();

            cout << "\n\n\n" << "Enter Username: ";
            cin >> inName;

            cout << "Enter Password: ";
            cin >> inPassword;

            if (inName == name && inPassword == password)
            {
                system("color 5f");
                system("cls");

                cout << "\t\t\t\t\t\tLogin Successful\n" << "\n\n" << "Welcome, " << inName << "\n";

                system("pause");

                goto Menue;
            }
            else
            {
                system("color 6f");
                system("cls");

                cout << "\t\t\t\t\t\tincorrect name or password\n";

                Sleep(5 * 400);

                goto Login;
            }
        }
        else
        {
            if (select == "3")
            {
                system("color 8f");
                system("cls");

                cout << "\n\n\n\n\n\n\n\t\t\t\t\t\tGood Bye";

                Sleep(5 * 400);

                return 0;
            }
            else
            {
                system("color 9f");
                system("cls");

                cout << "\t\t\t\t\t\tInvalid Selection";

                Sleep(5 * 400);

                goto Menue;
            }
        }
    }
}

I did this so the line numbers I refer to will match.

The blank lines make the code easier to read and follow. This is just one suggestion. How you choose to implement this is still your choice.

Starting with the "include" files you can see the changes and additions I made.

Your program defines "std::string"s and uses "std::getline" both of which need the header file "string" to work. Also some of the "cout" statements will also need the "string" header file to properly output the "std::string"s

"stdlib.h" is a C header file and it would be better to use the C++ version of "cstdlib".

Go to the top left of the page and under the "C++" box either click on the "Reference" link or open it in another tab. There it starts with all the C header files that have a C++ counterpart that you should use.

When you have a program that used the "windows.h" file and the "limits" file yo need to undefine the "min" and "max" macros that "windows.h" defines because "limits" will define "min" and "max" in a different way. I concede that this may not be the best way to do this, but for now it is what I know.

Line 14 works with line 64 and the others, which I have not changed yet, to better implement the delay time rather than using the magic numbers as you have. I am a bit perplexed as to why you used "5 * 400" instead of "2000"?

Lines 28 - 33 are just a different way to do what you did, but it does make it easier to work with and give you an idea of what the output will look like.

When you mix formatted input, as in line 33, with unformatted input of "getline". You need line 35 to clear the input after a formatted input and before an unformatted input with "getline".

In other words formatted input will leave the new line in the input buffer and the unformatted input of "getline" will extract the new line from the input buffer and move on not allowing any input from the key board or a file. To use with a file stream just replace the "cin" with the file stream name.

Line 37 goes back to the comment on line 23. if "select" contains 1 character, (1, 2 or 3), everything is fine, but "select" as a string could contain more than 1 character and the comparison would never be equal. Also you do not need a string for this as you should only be entering 1 character.

Whether you write:
1
2
system("cls");
system("color 5f");

or
1
2
system("color 2f");
system("cls");

It seems more logical to change the colour before you clear the screen. Unless there is a reason for what you did.

The "system" calls should be avoided if possible. 1 not everyone can use them. And 2 it leaves your computer open to attack.

Your code:
1
2
std::ofstream create_account;
create_account.open("data.txt",ios::out|std::ios::app);

Line 1 creates an output stream when you use "ofstream", so in line 2 the "ios::out" is ignored as it is already an output stream. The "ios::app" is fine.

Unless you are new to file streams, most people write just 1 line:
std::ofstream create_account("data.txt", std::ios::app);This not only creates the files stream it open the file at the same time.

Lines 48 and 51 are a better choice the the formatted input you started with.

In lines 53 and 54 I would loose the "Name is " << and "password is\t" << part and just output the registerName and registerPassword. Also you do not nee the extra "\n" on line 54. This will make reading the file easier when the time comes. Your output file should look like:
name
password
name
password
name
password
name
password

When it comes time to reading the file it will be much easier.

If you need the format of:
Name is asdfads
password is	asdfa

Name is asdaadf adgag
password is	adfasd

Then write a function and a different file for this.

Something to be careful of. Using a "\t" at the beginning of a "string"is OK, but when used in the middle of a "string the "\t" may not space the output correctly. This depends on how many characters come before the "\t".

Line 62 is another way to write line 61. There is also the possibility that your tab may be set larger than someone else's tab and the output may not look right.

Line 64 shows how to use the constant variable. This way you only have 1 place to change the variable with having to hunt through the entire program to make several changes.

Andy
Hello adeel zamann,

Sorry I ran out of room before I could mention this.

Try to avoid using the "goto" statements. This is considered bad form and bad programming these days.

The "goto" is an unconditional jump that should be used sparingly, but it still has an occasional use.

It would also help to know if you have studied; functions, structs, vectors, switch and loops? Along with what IDE you are using?

The program could make use of switch and do/while loops. With the do/while loops replacing the need for the "goto" statements.

Andy
Thank you so much for showing interest in my my problem brother i really appreciate you to reply me but I didn't understand what things should i change for the multiple registration may i have to use a while loop of what should i do
Brother if you have a time can you just fix this program for me because its still not working for the multiple users.
You can remove the gotos if you arrange your code to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{   
    while (true) {
        int selection = menu();
        if (selection == 1) {
            doCreateAccount();
        }
        else if (selection == 2) {
            doLogin();
        }
        else if (selection == 3) {
            cout<<"Good Bye\n";
            break;
        }
        else {
            cout<<"Invalid Selection\n";
        }
    }
}


Then move the code into the appropriate handlers. I've commented out some sections:
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
void doCreateAccount()
{
//  system("cls");
//  system("color 2f");
    cout<<"\t\t\t\t\t\tCreate Account";

    string registerName, registerPassword;
    cout << "\n\n\n"<<"New Username: ";
    cin >> registerName;
    cout << "\nNew Password: ";
    cin >> registerPassword;

    std::ofstream create_account("data.txt", ios::out|std::ios::app);
    create_account <<"Name is        " << registerName << '\n';
    create_account <<"password is\t" << registerPassword << '\n'<<'\n';

//  system("cls");
//  system("color 3f");
    cout<<"\t\t\t\t\t\tAccount Created";
//  Sleep(5*400);
}

void doLogin()
{
//  system("cls");
//  system("color 4f");
    cout<<"\t\t\t\t\t\tLogin";

    string name, password;
    std::ifstream Login("data.txt");
//  Login.ignore(16,'\n');
    std::getline(Login, name);
//  Login.ignore(11,'\t');
    getline(Login, password);

    string inName, inPassword;
    cout << "\n\n\n"<<"Enter Username: ";
    cin >> inName;
    cout << "Enter Password: ";
    cin >> inPassword;

    if (inName == name && inPassword == password)
    {
//      system("cls");
//      system("color 5f");
        cout<<"\t\t\t\t\t\tLogin Successful\n"<<"\n\n"<<"Welcome, "<<inName<<"\n";
//      system("pause");
    }
    else
    {
//      system("color 6f");
//      system("cls");
        cout<<"\t\t\t\t\t\tincorrect name or password\n";
//      Sleep(5*400);
    }
}
But for multiple registration what should i do?

doLogin() can just searches the file.

Alternatively, we can get rid of the file from the code and use an object to do all that. Notice how your top level code is becoming simpler and clearer even though it's doing more. BTW, I've not run any of this, consider any bugs as an exercise.
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
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <fstream>
//#include<windows.h>
#include <stdlib.h>
#include <map>
using namespace std;

class PasswordDb
{
	std::map<std::string, std::string> passwords; // user -> password

public:
	PasswordDb() {
		std::ifstream is("passwd.txt");
		std::string user, password;
		while (std::getline(is, user) && std::getline(is, password))
			passwords.insert(std::make_pair(user, password));
	}

	bool addUserAndPassword(const std::string& user, const std::string& password) {
		auto ret = passwords.insert(std::make_pair(user, password));
		if (ret.second) {
			std::ofstream os("passwd.txt", std::ios::app);
			os << user << "\n" << password << "\n";
		}
		return ret.second;
	}

	bool verifyUserAndPassword(const std::string& user, const std::string& password) const {
		auto p = passwords.find(user);  // you also need to check the passwords match
		return p != passwords.end();
	}
};

int menu();
void doCreateAccount(PasswordDb& db);
void doLogin(const PasswordDb& db);

int main()
{
	PasswordDb db;

	while (true) {
		int selection = menu();
		if (selection == 1) {
			doCreateAccount(db);
		}
		else if (selection == 2) {
			doLogin(db);
		}
		else if (selection == 3) {
			cout<<"\n\n\n\n\n\n\n\t\t\t\t\t\tGood Bye";
			break;
		}
		else {
			cout<<"\t\t\t\t\t\tInvalid Selection";
		}
	}
}

int menu()
{
	system("color 1f");
	system("cls");

	cout<<"Enter 1 for Create account\n";
	cout<<"Enter 2 for Login account\n";
	cout<<"Enter 3 for Exite\n\n";

	string select;
	cin >> select;
	return atoi(select.c_str());
}

void doCreateAccount(PasswordDb& db)
{
//	system("cls");
//	system("color 2f");
	cout<<"\t\t\t\t\t\tCreate Account";

	string registerName, registerPassword;
	cout << "\n\n\n"<<"New Username: ";
	cin >> registerName;
	cout << "\nNew Password: ";
	cin >> registerPassword;

//	system("cls");
//	system("color 3f");
	if (db.addUserAndPassword(registerName, registerPassword)) {
		cout<<"\t\t\t\t\t\tAccount Created";
	}
	else {
		cout<<"\t\t\t\t\t\tAccount Creation Failed";
	}

//	Sleep(5*400);
}

void doLogin(const PasswordDb& db)
{
//	system("cls");
//	system("color 4f");
	cout<<"\t\t\t\t\t\tLogin";

	string inName, inPassword;
	cout << "\n\n\n"<<"Enter Username: ";
	cin >> inName;
	cout << "Enter Password: ";
	cin >> inPassword;

	bool matched = db.verifyUserAndPassword(inName, inPassword);
	if (matched)
	{
//		system("cls");
//		system("color 5f");
		cout<<"\t\t\t\t\t\tLogin Successful\n"<<"\n\n"<<"Welcome, "<<inName<<"\n";
//		system("pause");
	}
	else
	{
//		system("color 6f");
//		system("cls");
		cout<<"\t\t\t\t\t\tincorrect name or password\n";
//		Sleep(5*400);
	}
}
Last edited on
Your program has many errors
Guy please if some can do for me only those one comment otherwise don't comment here
I took your code and refactored it. So it's basically your code. I also commented on the password check that needs some work.

This isn't a homework service, and I'm not obliged to produce tested code for you.

The code is supposed to help you learn. If you have specific questions that will help your understanding, I'll spend as much time as you need. Otherwise, forget it.
Actually it is my homework and I have to submit it on tomorrow that's why I urgently need this
What are you allowed to use?
vector, array map...?
Anyone of them
It is actually that "write a program in which user can login and create accounts and the program is for multiple users as many users can create accounts in them and can login."
So please if anyone can help me
With the password check fixed, the code works. I'm not sure what you mean by the code has errors.
Topic archived. No new replies allowed.