zoukankan      html  css  js  c++  java
  • getchar()和scanf()混合使用的坑

    最近在混合使用 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:
    ");
    	while ((ch = getchar()) != '
    ')
    	{
    		scanf("%d %d", &rows, &cols);
    		display(ch, rows, cols);
    		printf("Enter another character and two integers;
    ");
    		printf("Enter a newline to quit.
    ");
    	}
    	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('
    ');/* 结束一行并开始新的一行 */
    	}
    }
    

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

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

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

    while (getchar() != '
    ')
          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:
    ");
    	while ((ch = getchar()) != '
    ')
    	{
    		if (scanf("%d %d", &rows, &cols) != 2)
    			break;
    		display(ch, rows, cols);
    		while (getchar() != '
    ')
    			continue;
    		printf("Enter another character and two integers;
    ");
    		printf("Enter a newline to quit.
    ");
    	}
    	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('
    ');  /* 结束一行并开始新的一行 */
    	}
    }
    

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

  • 相关阅读:
    智能手机
    Micro LED
    paper-10-IRM-in-MANETs
    INFOCOM
    如何基于 Android Things 构建一个智能家居系统?
    (OK) VNCserver
    CCF 2016-04-2 俄罗斯方块
    CCF 2016-04-1 折点计数
    洛谷 P1927 防护伞
    洛谷 P1843 奶牛晒衣服
  • 原文地址:https://www.cnblogs.com/nnngu/p/13223836.html
Copyright © 2011-2022 走看看