#include <iostream>
int main()
{
char username[15];
char password[15];
std::cout << "Hello, please login to continue your action.<Max 15 Char>" << std::endl;
std::cout << "Username: ";
std::cin >> username;
std::cout << "Password: ";
std::cin >> password;
if (username == "User" && password == "qwerty")
{
std::cout << "Hello, creator.";
}
else
{
std::cout << "Invalid Login";
}
/*23 row*/ std::cout << std::endl << std::endl << "Username=" <<username << std::endl << "Password=" << password;
std::cout << std::endl << std::endl << "Press Enter to close the window . . . ";
std::cin.clear();
std::cin.sync();
std::cin.get();
}
When i type correct it should say Hello Creator but it only goes to invalid i thinked maybe char stores only 1 char thats why at 23 row i taked look what is stored in char username and pasword but everything is fine. Why then it takes Else {…} sentence?
There are two types of strings in C++. The kind you are using for
usernameandpasswordare old-style C strings. They are basically a sequence of characters in memory, terminated by a special character'\0'. Since they come from old C, you can not use things like comparison or assignment operators on them.To compare two old-style C string you have to use the
strcmpfunction:A better solution is to use the new C++ string class:
std::stringinstead, as it has lot more functionality built in. For example handling comparison.