#include <iostream>
#include<string>
usingnamespace std;
////////Expand the password checking program from earlier in this chapter and make it take multiple usernames, each with their own password,
///////and ensure that the right username is used for the right password. Provide the ability to prompt user's again if the first login attempt failed.
///////Think about how easy (or hard) it is to do this for a lot of usernames and passwords.
// User 1: book//keeper
// User 2: bus///boy
// User 3: cat///dog
string userpass(string username, string password)
{
username=="book" && password=="keeper";
username=="bus" && password == "boy";
username=="cat" && password == "dog";
}
int main()
{
string userpass;
string username;
string password;
cout << "Enter your username: " << endl;
getline(cin, username);
cout << "Enter your password \n";
getline(cin, password);
while(userpass != true ) // Why is it giving me an error here?
{
cout << "Wrong username or password, please Enter your username: " << endl;
getline(cin, username);
cout << "Enter your password \n";
getline(cin, password);
}
cout << "Access Granted!" << endl;
cin.get();
}
EDIT:
I dont want multiple while/if statements, I want it as efficient as possible.
1) Line 16, the function has no return statement (also has an incorrect return type)
2) Lines 18, 19, and 20 should be joined by the "||" operator instead of semicolons
2) line 38, 'userpass' is a declared function and should be called as follows: while(userpass(username, password) != true)
For an efficiency boost, you might consider passing by const-reference to your function, as well as perhaps using a std::map for logarithmic complexity.
Also, you have defined a userpass instance within the main function (line 26) which is shadowing your function declaration. You want to make your userpass function return bool, too.
First of all, you'll want userpass to return a bool, not a string, since you're checking whether the username/password combination is valid (true) or invalid (false).
Also, you'll want to actually return something:
1 2 3 4 5 6 7 8 9 10 11 12
bool userpass(string username, string password)
{
// Yes, I know this can be written in a much simpler way, but this is just for illustration purposes
if (username=="book" && password=="keeper")
returntrue;
if (username=="bus" && password == "boy")
returntrue;
if (username=="cat" && password == "dog")
returntrue;
// If we got this far, the username and password are incorrect
returnfalse;
}
Also, in main, when you call userpass, you have to pass the proper arguments to it:
38 39
while(userpass(username, password) != true ) // Why is it giving me an error here?
{ // Because you didn't call it correctly :)
You should also get rid of string userpass; on line 26, since that declaration will shadow the function declaration and make the compiler think you're trying to call a variable instead of a function.
For better efficiency, you can use std::map or std::unordered_map (C++11):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// ...
#include <unordered_map>
const std::unordered_map<std::string, std::string> logins =
{ {"book", "keeper"},
{"bus", "boy"},
{"cat", "dog"} };
bool userpass(std::string username, std::string password)
{
auto iter = logins.find(username);
if (iter == logins.end()) // If we couldn't find the username...
returnfalse; // ...then of course it's wrong
elseif (iter->second != password) // If the username exists but the password is wrong
returnfalse;
else // Otherwise, the username exists and the password for it matches
returntrue;
// (Yes, I know you can combine the first two ifs into one, but whatever)
}
// ...
ty guys for all the help, what are unordered maps?
most of my changes were like luc's, Im pretty much a newb till now and wouldnt mind learning more about what unordered maps do. (Some of the recommendations are way beyond what I currently know/learned, just started coding 2 weeks ago).
Unordered Map
Unordered maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.
In an unordered_map, the key value is generally used to uniquely identify the element, while the mapped value is an object with the content associated to this key. Types of key and mapped value may differ.
Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).
unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.
Unordered maps implement the direct access operator (operator[]) which allows for direct access of the mapped value using its key value as argument.
Iterators in the container are at least forward iterators.