C语言--scanf
关于C语言的scanf,首先看个例子
int get_int(void){
int input;
char ch;
while(scanf("%d",&input)!=){
printf("is not an integer,please enter agin\n");
}
printf("%d\n",input);
return input;
}
这个例子中,如果你输入的不是一个数字的话,程序就会陷入死循环,原因: 如果scanf没有成功读取输入就会将其留在输入队列中,所以下次再从输入队列中读取的时候,还是失败,所以循环了
改正:
int get_int(void){
int input;
char ch;
while(scanf("%d",&input)!=){
while(getchar()!='\n'){
continue;
}
printf("is not an integer,please enter agin\n");
}
printf("%d\n",input);
return input;
}
从输入队列中剔除那些有问题的输入
例子:
/**
* menu.c
* @desc 菜单技术
*/
#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void); int main(void){
char choice;
choice = get_choice();
if(choice != 'q'){
switch(choice){
case 'a':
printf("Buy low, sell high\n");
break;
case 'b':
putchar('\a');
break;
case 'c':
count();
break;
default:
printf("Program error!\n");
break;
}
}
printf("Bye\n");
} /**
* 获取一个整数型的输入
*/
int get_int(void){
int input;
printf("Please input a int:\n");
while(scanf("%d",&input) != ){
printf("input error,Please input again:\n");
while(getchar() != '\n'){
continue;
}
}
return input;
} /**
* 获取用户输入字符串的第一个字符
*/
char get_first(void){
char ch;
ch = getchar();
//清空输入队列中的其他字符
while(getchar() != '\n'){
continue;
}
return ch;
} char get_choice(void){
char ch;
printf("Enter the letter if your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while((ch < 'a' || ch > 'c') && ch != 'q'){
printf("Please choice a b c or q\n");
ch = get_first();
}
return ch;
} void count(void){
int n,i;
printf("Count how far? Enter a integer:\n");
n=get_int();
for(i=;i<=n;i++){
printf("%d\n",i);
}
while(getchar() != '\n'){
continue;
}
}
C语言--scanf的更多相关文章
- C语言 scanf()和gets()函数的区别
C语言 scanf()和gets()函数的区别 1.相同点:scanf( )函数和gets( )函数都可用于输入字符串 2.不同点:两者在功能上有所区别,具体区别如下: 要实现如下需求“从控制台输入字 ...
- C语言scanf与get char,gets的区别
C语言scanf与get char,gets的区别 1.scanf() scanf是C语言的格式输入函数是通用终端格式化输入函数,它从标准输入设备(键盘) 读取输入的信息.可以读入任何固有类型的数据并 ...
- C语言scanf函数详细解释
原文链接 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准 ...
- C语言Scanf函数
C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...
- [转载]VS2012编译C语言scanf函数error的解决方法
在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may ...
- C语言scanf函数详解
函数名: scanf 功 能: 运行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准输入设 ...
- c语言scanf详解
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...
- c语言:scanf()高级应用
1) 指定读取长度 还记得在 printf() 中可以指定最小输出宽度吗?就是在格式控制符的中间加上一个数字,例如,%10d表示输出的整数至少占用 10 个字符的位置: 如果整数的宽度不足 10,那么 ...
- 为什么C语言Scanf函数对字符串不要加 取地址运算符&
原文1:http://www.360doc.com/content/16/0515/11/19455598_559288667.shtml 原文2:https://zhidao.baidu.com/q ...
随机推荐
- 操作系统开发系列—13.b.进程之丰富中断处理程序
首先打开时钟中断: out_byte(INT_M_CTLMASK, 0xFE); // Master 8259, OCW1. out_byte(INT_S_CTLMASK, 0xFF); // Sla ...
- java 实现https请求
java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...
- Android属性之build.prop生成过程分析
Android的build.prop文件是在Android编译时刻收集的各种property(LCD density/语言/编译时间, etc.),编译完成之后,文件生成在out/target/pro ...
- Android 多媒体播放API简介
本文调用android的媒体播放器实现一些音乐播放操作 项目布局: <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- ios 怎么解决Could not find Developer Disk Image
- Android开发中遇到的小问题 一
1)想要ListView活着Girdview左右留些空隙,但Scrollbar要在屏幕最右边 在xml中加入 android:paddingLeft="8dp" android:p ...
- mysql 数据库乱码问题
mysql 数据库乱码问题,按如下顺序检查,一步一步排除出错位置. 最好全部编码都使用UTF8编码. 网页页面编码方式使用UTF8: <meta http-equiv="Content ...
- Solr页面查询各个字段参数解释
q:查询的关键字,此参数最为重要,例如,q=id:1,默认为q=*:*,类似于sql中的where 1=1. fq(filter query):过滤查询,提供一个可选的筛选器查询.返回在q查询符合结果 ...
- MVC Ajax Helper或jQuery异步方式加载部分视图
Model: namespace MvcApplication1.Models { public class Team { public string Preletter { get; set; } ...
- windows开关机时间记录
1. 开机时间记录批处理文件,kai.bat @echo off echo %date% %time% 开机 >>D:\开关机记录\开关机时间.txt 2. 关机时间记录批处理文件,gua ...