c - do-while 循环没有结束

标签 c if-statement scanf user-input do-while

<分区>

我正在尝试做一个 do-while 循环,用户输入一个值,1 或 0,如果它不是 1 或 0,那么循环将要求他们再次执行,直到他们输入 1 或 0 . 但是,在运行时,当我输入 1 或 0 时,它会继续打印 if 语句中的内容

you have given an incorrect response, it must either be 1 or 0

即使我进入了 1/0,也永远停留在这个循环中。它是否存储 enter 键?我缺少什么以及如何解决这个问题?

int validateresultmanual(char* word, char* suggestion)
{
    int choice;
    int result;

    if(suggestion == NULL)
    {
        result = FALSE;
    }
    else
    {
        do
        {
            printf("Enter 0 for yes or 1 for no: Do you want to change %s to %s?\n", word, suggestion);
            scanf("%c", &choice);
            if(choice != 0 || choice != 1)
            {
                printf("You have given an incorrect response, it must either be 1 or 0\n");
            }
            else if(choice == 0)
            {
                printf ("\n yaaaaaaaaaaaaa \n");
                result = TRUE;
            }
            else if (choice == 1)
            {
                result = FALSE;
            }
     } while (choice != 0 || choice !=1 );
}
return result;

最佳答案

您的情况检查不正确:

改变这个

if(choice != 0 || choice != 1)

if(choice != 0 && choice != 1)

关于c - do-while 循环没有结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46936478/

相关文章:

c - 循环在第一次后跳过 scanf 语句

c++ - 何时使用 const char * 何时使用 const char[]

C中将2个字节组合成一个短整型

c - 目标和模块的内核版本

java - 不知道为什么我收到 java.util.NoSuchElementException 错误

c++ - INT 在 FOR 循环外保持不变

c++ - 为什么它没有进入 if 条件?

c - 段错误(核心转储)但不知道如何修复

c - C 语言中 inet_aton() 和 gethostbyname() 的区别?

c - 将文件中的值插入链表时出现段错误