Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How come strcmp cant work on matching strings??

WengWeng Posts: 52Member
Hi,
I am writing a simple password management program using C. The program asks the user for username and password and stores them in 2 character arrays. The 2 character arrays are then concatanated using strcat. Then the password.txt file is read and they are also stored in a character array.

Then, I use strcmp to compare the char array containing the user input with the char array containing the lines read from the file. I used if(strcmp(userinput,temp) == 0) to try to detect a match but even if there is a match, strcmp cant detect it.

Could the problem be caused by the random characters near the end of the character arrays which cause the mismatch? I purposely set the array to be longer than the username so as to prevent overflow. Does inserting null using strcat(userinput,""); work?

A snippet of the code is as follows:

do{
p = fgets(temp,90,fp); //use fgets to read lines from the file
puts(temp);

if(p != NULL)
{
if(strcmp(userinput,temp) == 0) //doesnt work!
{
match = 1; //matching flag
printf("Login Authorised
");
break;
}
}

}while(p != NULL);

if(match == 0)
printf("Unauthorised Login
");

Advice would be appreciated! :D

Comments

  • DonotaloDonotalo Posts: 715Member
    : Hi,
    : I am writing a simple password management program using C. The program asks the user for username and password and stores them in 2 character arrays. The 2 character arrays are then concatanated using strcat. Then the password.txt file is read and they are also stored in a character array.
    :
    : Then, I use strcmp to compare the char array containing the user input with the char array containing the lines read from the file. I used if(strcmp(userinput,temp) == 0) to try to detect a match but even if there is a match, strcmp cant detect it.
    :
    : Could the problem be caused by the random characters near the end of the character arrays which cause the mismatch? I purposely set the array to be longer than the username so as to prevent overflow. Does inserting null using strcat(userinput,""); work?
    :
    : A snippet of the code is as follows:
    :
    : do{
    : p = fgets(temp,90,fp); //use fgets to read lines from the file
    : puts(temp);
    :
    : if(p != NULL)
    : {
    : if(strcmp(userinput,temp) == 0) //doesnt work!
    : {
    : match = 1; //matching flag
    : printf("Login Authorised
    ");
    : break;
    : }
    : }
    :
    : }while(p != NULL);
    :
    : if(match == 0)
    : printf("Unauthorised Login
    ");
    :
    : Advice would be appreciated! :D
    [purple]
    how do u take [blue]userinput[/blue]? using printf() see both [blue]userinput[/blue] and [blue]temp[/blue] & check whether they r same.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • shuvamshuvam Posts: 9Member
    : : Hi,
    : : I am writing a simple password management program using C. The program asks the user for username and password and stores them in 2 character arrays. The 2 character arrays are then concatanated using strcat. Then the password.txt file is read and they are also stored in a character array.
    : :
    : : Then, I use strcmp to compare the char array containing the user input with the char array containing the lines read from the file. I used if(strcmp(userinput,temp) == 0) to try to detect a match but even if there is a match, strcmp cant detect it.
    : :
    : : Could the problem be caused by the random characters near the end of the character arrays which cause the mismatch? I purposely set the array to be longer than the username so as to prevent overflow. Does inserting null using strcat(userinput,""); work?
    : :
    : : A snippet of the code is as follows:
    : :
    : : do{
    : : p = fgets(temp,90,fp); //use fgets to read lines from the file
    : : puts(temp);
    : :
    : : if(p != NULL)
    : : {
    : : if(strcmp(userinput,temp) == 0) //doesnt work!
    : : {
    : : match = 1; //matching flag
    : : printf("Login Authorised
    ");
    : : break;
    : : }
    : : }
    : :
    : : }while(p != NULL);
    : :
    : : if(match == 0)
    : : printf("Unauthorised Login
    ");
    : :
    : : Advice would be appreciated! :D
    : [purple]
    : how do u take [blue]userinput[/blue]? using printf() see both [blue]userinput[/blue] and [blue]temp[/blue] & check whether they r same.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :

    strcat(userinput,""); will not work. I think here lies the problem. You will have to explicitly insert the Null character at the end of the string.

Sign In or Register to comment.