Howdy, Stranger!

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

Categories

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.