C++ IO流
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main(){
int age;
//standard input(cin)
cout<<"Please enter an integer value as your age: ";
cin>>age;
cout<<"Your ager is: "<<age<< endl; //endl没有去掉回车, 因为输入和输出用的是不同的缓冲区
//cin and string
string mystr;
cout<<"What's your name? ";
getline(cin,mystr); //接收了前面留在缓冲区的回车,会直接打印"Hello"
cout<<"Hello,"<<mystr<<endl;
char sex;
cout<<"Please enter a F or M as your sex: "<<endl;
cin>>sex;
cout<<"Your sex is: "<<sex<<endl;
cout<<"What's your favorite team? ";
getline(cin,mystr);//ditto, 会直接打印"I like"
cout<<"I like "<<mystr<<".\n";
return 0;
}
解决办法: 在两处getline下面再添加 cin.get()以接收回车