I've been trying to set up a kind of password confirmation code for fun. Containing strings and conditions. And I can only imagine this has 1001 errors that you may see that I cannot. Just give me any suggestions:
It is a bit overcomplicated. This string correct = password; doesn't even compile because password is a char and not a string. I don't think you need any of a, b and c. You know that your password can only be one char long, right?
This is a simple one using getline() function instead of cin.
You can improve it by giving the user a limited number of times to insert their password.
I don't know what you were trying to do there declaring a, b and c characters...
Thanks, Wisely Done! That's exactly what I was looking for. Sorry if my idea was crazily put together. I didn't even know about "getline()", "if", or "else." Thanks everybody!
Passwords are often case-sensitive because it adds security. For a real application it is probably not so good to show the password when the user types it. There is no standard C++ way to hide the password but you can use system specific functions to do it. This works on Linux:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include <unistd.h>
int main()
{
usingnamespace std;
string password = getpass("Please enter a password: ");
if (password == "ThisIsTheCorrectPassword")
cout << "Correct Password" << endl;
else
cout << "Incorrect Password!" << endl;
}