-4

I'm having a problem where whenever I run this code and put a space in studentName input it the program skips the next question and then ends.

This is what it looks like when I have a space in the student's name:

https://i.stack.imgur.com/AV6po.png

And this is what its supposed to look like:

https://i.stack.imgur.com/6vGCe.png

And this is my code:

    string className;
    int classTime;
    string studentName;
    cout<< setw(50);
    cout << "Enter the students name: ";
    cin >> studentName;
    cout << "Enter first class name: ";
    cin >> className;
    cout << "---------------------------------------------------------------
    ----------" << endl;
    cout << "|" << studentName << "|Monday |Tuesday|Wensday  |Thursday|Friday|Saturday|Sunday|" << endl;
    cout << "-------------------------------------------------------------------------" << endl;
    cout << "|   9:00-10:00 |       |       |         |        |      |        |      |" << endl;
    cout << "-------------------------------------------------------------------------" << endl;
    cout << "|   10:00-11:00|       |       |         |        |      |        |      |" << endl;
    cout << "-------------------------------------------------------------------------" << endl;
    cout << "|   11:00-12:00|       |       |" << className << "|        |      |        |      |" << endl;
halfer
  • 19,824
  • 17
  • 99
  • 186
Jack.B
  • 11

1 Answers1

1

Try using std::getline to read in names with spaces.

The cin will finish string input when it encounters a space.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154