Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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 - Fatal编程技术网

在C中验证用户输入

在C中验证用户输入,c,C,所以我需要一个int大于2的用户输入 printf("Number of triangles (must be greater than 2) = "); fflush(stdin); scanf("%d", &num_of_triangles); while (num_of_triangles < 3) // ?how to check using scanf? { printf("Number of triangles (

所以我需要一个int大于2的用户输入

    printf("Number of triangles (must be greater than 2) = ");
    fflush(stdin);
    scanf("%d", &num_of_triangles);
    while (num_of_triangles < 3) // ?how to check using scanf?
    {
        printf("Number of triangles (must be greater than 2) = ");
        fflush(stdin);
        scanf("%d", &num_of_triangles);
    }
printf(“三角形数(必须大于2)=”);
fflush(stdin);
scanf(“%d”、&num\u个三角形);
while(三角形的数量<3)/?如何使用scanf进行检查?
{
printf(“三角形数(必须大于2)=”);
fflush(stdin);
scanf(“%d”、&num\u个三角形);
}
是否有可能针对重复行优化此代码?

fflush(stdin)
是未定义的行为,因此您需要完全删除该行为并替换其他行为

如果出现故障,还应检查scanf的输出

如果希望提高可读性,还可以将
scanf
直接移动到while循环中,如下所示

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3)
while(scanf(“%d”、&num\u of_三角形)==1&&num\u of_三角形<3)
fflush(stdin)
是未定义的行为,因此您需要完全删除该行为并替换其他行为

如果出现故障,还应检查scanf的输出

如果希望提高可读性,还可以将
scanf
直接移动到while循环中,如下所示

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3)
while(scanf(“%d”、&num\u of_三角形)==1&&num\u of_三角形<3)

您可以这样编写代码-

printf("Number of triangles (must be greater than 2) = ");

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3) //check return of scanf as well as number
{
    printf("Number of triangles (must be greater than 2) = ");       
}
printf(“三角形数(必须大于2)=”);
while(scanf(“%d”),&num\u of_triangles)==1&&num\u of_triangles<3)//检查scanf的返回值以及数量
{
printf(“三角形数(必须大于2)=”);
}

循环将迭代,直到
scanf
成功并且
num\u of_三角形
小于
3

您可以这样编写代码-

printf("Number of triangles (must be greater than 2) = ");

while (scanf("%d",&num_of_triangles)==1 && num_of_triangles < 3) //check return of scanf as well as number
{
    printf("Number of triangles (must be greater than 2) = ");       
}
printf(“三角形数(必须大于2)=”);
while(scanf(“%d”),&num\u of_triangles)==1&&num\u of_triangles<3)//检查scanf的返回值以及数量
{
printf(“三角形数(必须大于2)=”);
}

循环将迭代,直到
scanf
成功并且
num\u of_三角形
小于
3

使用
do
代替
while
while
。这样,循环将始终至少运行一次

num_of_triangles = 0;
do {
    printf("Number of triangles (must be greater than 2) = ");
    scanf("%d", &num_of_triangles);
    while (getchar() != '\n');    // this flushes the input buffer
} while (num_of_triangles < 3);
num\u of_三角形=0;
做{
printf(“三角形数(必须大于2)=”);
scanf(“%d”、&num\u个三角形);
while(getchar()!='\n');//这将刷新输入缓冲区
}而(三角形的数量<3);
另外,不要
fflush(stdin)
,因为这是未定义的行为

编辑:

Visual Studio似乎允许
fflush(stdin)
。发件人:

fflush函数刷新流。如果文件与 流是为输出打开的,fflush将其内容写入该文件 与流关联的缓冲区如果该流为 输入时,fflush清除缓冲区的内容。fflush否定缓冲区 任何先前对ungetc的调用对流的影响。另外,fflush(NULL) 刷新为输出打开的所有流。这条小溪在洪水过后仍然畅通 电话。fflush对未缓冲流没有影响


然而,一般来说,这种行为不应该被依赖。最好使用像上述代码这样更便于携带的操作。

而不是
while
,使用
do
while
。这样,循环将始终至少运行一次

num_of_triangles = 0;
do {
    printf("Number of triangles (must be greater than 2) = ");
    scanf("%d", &num_of_triangles);
    while (getchar() != '\n');    // this flushes the input buffer
} while (num_of_triangles < 3);
num\u of_三角形=0;
做{
printf(“三角形数(必须大于2)=”);
scanf(“%d”、&num\u个三角形);
while(getchar()!='\n');//这将刷新输入缓冲区
}而(三角形的数量<3);
另外,不要
fflush(stdin)
,因为这是未定义的行为

编辑:

Visual Studio似乎允许
fflush(stdin)
。发件人:

fflush函数刷新流。如果文件与 流是为输出打开的,fflush将其内容写入该文件 与流关联的缓冲区如果该流为 输入时,fflush清除缓冲区的内容。fflush否定缓冲区 任何先前对ungetc的调用对流的影响。另外,fflush(NULL) 刷新为输出打开的所有流。这条小溪在洪水过后仍然畅通 电话。fflush对未缓冲流没有影响


然而,一般来说,这种行为不应该被依赖。做一些更具可移植性的事情,比如上面的代码是更好的。

fflush(stdin)
是未定义的行为。请发布一个完整的例子。
fflush(stdin)
是未定义的行为。请发布一个完整的例子。我们在Visual Studio中工作,我们的老师建议在scanf()之前使用fflush(stdin)。我应该如何替换它?@Lorant最初没有。否则,
getchar()
将拾取您输入的第一个值,而不是
scanf
。示例代码中有这样的注释:
//fflush on input stream是C标准的扩展
我们在Visual Studio中工作,我们的老师建议在scanf()之前使用fflush(stdin)。我应该如何替换它?@Lorant最初没有。否则,
getchar()
将拾取您输入的第一个值,而不是
scanf
。示例代码中有这样的注释:
//输入流上的fflush是C标准的扩展
不会(scanf(“%d”,&num_of_三角形)!=1 | num_of_三角形<3)更好吗?@Lorant会给你一个字符输入的无限循环。@Coolgay对不起,我没有字符输入的无限循环。哦。我误读了你的密码,弄糊涂了。它应该是
while(scanf(“%d”,&num_of_triangles)!=1 | | num_of_triangles<3)
,以便在无效输入(如字符)上显示错误消息。如果输入无效,您还需要清除
stdin
。为什么它是一个无限循环?而(scanf(“%d”、&num_of_三角形)!=1|num