problem with if/else if

ok so i am almost completely new to c++, and i am just messing around with some code to learn, so here is the problem. it keeps goingto the pass word is incorrect even when you type in the correct password, here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main() {
char password[50];
int x;
x = 1;
while ( x = 1 ){
std::cout<< "enter password ";
std::cin>> password;
if(password == "lolcat"){
	std::cout<< "yay you got the password!!!!!"<<std::endl;
	x = 2;
}
else if (password != "lolcat") {
	std::cout<< "thats not the correct password, you suck!!"<<std::endl;
}
}
system("PAUSE");
    return 0;
    return EXIT_SUCCESS;}

help would be soooo much appreciated :D
closed account (z05DSL3A)
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
#include <iostream>

#include <string.h> // For strcmp();

int main() 
{
    // See later
    char key[]= "lolcat";
    
    char password[50];
    int x;
    x = 1;
    while ( x == 1 ) // while ( x = 1 ) is always true
    {
        std::cout<< "enter password ";
        std::cin>> password;

        // You can not compare a char array with a literal 
        // in this manner: password == "lolcat"
        // use strcmp() to compare to C style strings
        if(strcmp (key, password) == 0)
        {
            std::cout<< "yay you got the password!!!!!"<<std::endl;
            x = 2;
        }
        else // if you have an either/or you don't need another if here 
        {    // If the condition is met it is handled abouve else it is handled here

            std::cout<< "thats not the correct password, you suck!!"<<std::endl;
        }
    }
    system("PAUSE");

    return EXIT_SUCCESS;
}


strcmp() info
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Edit:
fixed while() error.
Last edited on
closed account (S6k9GNh0)
It works with C++ strings. But apparently you can't compare C strings like this. You need to use the strcmp method like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
	char password[10];
	while ( true )
	{
		std::cout<< "enter password ";
		std::cin >> password;

		std::cout << password << std::endl;
		if(!strcmp(password, "bob"))
		{
			std::cout<< "yay you got the password!!!!!"<<std::endl;
			break; //Instead of using x, use this.
		}
		else //Brackets aren't need if there's only one statement used.
			std::cout<< "thats not the correct password, you suck!!"<<std::endl;
	}
	system("PAUSE"); //Generally not a good practice.
    return 0;
    //return EXIT_SUCCESS; //You can only return once.
}


Though you could use C++ strings and accomplish what your wanting like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
	std::string password;
	while ( true )
	{
		std::cout<< "enter password ";
		std::cin >> password;

		std::cout << password << std::endl;
		if(password == "bob")
		{
			std::cout<< "yay you got the password!!!!!"<<std::endl;
			break; //Instead of using x, use this.
		}
		else //Brackets aren't need if there's only one statement used.
			std::cout<< "thats not the correct password, you suck!!"<<std::endl;
	}
	system("PAUSE"); //Generally not a good practice.
    return 0;
    //return EXIT_SUCCESS; //You can only return once.
}

Last edited on
sorry im a noob what is strcmp? lol? edit*
nvm i see your link now lol
Last edited on
closed account (S6k9GNh0)
strcmp in the STD library is a function that compares two C strings. You can get good reference of it here: http://cplusplus.com/reference/clibrary/cstring/strcmp/

From the way you sound, you don't sound like you've read much on C++ or C. I suggest you read an article explaining the differences between C and C++ and read a good C++ based tutorial. For future reference, you don't need to learn C to learn C++. Normally, I'd bust out into lecture but this isn't the place so please take care accordingly.
Last edited on
closed account (z05DSL3A)
lestatx2, I have just notice that you also have while ( x = 1 ) that should be while ( x == 1 ).

PS. the tutorials hear are pretty good.
http://www.cplusplus.com/doc/tutorial/
Last edited on
ok thanks guys, i have it working now ^_^ and i have a better understanding of it now, but i forgot to use string as an include but it still worked, why do you think that is?
closed account (z05DSL3A)
It was probably included via iostream, but you should always explicitly include the headers you need.
how do i include a standard header? i seem to remeber beng able to do that a long time ago when i first tried c++ but now i cant rember how cause it says file cannot be found or something like that
cause i dont remember having to put the std:: thing before any hints?
closed account (S6k9GNh0)
Your in need of a good tutorial. *ACTIVE ARTICLE MODE*:

The standard library is a library of functions that are defined by the C++ standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf

There is a difference between C and C++. In C, not very often did you use functions inside of the std:: namespace and as a result, you would never see it. In C++ all functions defined inside of the STD library and STL are defined INSIDE of the std namespace: http://www.cplusplus.com/doc/tutorial/namespaces/ For your information, C and C++ are similar but not the same. C++ is considered to be a superset of C. C++ is also backwards compatible with C and as a result, EVERY C++ compiler must be able to compile C code as well or else it's not a C++ compiler.

In C++, you need to define that you are using std by either placing using namespace std inside your code or you may specify individual members of the namespace like so: std::cout

EDIT: There is NO Standard Library Header, I totally got ahead of myself and I'm being retarded. The standard library is all of these: http://www.cplusplus.com/reference/

Thank ya' Grey Wolf for the notice.
Last edited on
closed account (z05DSL3A)
In C++, the standard library header is cstdlib.h but

cstdlib.h is the C Standard, General Utilities Library.
Topic archived. No new replies allowed.