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
#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/
#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:
#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.
}
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.
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?
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?
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 usingnamespace 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/