Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How come strcmp cant work on matching strings?? Posted by Weng on 8 Jan 2006 at 7:55 AM
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,"\0"); 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\n");
break;
}
}

}while(p != NULL);

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

Advice would be appreciated! :D
Report
Re: How come strcmp cant work on matching strings?? Posted by Donotalo on 8 Jan 2006 at 9:21 AM
: 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,"\0"); 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\n");
: break;
: }
: }
:
: }while(p != NULL);
:
: if(match == 0)
: printf("Unauthorised Login\n");
:
: Advice would be appreciated! :D

how do u take userinput? using printf() see both userinput and temp & check whether they r same.


~Donotalo()

Report
Re: How come strcmp cant work on matching strings?? Posted by shuvam on 26 Jan 2006 at 11:01 PM
: : 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,"\0"); 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\n");
: : break;
: : }
: : }
: :
: : }while(p != NULL);
: :
: : if(match == 0)
: : printf("Unauthorised Login\n");
: :
: : Advice would be appreciated! :D
:
: how do u take userinput? using printf() see both userinput and temp & check whether they r same.
:

:
~Donotalo()
:
:

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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.