所以我有一些这样的代码。 请注意,这是在C89中。
void inputChoice(int* choicePtr)
{
int choice;
printf(BLUE "\nINPUT: " RESET); /* Print input using the ansi colour code for Blue. */
scanf("%d", &choice); /* Parse the choice. */
while ((choice < 1) && (choice > 5)) /* Loop until choice is one of the menu options. */
{
/* Request for a valid menu option, printing this using the ansi colour code for Red, then resetting back to the default colour. */
printf(RED "\nMenu Selection must be between (1 and 5) inclusive. \nPlease Re-enter. \n" RESET);
scanf("%d", &choice); /* Parse the choice. */
}
*choicePtr = choice; /* Set the choice pointer to the valid choice after validation. */
}
哪个得到选择。它适用于整数。但是,如果有人输入其他任何内容,例如角色。它无限循环。 我想以某种方式检查是否输入了字符。
我尝试此操作的一种方法是添加此代码以检查是否输入了字符,因为如果扫描不正确,则整数将为0。
像这样:
scanf("%d", &choice);
while (choice == 0)
{
printf("Invalid Choice Re-enter.");
scanf("%d", &choice);
}
但这也不起作用。
任何帮助将非常感谢。
表达方式
永远不会是真的,因为一个数字不能大于5并且小于1,因此您需要: