I am writing a simple test program to quit a while loop when the user enters EXIT:
char *userEntry;
while(userEntry != "EXIT")
{
NSLo开发者_高级运维g(@"Enter EXIT to quit");
scanf("%s", &userEntry);
}
As is, the loop doesn't quit.
Could anyone explain to me what I need to do so make it work? Thank you :)
You're comparing the address of the userEntry variable with that of the "EXIT" string constant. You need to compare their contents instead. Use strcmp
, and read up on pointers.
you have 2 problems,
You need to assign space to hold your string, just to declare *userEntry only creates a pointer. try writing it like this char userEntry[10];
you can't compare a pointer to a char array, try using strcmp... write it like this
if(0!=strcmp(userEntry, "EXIT")) { scanf(...) }
AsiQue
精彩评论