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 ...
随机推荐
- subversion SVN
subversion(简称svn)是近年来崛起的版本管理软件系统,是cvs的接班人.目前,绝大多数开源软件都使用svn作为代码版本管理软件. Subversion是一个版本控制系统,相对于的RCS.C ...
- GET和POST请求
GET与POST请求 简介 GET请求解释及语法格式 POST请求简介及语法 GET请求代码 POST请求代码 GET请求解释及语法格式: 网络请求默认是get 网络请求有很多种:GET查 POST改 ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- CoreData数据库迁移的操作
CoreData数据库迁移操作步骤,操作是基于Xcode7. 1.添加新的数据库.选中当前数据库版本:Editor->Add Model Verson,创建一个新的数据库版本. 2.Comman ...
- Swift 二维码扫描 简单实现
3.30看视频 学到了二维码简单的实现 还有一些动画的实现 今天就先记录一下二维码扫描的简单实现 不太好记手写一遍 学习的基础在于模仿嘛 创建一个实现二维码扫描的步骤 1.首先是懒加载创建 会话 ...
- GCD中的dispatch_barrier_async函数的使用(栅栏函数)
<一>什么是dispatch_barrier_async函数 毫无疑问,dispatch_barrier_async函数的作用与barrier的意思相同,在进程管理中起到一个栅栏的作用,它 ...
- MVC数据库数据分页显示
首先从数据库获取数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- VBS进行http请求及JSON数据的读取和生成
背景: 近期帮一个公司做第三方API的二次封装,需要部署到该公司网站.所获取的是Json数据格式.由于该公司原系统采用的ASP+VBS技术方案,因此采用VBS对API进行请求.封装. 实现: 废话不多 ...
- 腾讯Tinker初入门总结
- Asp.net MVC验证哪些事(3)-- Remote验证及其改进(附源码)
表单中的输入项,有些是固定的,不变的验证规则,比如字符长度,必填等.但有些是动态的,比如注册用户名是否存在这样的检查,这个需要访问服务器后台才能解决.这篇文章将会介绍MVC中如何使用[RemoteAt ...