c - 如何轻松检查输入是否为 char 而不是运行它?
问题描述
这段代码几乎按照我喜欢的方式运行,但唯一的问题是当我使用 char 作为输入时,它会进入无限循环。我怎样才能解决这个问题?
#include <stdio.h>
#include <stdlib.h>
int main(){
int year;
int age;
printf("This program will calculate how old you are \n\n");
//sleep(2);
do
{
printf("what year are you born in?\n");
scanf("%d", &year);
age = 2020 - year;
if(year >= 1910 && year <= 2020){
printf("You will or have turned %d year this year \n\n", age);
//sleep(1);
}else if (year == 0){
printf("Program exits \n");
}else{
printf("write a valid input between 1910 and 2020\n\n");
}
}while (year != 0);
//system("PAUSE");
return 0;
}
解决方案
推荐阅读
- javascript - 如何从 HTML 选择选项填充对象的 javascript 数组
- javascript - 有谁知道如何使用 React Express 解决 CORS 问题?
- python - 使用 GEKKO 求解二维热方程
- javascript - 无法使用循环 Jquery 获取按钮值
- javascript - 如何保留掉落物品的位置?
- server - 在实时服务器上维护 Laravel 8 代码的最佳方法?
- javascript - 用于随机排列数组的异步函数
- drupal - Drupal 7 模块升级到 Drupal 9
- docker - Openshift HTTP 在 60 秒后超时
- python - Request-html 每次调用函数时获取数据