Password program

Feb 7, 2013 at 1:14pm
I'm having my problems with my password program, it doesn't recognise the word for some reason

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
//-------------------------------
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//-------------------------------
void main()
{
	
char password[] = "";
char correctpassword[] = "family";
cout << "Enter Password";
cin >> password;
cout << "You typed " << password;
system("pause>nul");
if(password == correctpassword)
{
	cout << endl << "Correct Password";
}
else
{
while(password != correctpassword)
{
	cout << endl << "Incorrect Password";
	system("pause>nul");
	cout << endl << "Please type it again or type exit to exit: ";
cin >> password;
if(password == correctpassword)
{
	MessageBox(NULL, "Hello", "Test", MB_OK);
}
else if(password == "exit")
exit (EXIT_SUCCESS);
}
}
}


Please help, I want it to recognise the word, it keeps saying incorrect password even though its correct
Feb 7, 2013 at 1:35pm
This declaration

char password[] = "";

defines an array that can contain only one characater.

The other problem is the statement

if(password == correctpassword)

Here two pointers are compared and the result is always equal to true because these pointers contain different addresses. You should use standard C function strcmp to compare two character arrays.
Feb 7, 2013 at 1:45pm
You already have #include <string> , why not just use a std::string to hold the password string entered by the user?

The user can enter any number of characters, and the string will resize itself to accept all of them.
And we can use an == to check if two strings compare equal.

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 <windows.h>
#include <string>

using namespace std;

//void main()
int main()
{
    //char password[] = "";
    string password ;
    //char correctpassword[] = "family";
    const char correctpassword[] = "family";

    cout << "Enter Password";
    cin >> password;
    cout << "You typed " << password;

    //system("pause>nul");
    if(password == correctpassword)
    {
        cout << endl << "Correct Password";
    }
    else
    {
        while(password != correctpassword)
        {
            cout << endl << "Incorrect Password";
            //system("pause>nul");
            cout << endl << "Please type it again or type exit to exit: ";
            cin >> password;
            if(password == correctpassword)
            {
                MessageBox(NULL, "Hello", "Test", MB_OK);
            }
            else if(password == "exit")
                exit (EXIT_SUCCESS);
        }
    }
}
Last edited on Feb 7, 2013 at 1:46pm
Feb 8, 2013 at 9:41am
Thanks a bunch, really appericiate the help
Topic archived. No new replies allowed.