int t,r,c;
int matrix[100][100][100];
int i,j,k=0,l=0;
int te,ck=0;
scanf("%d",&t);
for(te=0;te<t;te++)
{
printf("RC");
scanf("%d %d",&r, &c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("te= %d i= %d j= %d",te,i,j);
fflush(stdin);
matrix[te][i][j] = getchar();
}
}
}
样本o / p
abhi@ubuntu:~/Desktop$ ./spoon.o
3
RC3
6
te= 0 i= 0 j= 0te= 0 i= 0 j= 1
控件直接询问j = 1的值,跳过j = 0.WHY?
答案 0 :(得分:2)
fflush(stdin)
不是清除输入缓冲区的方法。
使用:
void flushInputBuffer( void )
{
int c;
while( (c = fgetc( stdin )) != EOF && c != '\n' );
}
答案 1 :(得分:1)
这是因为getchar()
仍然会看到您在6
之后输入的换行符,因此无需阻止并等待您的更多输入。我想getchar()
不是你需要打电话的功能。
答案 2 :(得分:0)
将stdin传递给fflush()是不正确的。它应该只用在输出流上。
我的输出中并不清楚,但如果您输入3&lt; enter&gt; 6&lt; enter&gt;在RC提示符下,第一个getchar()抓取你在该行末尾输入的换行符。
尝试更改此行:
scanf("%d %d",&r, &c);
对此:
scanf("%d %d\n",&r, &c);
答案 3 :(得分:-1)
请将您的C program
与LIVE Demo