程序崩溃了,但不是固定的。当前输入的每个字符在 while 循环中返回无效。
char get_yes_or_no_character(void)
/*
This is to purely get the Y or N for options
*/
{
char choice = '\0';
choice = printf("Would you like to use this roll for a game combination? Press Y for yes or N for no!\n");
scanf(" %c", &choice);
return choice;
}
char choice_input_validation(char choice)
/*
This makes sure that input validation for the character is correct
*/
{
while (choice != 'y' || choice != 'Y' || choice != 'n' || choice != 'N')
{
printf("Invalid option, try again\n");
choice = printf("Would you like to use this roll for a game combination? Press Y for yes or N for no!\n");
scanf(" %c", &choice);
}
return choice;
}
这是一段代码
if (roll < 3)
{
choice = get_yes_or_no_character();
choice = choice_input_validation(choice);
}
if (choice == 'y' || choice == 'Y')
{
break;
}
最佳答案
换行:
scanf(" %c", choice);
到:
scanf(" %c", &choice);
scanf
要求以指针作为参数。
同时更改您的 while
条件:
while (choice != 'y' || choice != 'Y' || choice != 'n' || choice != 'N')
到:
while ( (choice != 'y') && (choice != 'Y') && (choice != 'n') && (choice != 'N') )
如果没有有任何以前的值,您就认为选择
无效。
关于字符仅被视为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712291/