求教,为什么字符串变量遇到空格中断输出?如何解决?
程序代码:
//输入部分: cout<<"address:"; cin>>addr; //输出部分: cout<<"address:"<<addr<<endl; //情况: //input:Shanghai Road //output:Shanghai
[ 本帖最后由 日寂 于 2014-4-5 16:09 编辑 ]
#include<iostream> #include<string> using namespace std; class Student { protected: int num; string name; char sex; public: void set() { cout<<"num:"; cin>>num; cout<<"name:"; cin>>name; cout<<"sex:"; cin>>sex; } void display() { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } ~Student(){}; }; class Student1:public Student { private: int age; string addr; public: void set_1() { set(); cout<<"age:"; cin>>age; cout<<"address:";//调用set_1时运行到这里停止 getline(cin,addr); } void display_1() { display(); cout<<"age:"<<age<<endl; cout<<"address:"<<addr<<endl; } ~Student1(){} }; int main() { Student1 s; s.set_1();//在输出"address:"后开始执行接下来的语句 putchar(10); s.display_1(); return 0; }