以下是一个简单的数列求和程序,我的问题是最后为何用两个getchar()函数,一般都是用一个就可以。经过上机实践的确要用两个getchar()才可以在最后暂时停止程序运行。请解释,谢谢。
#include <stdio.h>
main()
{
int i,j,n;
long sum=0,temp=0;
printf("Please input a number to n:\n");
scanf("%d",&n);
if(n<1)
{
printf("The n must no less than 1!\n");
return;
}
for(i=1;i<=n;i++)
{
temp=0;
for(j=1;j<=i;j++)
temp+=j;
sum+=temp;
}
printf("The sum of the sequence(%d) is %d\n",n,sum);
getchar();
getchar();
}
下面的也有scanf("%d",&matrixA[i][j]);语句,为何用一个getch();就可以?想知道什么时候用两个getch();什么时候用一个就可以了?
/* 用二维数组实现矩阵的转置 */
#include <stdio.h>
#define ROW 3
#define COL 4
main()
{
int matrixA[ROW][COL],matrixB[COL][ROW];
int i,j;
clrscr();
printf("Enter elements of the matrixA,");
printf("%d*%d:\n",ROW,COL);
for( i=0; i<ROW; i++ )
{
for( j=0; j<COL; j++ )
{
scanf("%d",&matrixA[i][j]);
}
}
for( i=0; i<ROW; i++ )
{
for( j=0; j<COL; j++ )
{
matrixB[j][i] = matrixA[i][j];
}
}
printf("MatrixB,");
printf("%d*%d:\n",COL,ROW);
for( i=0; i<COL; i++ )
{
for( j=0; j<ROW; j++ )
{
printf("%8d",matrixB[i][j]);
}
printf("\n");
}
printf("\n Press Any Key to Quit... \n");
getch();
}