DEV Community

Denys Shabelnyk
Denys Shabelnyk

Posted on

trap for beginners in C++

Following one of C++ course I found one interesting example how to correctly use combination std::cin together with std::getline().
Look at firts example below:

What do you think the output will be ?
Your age is
Your name is
but not. Output is below:

To avoid this strange behaviour always use std::cin.get() before std::cin and std::getline() like on the code snippet below:

Now you will get correct output.

Be careful and happy coding!

Top comments (0)