No, you should place cin.ignore() immediately after the
cin >> CourseCode;
statement.
The input comes as a sequence of characters, like
The following code will fail to read it:
1 2 3 4
|
cout << "How old are you? ";
cin >> age;
cout << "What is your name? ";
getline( cin, name );
|
That is because the >> is for reading
formatted input, and it doesn't read
anything more than what it was asked to read.
What this means is that using
cin >> age
reads "34" from the input, and leaves "\nJane\n". Calling
getline() on that returns you the string "" and leaves "Jane\n" in the input.
Now, you may want to argue that the user only typed "34" in response to the first question. That isn't quite true. The user typed "34" and
pressed the ENTER key. This is very important to remember:
The user will always press ENTER at the end of every input.
A good rule of thumb is not to mix formatted and unformatted input methods (>> and getline()). Stick to one or the other. But if you must mix, make sure to do it the Right Way.
1 2
|
cin >> CourseCode;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
|
Here is a good thread to read through to understand better.
http://www.cplusplus.com/forum/beginner/18258/#msg92955
Make sure to follow the links given there also.
Good luck!