字符仅被视为无效

标签 c character

程序崩溃了,但不是固定的。当前输入的每个字符在 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/

相关文章:

c - C语言中的字符串输入

c - GCC、重复的 typedef 和 DWARF

string - 反转用户在汇编语言中给出的字符串

r - lapply 与 gregexpr 和字符向量

bash - 如何在 Bash 中替换文件每行中除最后 n 个实例之外的所有字符实例

java - 如何将 ASCII 值的字符串表示形式转换为字符

CUDA 内核启动参数解释对了吗?

c - C中的旋转程序

html - nbsp 的反义词是什么?

c - 宏定义的未定义错误