That is a typical problem that we all experience. It should be covered in Ivor Horton's Beginning C++, or other beginning C++ texts, but unfortunately it's not. It has to do with the way cin>> works. When these statements are executed:
cout << "\nEnter a number: ";
cin >> num;
The program stops and waits for you to enter the data. Then, you enter the data and hit return. If you enter 10 and hit return, this is what your program sees:
When you hit return a '\n' character is entered and that becomes part of your input. However, cin>> is defined to stop reading when it encounters a '\n' character. So, your program reads 10 into your variable num, and then things look like this:
Your program actually keeps track of where it stopped reading your input, and as far as it's concerned there's still input left to be read at some future date, namely the '\n' character. Then, execution continues to these lines:
cout << "\nEnter a string: ";
cin.getline(name, MAX, '\n');
cin.getline() is defined to read in input until it encounters a '\n' or MAX-1 characters, but it's also defined to read in the '\n'--unlike with cin>> which stops and doesn't read in the '\n'. Well, your program doesn't wait for you to input anything because it still has stuff to read--there is still a '\n' left over from when you used cin>>. So, cin.getline() happily reads that '\n', which is also its signal to stop reading, and as far as your program is concerned cin.getline(name, MAX, '\n'); has done its job and it's time to move on. Therefore, your program continues execution making it seem like it skipped right by that line.
The solution? There is a function called ignore() that you can use:
That ingnores the number of characters you indicate between the parentheses with the default being 1. So, the above line will ignore one character. If you include that after your cin>> line, then your program will see this:
and then when it comes to this line:
cin.getline(name, MAX, '\n');
There won't be any data for it to read, so it will wait for you to enter data.
The conclusion of that long explanation is that whenever you use cin>> and then use cin.getline() you need to use cin.ignore() after the cin>> line.
Note: That may lead you to wonder why these lines work(you can verify yourself that you can use cin>> twice in a row and the data will be read in correctly):
cout<<"Enter a number: ";
cout<<"Enter a second number: "
If you enter a 10 and hit return after the first cout<< statement, your program will see this:
and cin>> will read in the 10, leaving this:
Then the second cout<< and cin>> will be executed, but since cin>> is required to stop at '\n' how does it get by the '\n' and correctly read in the second number you enter:
The answer is that cin>> is defined to skip a leading
'\n', but stop at a trailing '\n'. The '\n' left over from the first cin>> is now a leading character(i.e nothing has been read in prior to it by the current cin>> statement) so it is skipped, and the program waits for you to enter the next number.