Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When you enter a letter or any character for an int variable, does the input str

ID: 3734829 • Letter: W

Question

When you enter a letter or any character for an int variable, does the input stream enter the failed state? How do you fix it so the program continues? • Please give all necessary explanations for “failed state”. • What is the syntax needed to fix the failed state? • Please give an example of the situation. When you enter a letter or any character for an int variable, does the input stream enter the failed state? How do you fix it so the program continues? • Please give all necessary explanations for “failed state”. • What is the syntax needed to fix the failed state? • Please give an example of the situation. When you enter a letter or any character for an int variable, does the input stream enter the failed state? How do you fix it so the program continues? • Please give all necessary explanations for “failed state”. • What is the syntax needed to fix the failed state? • Please give an example of the situation.

Explanation / Answer

whenever an operation fails further operations on stream are suspended .a non fatal error occured when the user entered letters when the program was expecting an integer .

cin.fail() : it detects whether the value entered gets fits the in the value defined in the variable.cin.fail() is always true if some error occured   with the stream .

Syntax:

But if cin.fail() is true, it means that


this is how we can fix this problem of failed state:


In exampe, we have the result that, after entering the wrong value, our stream is broken. But we do not repair the stream and did not clear the buffer. So, no matter how often the loop will be made, "cin >> number" will always get /extract the FIRST value which we have entered after starting the programm. And thus, it will always return a cin.fail() and our loop can not be left by any value which we will enter afterwards.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote