Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,如何只接受某些字符串并继续要求用户输入,直到输入有效的输入?_C_Input_While Loop - Fatal编程技术网

在C语言中,如何只接受某些字符串并继续要求用户输入,直到输入有效的输入?

在C语言中,如何只接受某些字符串并继续要求用户输入,直到输入有效的输入?,c,input,while-loop,C,Input,While Loop,我试图用C语言编写一个简单的程序,它接收字符串并根据输入执行操作。但是,如果用户输入无效字符串,我希望程序再次要求用户输入,直到用户提供有效的输入字符串 示例输出: P: Please enter a string. U: runMarathon P: Unable to process request, please enter a valid Input: U: rideBike P: Unable to process request, please enter a valid Input

我试图用C语言编写一个简单的程序,它接收字符串并根据输入执行操作。但是,如果用户输入无效字符串,我希望程序再次要求用户输入,直到用户提供有效的输入字符串

示例输出:

P: Please enter a string.
U: runMarathon
P: Unable to process request, please enter a valid Input:
U: rideBike
P: Unable to process request, please enter a valid Input:
U: sayHello
P: Hello World.
我有一个这样的计划:

int num;

while (scanf("%d",&num) != 1 || num <= 0)
{
    printf("Please enter an integer greater than 0\n");
    fflush(stdin);
}
但是,当运行此方法时,在接受输入后,程序将继续打印:

Your command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: HelloYour command is this: Hello
诸如此类。我很惊讶,我找不到任何资源来解决这个看似简单的问题。我知道我可以使用strcmp来比较字符串,但是如何让while循环在再次打印响应之前等待用户输入呢?为什么我不能使用fflush(stdin)


欢迎您的任何意见,谢谢

为什么我不能使用
fflush(stdin)

从技术上讲,您可以这样做,但您必须非常小心,因为C标准只为输出/更新流定义了
fflush
,而不是为输入流定义了
fflush(stdin)
。然后,一些实现所做的可能是例如清除输入缓冲区。如果您确实迫切需要使用它,请查阅您的实现文档和代码


C-99标准§7.19.5.2/2/3 fflush功能

概要

一,
#包括
int-fflush(文件*流)

2如果流指向输出流或更新流,其中 未输入最近的操作,fflush函数导致 要传递到主机的该流的任何未写入数据 要写入文件的环境;否则,该行为是错误的 未定义

3如果流是空指针,则fflush函数执行 此刷新操作用于定义行为的所有流 上面

返回

4 fflush函数设置流的错误指示器 如果发生写入错误,则返回EOF,否则返回零


为什么我不能使用
fflush(stdin)

从技术上讲,您可以这样做,但您必须非常小心,因为C标准只为输出/更新流定义了
fflush
,而不是为输入流定义了
fflush(stdin)
。然后,一些实现所做的可能是例如清除输入缓冲区。如果您确实迫切需要使用它,请查阅您的实现文档和代码


C-99标准§7.19.5.2/2/3 fflush功能

概要

一,
#包括
int-fflush(文件*流)

2如果流指向输出流或更新流,其中 未输入最近的操作,fflush函数导致 要传递到主机的该流的任何未写入数据 要写入文件的环境;否则,该行为是错误的 未定义

3如果流是空指针,则fflush函数执行 此刷新操作用于定义行为的所有流 上面

返回

4 fflush函数设置流的错误指示器 如果发生写入错误,则返回EOF,否则返回零


你可以这样做:

int main()
{
    char input[50];
    do
    {
        printf("Please enter a command:");
        if (scanf("%s", &input) != 1) //scanf returns the number of args successfully received
        {
            printf("Please enter a command\n");
        }

        printf("Please enter a valid command!\n");

    } while (strcmp(input, "good") != 0); //repeat above until input = "good"

                                          //print the good command
    printf("Your command is this: %s\n", input);
    return 0;
}

需要注意的是,您应该始终确保您正在写入的缓冲区足够大,以容纳要放入的内容

您可以这样做:

int main()
{
    char input[50];
    do
    {
        printf("Please enter a command:");
        if (scanf("%s", &input) != 1) //scanf returns the number of args successfully received
        {
            printf("Please enter a command\n");
        }

        printf("Please enter a valid command!\n");

    } while (strcmp(input, "good") != 0); //repeat above until input = "good"

                                          //print the good command
    printf("Your command is this: %s\n", input);
    return 0;
}

需要注意的是,您应该始终确保您正在写入的缓冲区足够大,以容纳要放入的内容

意外输入的问题是,
scanf
不会从输入缓冲区中删除该输入,因此下次循环迭代时,它将尝试读取相同的意外输入


解决此问题的最常用方法是使用例如
fgets
读取整行,然后在字符串上使用
sscanf

意外输入的问题是
scanf
不会从输入缓冲区中删除该输入,因此下次循环迭代时,它将尝试读取相同的意外输入


解决这一问题的最常用方法是使用例如
fgets
读取整行,然后在字符串上使用
sscanf

您甚至可以使用c吗?数组永远不会与字符串文字进行比较。@EOF我使用C,但过多的C会使我生疏:(Tony,我运行了代码。while循环缺少一个分号,我使用strcmp而不是!=。我仍然遇到程序无限打印出第一个用户输入的问题。有什么想法吗?@EquityEcho我编辑了代码,所以现在它实际上可以工作了。@TonyTheLion,谢谢!它现在似乎可以工作了!它到底为什么运行无限厕所以前的p?似乎是用了符号和。你连c都没有吗?数组永远不会和字符串文字比较。@EOF我是c,但太多的c会让我生疏(Tony,我运行了代码。while循环缺少一个分号,我使用strcmp而不是!=。我仍然遇到程序无限打印出第一个用户输入的问题。有什么想法吗?@EquityEcho我编辑了代码,所以现在它实际上可以工作了。@TonyTheLion,谢谢!它现在似乎可以工作了!它到底为什么运行无限厕所p之前?好像是用了符号。