Which function is commonly used to take input from the user in C++?
Analysis & Theory
`cin` is the standard way to take input from the user in C++.
What happens if the user enters a letter when an integer is expected by `cin`?
B
The input is ignored and cin enters fail state
C
It is automatically converted
D
The program skips the input
Analysis & Theory
Invalid input causes `cin` to enter a fail state and further inputs are blocked until cleared.
Which function can be used to clear the error state of `cin`?
Analysis & Theory
`cin.clear()` resets the error state of the input stream.
Which function is used to discard invalid input characters in C++?
Analysis & Theory
`cin.ignore()` skips characters in the input buffer, helping clean up after invalid input.
How do you check if the previous input failed in C++?
Analysis & Theory
`cin.fail()` returns true if the previous input operation failed.
What does the following code do?
`if (cin.fail())`
A
Checks if the last input succeeded
B
Checks if cin is working
C
Checks if the last input failed
D
Clears the input buffer
Analysis & Theory
The function `cin.fail()` returns true if the last input was invalid.
Which of the following is the correct way to validate integer input?
Analysis & Theory
`cin.fail()` is used to check for invalid input, especially with wrong types.
What is a good practice after clearing a fail state in C++ input?
B
Call cin.ignore() to discard remaining input
Analysis & Theory
After clearing the error with `cin.clear()`, you should call `cin.ignore()` to discard invalid input.
How many characters does `cin.ignore(1000, '\n')` ignore?
A
Until 1000 characters or a newline is found
C
Exactly 1000 characters
D
All input in the buffer
Analysis & Theory
`cin.ignore(1000, '\n')` skips up to 1000 characters or until a newline is found—whichever comes first.
What is the purpose of input validation?
B
To catch logical errors
C
To ensure correct and safe user input
Analysis & Theory
Input validation ensures the program receives correct and expected input, preventing runtime errors.