关于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的更多相关文章

  1. C语言 scanf()和gets()函数的区别

    C语言 scanf()和gets()函数的区别 1.相同点:scanf( )函数和gets( )函数都可用于输入字符串 2.不同点:两者在功能上有所区别,具体区别如下: 要实现如下需求“从控制台输入字 ...

  2. C语言scanf与get char,gets的区别

    C语言scanf与get char,gets的区别 1.scanf() scanf是C语言的格式输入函数是通用终端格式化输入函数,它从标准输入设备(键盘) 读取输入的信息.可以读入任何固有类型的数据并 ...

  3. C语言scanf函数详细解释

    原文链接 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准 ...

  4. C语言Scanf函数

    C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...

  5. [转载]VS2012编译C语言scanf函数error的解决方法

    在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may ...

  6. C语言scanf函数详解

    函数名: scanf  功 能: 运行格式化输入  用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准输入设 ...

  7. c语言scanf详解

    函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...

  8. c语言:scanf()高级应用

    1) 指定读取长度 还记得在 printf() 中可以指定最小输出宽度吗?就是在格式控制符的中间加上一个数字,例如,%10d表示输出的整数至少占用 10 个字符的位置: 如果整数的宽度不足 10,那么 ...

  9. 为什么C语言Scanf函数对字符串不要加 取地址运算符&

    原文1:http://www.360doc.com/content/16/0515/11/19455598_559288667.shtml 原文2:https://zhidao.baidu.com/q ...

随机推荐

  1. 【代码笔记】iOS-浇花动画

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  2. 实现BaseFragment

    package liu.basedemo.base; import android.app.Activity; import android.content.Intent; import androi ...

  3. Swfit 字符与字符串

    Swfit 字符与字符串 OC 定义字符 char charValue = 'a'; Swift 定义字符 var charValue:Character = "a" Unicod ...

  4. 关于 python

    关于python的版本: python2.7  和 python3.0 有很大的不同,学习时应因目的而做出选择. 目前用到的python2.7最多, 而web3.0时代 则用到的python3.0 所 ...

  5. DbUtils是Apache出品一款简化JDBC开发的工具类

    DbUtils     - DbUtils是Apache出品一款简化JDBC开发的工具类     - 使用DbUtils可以让我们JDBC的开发更加简单     - DbUtils的使用:       ...

  6. 1、HTML学习 - IT软件人员学习系列文章

    本文做为<IT软件人员学习系列文章>的第一篇,将从最基本的开始进行描述,了解的人完全可以跳过本文(后面会介绍一些工具). 今天讲讲Web开发中最基础的内容:HTML(超文本标记语言).HT ...

  7. visual studio生成后调试启动又提示部分项目需要生成问题总结

    长久以来若干个项目都遇到过类似的情形,已经成功生成的项目启动调试或者再生成依然认为部分项目需要生成而不是跳过.总结以往的经验,记录下来,以便大家遇到时处理. 若有多个项目提示需要重新生成,优先检查被依 ...

  8. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

  9. Windows Phone Studio-任何人都能开发Windows Phone App的在线工具

    在一段时间的内测以后,微软于今天早些时候发布了其Windows Phone应用开发的在线工具,名字叫做Windows Phone Studio.其意义在于,通过简单的内容添加和样式选择,实现Windo ...

  10. 挖一挖C#中那些我们不常用的东西之系列(3)——StackTrace,Trim

    时间太快了,三月又要过去了,告别一下...继续期待生死未卜的四月,今天我们继续挖一挖. 一: Environment.StackTrace 可能我们看到最多的就是catch中的e参数,里面会有一个St ...