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;
    }

};

when i getting the input for an object in the console after giving the input for roll no it is printing "enter the name" and then without any chance to give the input it is displaying the next print statement that is "enter the marks". is there any reason why the getline statement is not getting the input from the console??

1 Answers1

1

cin will leave new lines in the buffer. Therefore when you get rno from cin there is actually an \n left in the cin buffer. When you go to read the name it just grabs the \n and instantly returns. Doing something like cin.ignore(); after the first cin should clear the buffer and allow you to correctly read the user input.

kreynolds
  • 406
  • 4
  • 15