Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
如何在循环中使用scanf忽略第一个字符后的所有输入?_C_Loops_Scanf - Fatal编程技术网

如何在循环中使用scanf忽略第一个字符后的所有输入?

如何在循环中使用scanf忽略第一个字符后的所有输入?,c,loops,scanf,C,Loops,Scanf,我要求用户输入字符,但问题是,如果用户输入多个字符,将对所有字符执行循环 do { printf("For input type 'u', for output type 'p': "); scanf("%c", &c); } while ((c != 'u') && (c != 'p')); 因此,如果用户输入示例类型'aaaa',循环将重复执行5次printf文本。我尝

我要求用户输入字符,但问题是,如果用户输入多个字符,将对所有字符执行循环

    do
    {
        printf("For input type 'u', for output type 'p': ");
        scanf("%c", &c); 
    } while ((c != 'u') && (c != 'p'));

因此,如果用户输入示例类型
'aaaa'
,循环将重复执行5次
printf
文本。我尝试了
getchar()
scanf
之后,使用转换字符串
“%c”
%c%*c
但没有任何效果。有没有办法只对第一个字符执行循环?

如果该字符不是u/p,是否只打印一次语句。如果为“是”,则可以将print语句移动到do循环上方,或通过检查是否设置了c值来使用条件语句。它将只打印一次语句。 我的语法可能不正确,请查看c编程指南

1. 
    printf("");
       do{
         }while

2.     do{
               if(c != '\0')
               {
                printf("For input type 'u', for output type 'p':");
               }
            scanf('%c',&c);
           } while ((c != 'u') && (c != 'p'));
,获取第一个字符,忽略其余字符?