C++ 的一个问题!求助

来源:百度知道 编辑:UC知道 时间:2024/06/30 05:20:30
题目是 :

if语句测试:
定义一个int类型的变量age ,然后让用户输入自己的年龄,
如果age的值在0~12 之间,就输出“你是儿童!”
如果age的值在13~18 之间,就输出“你是青少年!”
如果age的值在19~60 之间,就输出“你是壮年!”
如果age的值在61~135 之间,就输出“你是一位老人!”
否则如果输入其它值,就输出“你的输入不合法!”

我做的答案是:

#include <stdio.h>
void main()
{
int age;
printf("请输入你的年龄");
scanf ("%d" , &age);
if (age <= 12)

printf("你是儿童!\n");

else if (age <= 18)

printf("你是青少年!\n");

else if (age <= 60)

printf("你是壮年!\n");

else if (age <= 135)

printf("你是位老人!\n");

else
printf("你输入了不合法的字符!\n");
}

我输入 10 提示:是儿童 !

输入 23 提示 是 壮年 !

上面的都对!

可是我输入 a、b、c等 英文字母时!却提示 :你是儿童!

应该是提示:“你输入了不合法的字符!”的啊!

我是编程初学者!请大家帮我看看哪点代码打错了 !

#include<iostream>
using namespace std;
void main()
{
int age;
cout<<"please enter a age ,the age between 0 and 135.";
cin>>age;
if(age>=0&&age<=12)
cout<<"you are a child"<<endl;
if(age>=13&&age<=18)
cout<<"you are a teenage"<<endl;
if(age>=19&&age<=60)
cout<<"you are a man"<<endl;
if(age>=61&&age<=135)
cout<<"you are an old man"<<endl;
if(age<0&&age>135)
cout<<"error"<<endl;
}

貌似这才是C++;

int age;
>>

unsigned int age;

输入 a、b、c时,age为负数

#include <stdio.h>
void main()
{
int age;
printf("请输入你的年龄");
scanf ("%d" , &age);
if (age<=0)
printf("你输入了不合法的字符!\n");
else if (age <= 12)