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?
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 😂).
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.
#include <iostream>
#include <string>
int main()
{
usingnamespace std;
constint 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.
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
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.