c 语言中混合输入字符和数字( c primer plus 中的程序 )
来源:互联网 发布:手机淘宝怎么看vip等级 编辑:程序博客网 时间:2024/04/25 16:15
这个程序的核心是这段代码
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") ;
}
所谓的混合输入字符和数字奥妙也就在这其中。。
#include <stdio.h>
#include <stdlib.h>
void display( char cr, int lines, int width ) ;
int main()
{
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') ;
}
}
这个程序的瑕疵。。。。就是只能输入输出一遍,因为在第二次再到达大while循环时,上次的'\n'在这里找到了安放的位置,然后程序自然地跳出了循环。。。
然后怎么办呢,当然是改下了。
书上说的是
while( ( ch = getchar() ) != '\n' )
{
scanf("%d %d", &rows, &cols ) ;
display( ch, rows, cols ) ;
while( getchar() != '\n' )
continue ;
printf("Enter another character and two integers:\n") ;
printf("Enter a newline to quit.\n") ;
}
然后网上面查了下,还有种经典的清除缓存的方法,如下:
while( ( ( c = getchar() ) != '\n' ) && c != EOF ) ;
这样子做是在任何情况下,缓冲区中的内容全部都会被清除干净,可谓是一劳永逸的方法呀。。。
本文就作为我的个人笔记记录下来,没有太内涵的内容。
- c 语言中混合输入字符和数字( c primer plus 中的程序 )
- C primer plus第8章(字符输入/输出和输入确认)习题
- C语言混合数值和字符输入简单介绍
- C Primer Plus 第8章 字符输入/输出和验证输入 编程练习
- C primer plus 字符输入输出
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- C语言:编写一个程序统计输入字符串中,各个数字、空白字符、以及其他所有字符出现的次数。
- 混合输入数字和字符
- c primer plus 程序笔记
- c primer plus 5 ch4 字符串和格式化输入/输出
- c++primer plus第十七章-输入和输出概述
- c++primer plus第十七章-文件输入和输出
- C Primer Plus-char数组类型和空字符
- c++primer plus字符库函数和条件运算符
- C Primer Plus-C语言字符串操作
- C Primer Plus-C语言运算优先级
- C Primer Plus学习 十五 菜单输入
- (学习)C Primer Plus(3)——字符串和格式化输入/输出
- 如何删除多余内核
- Linux下查看进程命令路径信息
- API两个程序间信息的传送与接收,一方发送另一方自动接收.
- hObject==handles.pushbutton1;sprintf('handles.pushbutton1 is %d',handles.pushbutton1)
- 求分段函数
- c 语言中混合输入字符和数字( c primer plus 中的程序 )
- MYSQL创建数据库时候直接指定编码和排序规则
- “文件指针,文件句柄,文件描述符” 的区别
- yii 中RULES方法的使用
- IPSecVPN和OpenVPN-IPSec平反
- Makefile 编写
- Arduino笔记
- Android中设置TextView的颜色setTextColor
- vmware play配置vmnet8 的方法