cin as the end of the space, causing the contents of the input can not be fully displayed.

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
Here Insert Picture Descriptionmodified 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
Here Insert Picture Descriptionso
string using the declared string

    getline(cin, nameOfCourse);

prompt

cin.getline()//其适用于char声明的字符串,不适用于string声明的字符串,
Published 10 original articles · won praise 0 · Views 26

Guess you like

Origin blog.csdn.net/qq_44933833/article/details/104909909