Sunday, August 2, 2009

Help complete this C?

i have problem in my program....when i enter my name kumagpoko..it displays name denied but it should be accepted...how can i fix this??? and one more... how can i put loop to "please enter your name" when i try to input a wrong name....


tnx for the help...:)





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





main()





{


int name;








printf("\n Welcome to Login Screen\n\n\n\n");





printf("Please enter your name:\n");


scanf("%s",%26amp;name);





if (name=="kumagpoko")


{


printf("Name Accepted");


}


else


{


printf("Name Denied");


}





getch();


}

Help complete this C?
e.m.bed correctly pointed out the type mistake in name and the incorrect comparision. The second part of your question could be answered by changing his code by as follows (add 3 lines marked with %26lt;%26lt;%26lt;%26lt;%26lt;):





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


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


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





void main()


{


char *name;





do { %26lt;%26lt;%26lt;%26lt;%26lt;


printf("\n Welcome to Login Screen\n\n\n\n");





printf("Please enter your name:\n");


scanf("%s",name);





if (stricmp(name, "kumagpoko") == 0)


{


printf("Name Accepted");


break; %26lt;%26lt;%26lt;%26lt;%26lt;


}


else


{


printf("Name Denied");


}


} while (strlen(name) %26gt; 0 %26lt;%26lt;%26lt;%26lt;%26lt; (empty string also terminates loop)
Reply:You're comparing int name with a string "kumagpoko".





Here is working code to demonstrate:





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


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


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





void main()


{


char *name;





printf("\n Welcome to Login Screen\n\n\n\n");





printf("Please enter your name:\n");


scanf("%s",name);





if (stricmp(name, "kumagpoko") == 0)


{


printf("Name Accepted");


}


else


{


printf("Name Denied");


}





getch();


}





Stricmp compares 2 strings and returns zero if they are equal.


Stricmp is case-insensitive.





If you want case-sensitive , use strcmp in the same way.


No comments:

Post a Comment