【问题标题】:Do you want to continue(y/n) issue你想继续(y/n)问题吗
【发布时间】:2014-10-19 03:48:16
【问题描述】:

Do you want to continue (y/n) 在此 c 代码中不起作用。我希望它在我输入'y'时要求输入一个字符串,如果我输入n则退出程序。我尝试了很多选择,但无济于事。 感谢您的帮助

    do
    {
        i = 0, final = 0, s = 0;
        printf("\n\nEnter Input String.. ");
        scanf("%s", string);

        while (string[i] != '\0')
            if ((s = check(string[i++], s)) < 0)
                break;
        for (i = 0 ; i < nfinals ; i++)
            if (f[i] == s )
                final = 1;
        if (final == 1)
            printf("\n String is accepted");
        else
            printf("String is rejected");

        printf("\nDo you want to continue.?  \n(y/n) ");
    }
    while (getch() == 'y');

    return getch();
}

【问题讨论】:

  • 不起作用请更具体。

标签: c


【解决方案1】:
if (getch() == 'n') {
    // print something
    return;
}

【讨论】:

    【解决方案2】:

    在将 char 作为输入之前应该刷新缓冲区

    do
    {//..your code
    printf("\nDo you want to continue.?  \n(y/n) ");
    scanf("%c",&ch);
    scanf("%c",&ch);
    }
    while (ch == 'y');
    

    【讨论】:

    • stdin 上使用fflush 是未定义的行为
    • 替换 fflush(stdin);用 scanf("%c",&ch);即放 scanf("%c",&ch); 2次
    猜你喜欢
    • 2020-11-05
    • 2015-11-29
    • 2015-03-13
    • 1970-01-01
    • 2021-06-30
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多