-
problem...
ok, i got to thinking, why not make a fake login screen for fun, since i was looking at a lesson in my book (not school) and it was talking about preprocessor directives, and it just so happens it was showing masking of characters. I made a login screen that asks for a user, then if the user checks out, then it asks for a password, but when i compile the program, it says "no matching function for call to `strcmp(std::string&, const char[7])' "
here is some of the code, i know something is wrong in the highlighted part, but i cant depict the problem...
Code:
string Pass() {
string numAsString = " ";
char ch = getch();
while ( ch != '\r' ) { //\r is the enter key
cout << '*';
numAsString += ch;
ch = getch();
}
return numAsString;
}
int main()
{
char user[50];
cout << "Plese Enter Your Username: ";
cin.get( user, 50 );
if ( strcmp ( user, "Warhawk" ) == 0 ) {
cout << endl << "Enter Password: ";
string pass1 = Pass();
if ( strcmp ( pass1, "password" ) == 0 ) {
system("cls");
cout << "Welcome!" << endl;
-
> if ( strcmp ( pass1, "password" ) == 0 ) {
How about the much simpler:
Code:
if ( pass1 == "password" ) ) {
-
it still dont work, and i thought for a string to be classified as true, you had to use if(strcmp (pass1, "password") == 0)
-
>it still dont work
What's the error?
-
A C style string (like char user[50] in your code) is different from a C++ string (like string pass1 in your code). A C style string requires strcmp because it is just a pointer and == will just compare the pointers. A C++ string works with == because it has overloaded the == operator to work correctly.
So use == when you use C++ strings, and strcmp with C style strings. In fact, why not use only C++ strings?
Also, there was a typo (an extra closing parenthesis) in swoopy's suggestion, it should be:
Code:
if ( pass1 == "password" ) {
-
then why doesnt it work? i tried that and it still goes to the part of password incorrect. Does it have to do with the preprocessor? i tried it with a similar program that show what you typed in the asteriks, and it too when told if string == password, does not work. It ignores it even if the statement is true or false.
-
>then why doesnt it work?
Because there's a bug in your Pass() function? :confused:
-
-
>how would i fix this
> string numAsString = " ";
This is initializing your string with one space. What you really want is to initialize it with an empty string. Now you can do this with either:
Code:
string numAsString("");
Or this:
Code:
string numAsString;
-
still no luck -- error message reads: "no matching function for call to `strcmp(std::string&, const char[7])' "
-
> "no matching function for call to `strcmp(std::string&, const char[7])' "
Either
- Make both variables a String, and use == to compare them.
- Make both variables a char[] and use strcmp() to compare them.
This walking down the middle of the road between C and C++ is just an invitation to become road-kill.
-
if you insist to use strcmp with std:string use
Code:
if ( strcmp ( pass1.c_str(), "password" ) == 0 ) {
Kurt
-
ok, i played around with my code and i got it to work, thanks for all your help guys!