Thread: strcmp

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    strcmp

    i have assigned a character array of 10 to hold a password.

    then i ask for the user to input a password and then compares the new password with the original to validate its authenticity.

    However i cant get strcmp to retun 0 (zero) although the new password is entered exactly like the old one.

    The while loop simply outputs *'s to the screen instead of echoing the password entered....i dont know if its the problem...


    char original_password[10]="member";

    char new_password[10];

    cout<<"enter your password\n";
    int i = 0;

    while( ( temp_mem_password[i] = getch() ) != '\r' )
    {
    cout << "*";

    i++;
    }


    compare=strcmp(mem_password,temp_mem_password);
    cout<<"compare:"<<compare;

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    1. getch(); returns a keycode -> that is a number!

    Code:
    int temp;
    	int compare;
    	string original_password="member"; 
    
    	string new_password; 
    
    	cout<<"enter your password\n"; 
    	int i = 0; 
    
    	while( ( temp = getch() ) != '\r' ) 
    	{ 
    		new_password += char(temp); //convert to character
    		cout << "*"; 
    		i++; 
    	}
    	if(original_password == new_password)
    		cout<<"correct!";
    You need to use an if statement here with the '==' operator because strcmp does not like the two different types of strings. However the code works fine. If you have any questions just ask.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: strcmp

    Originally posted by stanleyw
    i have assigned a character array of 10 to hold a password.
    <--snip-->
    .... then if I hex dump your binary, I will know your password.

    Never store passwords in programs this way, it's far too easy to get at them! Encrypt it, or build it dynamically or something.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM