我在使用下面的代码时遇到了问题。在我输入响应 [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;
}
感谢您的帮助!
最佳答案
strcmp
采用空终止的 char *
,但您传递的是 char
作为参数。
您可以使用==
运算符直接比较两个char
。
改变。
if (strcmp(opt1, 'Y') == 0){
到
if (opt1 == 'Y'){
关于C : Process keeps exiting before being able to input anything,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58729014/