首页 > 其他 > 详细

getchar()和scanf()混合使用的坑

时间:2020-07-02 12:54:39      阅读:41      评论:0      收藏:0      [点我收藏+]

最近在混合使用 getchar()scanf() 的时候遇到一个坑,现在记录一下。

代码中使用 getchar() 处理字符输入,用 scanf() 处理数字输入。

getchar() 读取每个字符,包括空格、制表符和换行符; 而 scanf() 在读取数字时则会跳过空格、
制表符和换行符。

比如下面这个程序,读入一个字符和两个数字,然后根据输入的两个数字指定的行数和列数打印该字符。

#include <stdio.h>
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‘)
	{
		scanf("%d %d", &rows, &cols);
		display(ch, rows, cols);
		printf("Enter another character and two integers;\n");
		printf("Enter a newline to quit.\n");
	}
	printf("Bye.\n");
	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‘);/* 结束一行并开始新的一行 */
	}
}

编译、运行程序,发现程序在输出 Enter a newline to quit. 之后就自动退出了。

原来,在输入了一个字符和两个数字之后,需要敲一个回车键,回车键产生了一个换行符。这个换行符不会被例程中的 scanf() 读取,但它会继续留在输入队列中。当程序运行到下一轮循环的 getchar() 函数时,换行符就会被 getchar() 函数读取,导致程序直接结束了。

解决方法:可以在循环内部添加如下代码来消耗输入队列中的换行符以及其他多余的字符:

while (getchar() != ‘\n‘)
      continue;

完整代码如下:

#include <stdio.h>
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.\n");
	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‘);  /* 结束一行并开始新的一行 */
	}
}

如发现有不对的地方,欢迎在评论区指出。

getchar()和scanf()混合使用的坑

原文:https://www.cnblogs.com/nnngu/p/13223836.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!