开发者

How can I quit a while loop in Objective C using scanf and a C character array string comparison?

开发者 https://www.devze.com 2023-02-20 03:19 出处:网络
I am writing a simple test program to quit a while loop when the user enters EXIT: char *userEntry; while(userEntry != \"EXIT\")

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,

  1. 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];

  2. you can't compare a pointer to a char array, try using strcmp... write it like this

if(0!=strcmp(userEntry, "EXIT")) { scanf(...) }

AsiQue

0

精彩评论

暂无评论...
验证码 换一张
取 消