Password Confirmation Code

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:
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
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int main()
{
    char password;
    char mypassword;
    char a;
    char b;
    char c;

    string correct = password;
    string incorrect = "\n\nInvalid password.";

    cout << "Please enter your password:\n" << endl;
    cin >> mypassword;

    a = correct;
    b = mypassword;
    c = (a==b) ? correct : incorrect;

    cout << c;

    return 0;
}


What do you think? And does this seem like an efficient way to do password confirmations?
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    cout << "Please enter a password: " << endl;
    string password;
    getline (cin, password);

    if (password == "ThisIsTheCorrectPassword")
    cout << "Correct Password" << endl;
    else
    cout << "Incorrect Password!" << endl;
}


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...
string is not a char. So this won't work: c = (a==b) ? correct : incorrect; c is char, correct, incorrect are strings.

Your password is supposed to contain only 1 char?

You also never initialize password (arbitrary garbage).

You don't really need a, b. Just use correct and mypassword instead.

Also are you sure you want to use conio.h? It's old I think.

Why don't give up chars and use string instead. == won't work for string either. Use compare instead.
1
2
3
4
string str1, str2;
...//give values to strings
if(!str1.compare(str2))
...


http://www.cplusplus.com/reference/string/string/compare/
== Only works for primitive data types. String, is an object data type so you will need to use string::compare.
What are you saying? == works for std::string.
I don't think so. Itll compile and run but as far as I know it will always read false because they are just two different objects
Oh well never mind it does work. Doesn't work for c style strings, but std::string uses an operator overload on == so it will work in c++
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!
Also, do you want your password to be case-sensitive? If so, your fine as is. If not, you gotta add a couple lines.
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()
{
	using namespace std;
	
	string password = getpass("Please enter a password: ");
	
	if (password == "ThisIsTheCorrectPassword")
		cout << "Correct Password" << endl;
	else
		cout << "Incorrect Password!" << endl;
}
Topic archived. No new replies allowed.