#include <iostream> using namespace std; int main() { char name[20]; char gender; int age; cout << "请输入姓名、性别(M/F)和年龄:"<<endl; cin >> name >> gender >> age; cout << "name :"<<name <<endl; cout << "sex:"<<gender <<endl; cout << "birthday:"<<2014-age<<endl;
}
ot@embedded c++]# g++ name.cc [root@embedded c++]# ./a.out 请输入姓名、性别(M/F)和年龄: 张三 M 22 name :张三 sex:M birthday:1992 [root@embedded c++]#
假设年龄小于0怎么办呢?
{ char name[20]; char gender; int age; cout << "请输入姓名、性别(M/F)和年龄:"<<endl; cin >> name >> gender >> age; cout << "name :"<<name <<endl; cout << "sex:"<<gender <<endl; if(age>=0) cout << "出生年份:"<<2014-age<<endl; if(age <0) { cout << "你来自未来??"<<endl; cout <<"出生于"<<-age<<"年之后!"<<endl; } }
t@embedded c++]# ./a.out 请输入姓名、性别(M/F)和年龄: 机器猫 M -10 name :机器猫 sex:M 你来自未来?? 出生于10年之后!