Comparing two strings with If statement

Hi, when I enter "oo" I get the "Wrong Password!!! output.. I need to get the "correct Password" output! I would think that this code would work but it doesn't!!!
void loginScreen()
{
char x [] = "oo";
char loginA[256];

std::cout<< "Login: ";
std::cin >> loginA;
std::cin.ignore();

if (loginA == x)
{
std::cout << "Correct password!
std::cin.get();
}
else
{
std::cout << "Wrong Password!!!";
std::cin.get();}

}
Last edited on
The problem is in that line:
 
if (loginA == x)


in C++ we use strcmp() to compare two C-style strings, instead of syntax, like in your code.
Check here: http://cplusplus.com/reference/clibrary/cstring/strcmp/

And what you did above? You compared two values of char pointers (correct me if I'm wrong)

You code should look like this:
1
2
3
//...
if (strcmp(x,loginA)==0)
//... 





Last edited on
Thx alot!! it woked :D
Yes, it should work, but I forgot an important thing:
You still can use syntax like this:
if (loginA == x)
since Cstring and std::string class have overloaded comparision operators. This will lead to more natural syntax:
1
2
3
4
5
6
7
#include <string>
//...
std::string x = "oo";
std::string loginA;
//...
if (loginA == x)
//... 


Topic archived. No new replies allowed.