#include <iostream>
using namespace std;
int main(){
int age;
char name[20] ;
cout << "enter your age >";
cin >> age;
cin.get(); //读取流中残余的回车,以便getline能正常工作
// cin.ignore(20,'/n'); //忽略20个字符或者碰到回车,从流中清除
cout << "enter your name >";
cin.getline(name,20);
cout << "your age is :" << age << endl;
cout << "your name is :" << name << endl;
}
本文展示了一个简单的C++程序,该程序通过标准输入获取用户的年龄和姓名,并将其输出到屏幕上。程序首先提示用户输入年龄,然后使用cin接收整数类型的年龄数据;接下来提示用户输入姓名,使用cin.getline()来读取字符串。最后将获取的数据打印出来。
212

被折叠的 条评论
为什么被折叠?



