The way I recommend is to read the line into a string, then parse the string to extract the specific input. This way, you don't need to "throw away extra data" because the next read will read the next line, rather than whatever is left on the line. This also avoids the problem where you read into an int, then try to read the next line into a string, only to find that you read nothing into the string because you ended up reading the rest of the previous line instead of the next line.
Originally Posted by aaleclaire
Unfortunately, doing this, while simple, requires more stuff that you probably don't know, unlike manasij7479's suggestion, e.g.,
// Headers to include
// Read a line from standard input.
// Create an input string stream from the line to extract the int
if ((ss >> my_int) && ss.eof())
// Use my_int
// Could not extract the int, or there was invalid input after extraction.