problem...

This is a discussion on problem... within the C++ Programming forums, part of the General Programming Boards category; ok, i got to thinking, why not make a fake login screen for fun, since i was looking at a ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    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;

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if ( strcmp ( pass1, "password" ) == 0 ) {
    How about the much simpler:
    Code:
    		if ( pass1 == "password" ) ) {

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    it still dont work, and i thought for a string to be classified as true, you had to use if(strcmp (pass1, "password") == 0)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >it still dont work
    What's the error?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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" ) {

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    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.
    Last edited by Warhawk; 09-16-2005 at 05:14 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >then why doesnt it work?
    Because there's a bug in your Pass() function?

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    how would i fix this

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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;

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    still no luck -- error message reads: "no matching function for call to `strcmp(std::string&, const char[7])' "

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > "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 dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    if you insist to use strcmp with std:string use
    Code:
    		if ( strcmp ( pass1.c_str(), "password" ) == 0 ) {
    Kurt

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    ok, i played around with my code and i got it to work, thanks for all your help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21