operator>> performs formatted input. That is, it interprets the data read in context of its right-hand operand. It skips over and discards whitespaces and stops at newline but leaves it in the input buffer where it [newline] waits to be read. The next read from input buffer with operator>> will discard the newline left in there, but other member functions of cin that perform unformatted input will read it, resulting in unexpected behavior.
Originally Posted by PersianStyle
Adding std::cin.ignore() after std::cin >> age; will read and discard the next character from the input buffer, '\n' in this case, getline() will have nothing to read and will wait for input from user.
int main(int argc, char** argv)
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Enter your name: ";
std::cin.getline(name, 256); //default delimiter is '\n', so this will result in a null string
std::cout << "Your name is " << name << ", your age is "
<< age << ".\n";
I personally prefer std::cin.sync(), but that might have side-effects I'm not aware of - I'm but a newbie myself, and if just said something incredibly stupid I expect someone to come along and smack me around.