初学萌新 求指教 !!!
错误程序:
#include
void display(char cr, int lines, int width);//打印字符的函数
int main(void)
{
int ch; //待打印字符
int rows, cols; //行数 列数
printf("Enter a character and two integers :\n");
scanf(" %c", &ch);
while (ch != '\n')
{
if(scanf("%d%d", &rows, &cols)!=2)
break;
display(ch, rows, cols);
printf("Enter another character and two integers :\n");
printf("Enter a newline to quit .\n");
while (getchar() != '\n')//跳过前面多余的的输入
continue;
scanf("%c", &ch); /*遇到的问题???*/
//这里如果输入 回车换行符 ch赋值为 \n 为什么程序不退出循环呢?
//如果用getchar() 简化所有的scanf()输入换行符 就能退出;
}
printf("Bye !");
return 0;
}
void display(char cr, int lines, int width)//字符成 行 列 打印
{
int row, col;
for (row = 1; row <= lines; row++) {
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n');
}
}
正确程序:
下面是改过的函数
#include
void display(char cr, int lines, int width);
int main(void)
{
int ch;//待打印字符
int rows, cols;//行数 列数
printf("Enter a character and two integers :\n");
while ((ch=getchar()) != '\n') //改
{
if(scanf("%d%d", &rows, &cols)!=2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers :\n");
printf("Enter a newline to quit .\n");
//输入换行符即可退出
}
printf("Bye !");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++) {
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n');
}
}
初学萌新 求指教 !!!
感谢耐心看完 感谢