Username and password code

This is a code I c+p, it works perfectly , is there a way I can make it accept multiple usernames and passwords in any order?


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
#include <iostream>
#include <string>

using namespace std;

int main()
{

	const string USERNAME = "Admin";
	const string PASSWORD = "password";
	string username, password;
	bool user_validated = false;

	do 
	{
		
		cout << "Insert ID:";
		cin >> username;

		if (username.length() < 4)
		{
			cout << "Username length must be atleast 4 characters long.";
		}
		else  
		{
			cout << "Insert ID password:";
			cin >> password;
			
			if (password.length() < 6)
			{
				cout << "Password length must be atleast 6 characters long." << endl;
			}
			else 
			{
			
				if (username == USERNAME && password == PASSWORD)
				{
					cout << "User credentials are correct, Granting access" << endl;
					user_validated = true;
				}
				else
				{
					cout << "Wrong Serial number" << endl;
				}
			}
		}
	} while (user_validated == false);
} 

Last edited on
You should take a look into a data structure called Hash, I think that it might help you.
I suppose you could have say
1
2
3
4
std::map<std::string,std::string> usernameToPassword;

usernameToPassword["Admin"] = "Password";
// and all your other credentials. 


You then input a username and password, as you do.

Then use the map::find() method to see if the username is valid, then compare the stored password with the entered password.

Sorry but I'm kind of a noob, you'd have to simplify it further. Where should I place that? And what do I replace it with? And how about the rest of the code, how would could I even replace cin >> username; or cin >> password; without having to recode the whole structure. And lastly how do I use the ::find() method to see if the username is valid, then comparing the stored password with the entered password?

Last edited on
Hey Nick, hope you are doing well. First of all. To make a program fully accept multiple passwords and users(wether it's for login in or signing up) you can do a few things since there is not an only way to do this... although the best way to do this is arguably by having a database (don't know if you have been trough a data structure or/and database class).

If not, don't worry you can use a few workarounds. Try making and array for usernames and a different array for passwords, then you can workout any restrictions, specifications, validations trough functions.

But please note that I would stay away from this kind of practice for now I where you. It's obviously not the best way.

I you want to store user data, you can also output it to a file using fstream (NOT ADVISABLE int terms of security, don't put passwords in a txt 😂).

For your reference :

https://www.youtube.com/watch?v=4ls9cHMpgvk

https://www.daniweb.com/programming/software-development/threads/270969/c-username-and-password

I can't tell if you want to create the username+password in your program, or just authenticate against a username+password. It looks like you're trying to do both. A login screen would never say "password length must be at least X" when simply trying to login to an existing account.

Anyway, so you want it to accept multiple possible valid username & password combinations. Using an std::map would be an efficient way to search the valid username/password combinations, but isn't necessary if you don't want to use that.

Since you're new, I suppose you could just use a simple array here.

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
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    const int NumUsers = 3;
    
    const string usernames[NumUsers] = { "Admin", "User", "Test" };
    const string passwords[NumUsers] = { "password", "hello123", "test123" }; // these are terrible passwords, please avoid IRL.

	string entered_username, entered_password;
	bool user_validated = false;

	do 
	{	
		cout << "Insert ID: ";
		cin >> entered_username;
		
		cout << "Insert Password: ";
		cin >> entered_password;
		
		for (int i = 0; i < NumUsers; i++)
		{
		    if (entered_username == usernames[i] && entered_password == passwords[i])
		    {
		        cout << "Welcome " << entered_username << '\n';
		        user_validated = true;
		        break;
		    }
		}
		
		if (!user_validated)
		{
		    cout << "Username or password is incorrect\n";
		}
		
	} while (!user_validated);
}


Note that in real life, you should never just store hardcoded passwords like that. They should be salted and hashed using cryptologically secure algorithms, among other best practices, but that's beyond what I feel is necessary to explain here.

https://en.wikipedia.org/wiki/Cryptographic_hash_function
Last edited on
Thanks, for all the replies. This helped a lot thanks for being patient and showing me the steps. But whats the problem of hardcoded passwords?
Stored passwords are the lowest fruit on the tree.
1
2
3
4
5
6
7
8
9
10
$ g++ bar.cpp
$ strings a.out
Admin
password
Insert ID:
Username length must be atleast 4 characters long.
Insert ID password:
Password length must be atleast 6 characters long.
User credentials are correct, Granting access
Wrong Serial number

But whats the problem of hardcoded passwords?


You can look at the binary and just read the passwords straight out of them.

Here's an extract from running the common utility "strings" on your program (not the source code - the executable):

AWAVI
AUATL
[]A\A]A^A_
Admin
password
Insert ID:
Username length must be atleast 4 characters long.
Insert ID password:
Password length must be atleast 6 characters long.
User credentials are correct, Granting access
Wrong Serial number
;*3$"
zPLR

There's the username and the password right there, ready for anyone to read.

Last edited on
Registered users can post here. Sign in or register to post.