Sunday, August 2, 2009

Password problem in c langauge?

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;string.h%26gt;


int main(void)


{


int i;


char pw[7],pass[]="helloa ";


for(i=0;i%26lt;6;i++)


{


pw[i]=getch();


printf("*");


}


pw[6]='/0';





if((strcmp(pw,pass))==0)


printf("yes");


else


printf("no");


return 0;


}





i am entering helloa as the password still it prints no instead of yes...


can somebody debug the program plzzzzzzzzzzzz.

Password problem in c langauge?
When debugging problems in programs, it's often a good idea to occasionally output the values of what you think may be wrong.





In this case, before your if((strcmp..., you could do:





printf("pw = %s.\npass = %s.\n", pw, pass);





When doing so, you should see that the two strings are not the same due to your additional space.
Reply:With the debugging steps, With the input not having a proper null character, you would have seen that there was a lot of garbage following the 6th byte of the string, which should have pointed you to the null byte problem.





Glad you got it working, and thanks for the best answer score. :) Report It

Reply:remove the extra space after character 'a' in "helloa "...
Reply:remove space at the end in "helloa "


for better understanding print the string pw[ ] and pass[ ]


before comparing then you come to know the mistake
Reply:there is a facility of pre-defined function called getpass() in conio.h, you can refer to that ,if not ,send e-mail to pavan.kumar10@yahoo.com


No comments:

Post a Comment