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

C 无法读取代码中的最后一个值

C 无法读取代码中的最后一个值,c,algorithm,printf,getchar,C,Algorithm,Printf,Getchar,我打算写一个程序,这样它就能打印出星星的数目。范例 输入数字:12 11 9 8 ^D 12 |************ 11 | *********** 9 | ********* 8 |********* } 使用我的代码,它会打印出正确的输出,但不会打印出我输入的最后一个值的星号。就像上面的例子一样,在我再次按下回车键或按^D键之前,它不会打印出8颗星。我想知道我的代码是否有问题 不,您的代码没有问题 stdin是行缓冲的,所以在Linux中只有按or=ctrl-d才能读取输入 有些

我打算写一个程序,这样它就能打印出星星的数目。范例

输入数字:12 11 9 8 ^D

12 |************ 11 | *********** 9 | ********* 8 |*********

}


使用我的代码,它会打印出正确的输出,但不会打印出我输入的最后一个值的星号。就像上面的例子一样,在我再次按下回车键或按^D键之前,它不会打印出8颗星。我想知道我的代码是否有问题

不,您的代码没有问题

stdin是行缓冲的,所以在Linux中只有按or=ctrl-d才能读取输入


有些库(如conio.h)提供非缓冲输入。

删除scanf%d和&a中%d之后的尾随空格


在上面的示例中,它何时打印与您键入的内容相关的每个数字的行?您应该先看看这个。。。请尝试扫描%d,&a而不是扫描%d,&a。当我按enter键时,它不应该打印它们吗??在我按下cntrl+Dstdout键之前,它不会打印最后一个,当连接到一个终端时,通常线路缓冲,因此printf\n;应将输出刷新到屏幕上。您可以尝试添加fflushstdout;以编程方式刷新输出缓冲区。
#include <stdio.h>

int main(int argc, char *argv[]) {
int a;
printf("enter values \n");

while (scanf("%d ", &a) != EOF) {

    printf("%d |", a);

    for (int j = 1; j<= a; j++) {
        printf("*");
    }
    printf("\n");
}
#include <stdio.h>

int main(int argc, char *argv[]) {
    int a;
    printf("enter values \n");

    while (scanf("%d", &a) != EOF) {

        printf("%d |", a);

        for (int j = 1; j<= a; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}