For example: Enter the C ++ programing
Run the code
{
int a;
string nameOfCourse;
GradeBook myGradeBook;//create an object, myGradeBook
cout << "Please enter the name of this grade book ";
cin >> nameOfCourse;
myGradeBook.setCourseName(nameOfCourse);
myGradeBook.dispiay();
return 0;
}
Result
modified the code
int main()
{
int a;
string nameOfCourse;
GradeBook myGradeBook;//create an object, myGradeBook
cout << "Please enter the name of this grade book ";
getline(cin, nameOfCourse);// >> nameOfCourse;
myGradeBook.setCourseName(nameOfCourse);
myGradeBook.dispiay();
return 0;
}
After the result is modified
so
string using the declared string
getline(cin, nameOfCourse);
prompt
cin.getline()//其适用于char声明的字符串,不适用于string声明的字符串,