【发布时间】:2020-03-02 20:52:59
【问题描述】:
我在使用下面的代码时遇到了问题。在我输入响应 [y/n] 之前,它就退出了程序。我在我的编译器中没有看到任何错误,所以我很难解决这个问题。
srand(time(NULL));
int nGid; //guest id
char opt1;
printf(" Hello Guest! do you have an id number [Y/N]?");
scanf("%c", &opt1);
opt1 = toupper(opt1);
//asks for guest id
if (strcmp(opt1, 'Y') == 0){
printf("Please enter id: \n");
scanf("%d", &nGid);
}
//generates random id number
else {
nGid = rand()%100;
printf("Your guest id is : %d", nGid);
return 0;
}
感谢您的帮助!
【问题讨论】:
-
OT:
strcmp()需要两个 指针 指向0-终止的char-array,即两个char*。代码通过了两个char。认真对待编译器的警告。 -
比较两个字符只是为了
opt1 == 'Y'。
标签: c char c-strings strcmp toupper