c++ - Read a line from the console -
i reading several string such name , surname , student number , grades, first 3 have done follows:
cout<<"enter student's first name: "<<endl; string name; cin>>name; cout<<"enter student's last name: "<<endl; string surname; cin>>surname; cout<<"enter student's unique student number: "<<endl; string studentno; cin>>studentno;
how grades enter in following manner : " 90 78 65 33 22" , want read entire line of grade string variable. these string used construct student object.
how achieve this, tried using getline() not work.
my attempt:
int main(){ cout<<"enter student's first name: "<<endl; string name; cin>>name; cout<<"enter student's last name: "<<endl; string surname; cin>>surname; cout<<"enter student's unique student number: "<<endl; string studentno; cin>>studentno; string classrcd; std::getline(cin , classrcd); db.add_student( name , surname , studentno , classrcd); /*creates student object , add list in db of type database*/ clear(); //clears screen in while loop return 0; }
you still have new line in stream after cin>>studentno
, giving empty string classrcd
.
you solve adding getline()
call after cin>>studentno
, leaving alone result, or ignore new line std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Comments
Post a Comment