2015-06-13 29 views
1

我使用Eclipse的c/C++我试过代码制作pascal's triangle,当我运行它不打印“输入行数”:直到我输入数字后,即使printf谈到scanf函数之前没有提示一个整数

int main(void) { 
int rows, coef = 1, space, i, j; 
printf("Enter number of rows: "); 
scanf("%d", &rows); 
printf("\n"); //i added this 
for (i = 0; i < rows; i++) { 
    for (space = 1; space <= rows - i; space++) 
     printf(" "); 
    for (j = 0; j <= i; j++) { 
     if (j == 0 || i == 0) 
      coef = 1; 
     else 
      coef = coef * (i - j + 1)/j; 
     printf("%4d", coef); 
    } 
    printf("\n"); 
} 
return 0; 
} 

我的问题是,是否有什么毛病我的Eclipse C/C++,因为我从来没有对Eclipse进行Java这个问题时,我问这样的输入。另外我如何解决这个问题。

回答

2

您的工具没有任何问题。

printf默认缓冲其输出,直到要打印换行\n

您可以通过在printf之后执行fflush(stdout)来解决此问题,该文件不包含\n

或者你可以关掉共行缓冲:

setvbuf(stdout, NULL, _IONBF, 0); 
+0

所以在java中它会自动做什么fflush(标准输出)不会,因为我从未有过这个问题。 –

+0

'System.out.print()'不像'printf'那样缓冲。 –

相关问题