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 ...
随机推荐
- 使用fastboot工具刷入recovery.img、boot.img、system.img等
在Android SDK目录的platform-tools目录下有fastboot.exe刷机工具(注:需要将机器进入fastboo模式) 刷recovery.img: fastboot flash ...
- Failed to push selection: Read-only file system的解决方法
1.获得root权限:adb root 2.设置/system为可读写:adb remount 3.将hosts文件复制到PC:adb pull /system/usr/keylayout/mtk-k ...
- 【代码笔记】iOS-饼图
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @class QuizChartView; @interf ...
- iOS如何获取网络图片(三)有沙盒的
沙盒 沙盒简介 默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 Documents:苹果建议将程序中建立的或在程 ...
- django tmeplate 循环基数
{% for item in item_list %} {{ forloop.counter }} {# starting index 1 #} {{ forloop.counter0 }} {# s ...
- 虚拟机安装 Centos6
1 虚拟机安装 Centos6 1.1 前期配置 New Virtual Machine———Typical(典型安装) 选择镜像文件. 设置文件名,用户名和密码. 设置系统在虚拟机软件的名称, 设置 ...
- .net 读写记事本文件
这是读取文件的代码 StreamReader myreader = File.OpenText(_filepath);//读取记事本文件 string s = ""; s = my ...
- 16、总经理要阅读的书籍 - IT软件人员书籍系列文章
总经理是公司的一个领导角色.他主要负责公司级别的比如规划,战略等等内容.有些公司的总经理,比如软件公司的总经理,往往是一个大的业务员,将更多的大型的软件项目投取过来,让公司能够有钱赚,让公司员工能够跟 ...
- 字符输入流Reader简要概括
字符输入流Reader组成结构 本篇将对JAVA I/O流中的字符输入流Reader做个简单的概括: 总得来说,每个字符输入流类都有一个对应的用途,如下: 字符流基类:Reader 字节流转字符流:I ...
- JavaScript(二)——语法
1.基本数据类型: 字符串.小数.整数.日期时间.布尔型等. 2.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parseflo ...