2017-10-05 113 views
0
//class 
class student 
{ 
    public: 
    int rno; 
    string name; 
    int marks; 
    int ran; 

    void getinfo() 
    { a: 
     cout<<"\t \tenter the roll number"<<endl; 
     cin>>rno; 
     cout<<"\t \tenter the name"<<endl; 
     getline(cin,name); 
     cout<<"\t \tenter the marks"<<endl; 
     cin>>marks; 
    } 
    void showinfo() 
    { 

     cout<<"\t"<<ran<<"\t "<<rno<<" \t\t"<<name<<" \t\t"<<marks<<endl<<endl; 
    } 

}; 

当我给控制台中的一个对象的输入后,为滚动输入它没有打印“输入名称”,然后没有任何机会给它的输入显示“输入标记”的下一个打印语句。是否有任何理由为什么getline语句没有从控制台获取输入?getline语句没有得到输入

+0

使用'cin.ignore()' –

回答

1

cin会在缓冲区中留下新行。因此,当从cin得到rno时,cin缓冲区中实际上还有一个\n。当你去读这个名字时,它只是抓住了\n并立即返回。 在第一个cin后应该执行类似cin.ignore();的操作应该清除缓冲区并允许您正确读取用户输入。