| Victor714 (22) | |||
All declarations are given on my assignment but i dont understand what do i need to do with bool correct;Here is what i have so far in my code
| |||
|
Last edited on
|
|||
| Raezzor (230) | |
| Without knowing your assignment we have no idea what you are supposed to do with it either. Only thing I could possibly see if using it to confirm that the user entered a proper input for each question. However if that's the intention of the assignment it would seem a bit out of the scope of this exercise since it would most likely require some kind of conditional expression. | |
|
Last edited on
|
|
| Victor714 (22) | |
|
I need to ask the user for input and loop until he enters valid input.I need to ask a yes or no question (y/n) and then check if the input is valid. then ask the user to enter the age.And again if the user does not enter a number for the age question, it will prompt the user to enter valid input the output should look like this Do you like the color blue?(y/n) : x incorrect value Do you like the color blue? (y/n): n Enter age as integer: abc You must enter an integer Enter age as integer: 20 Thank you for your input! Press key to continue | |
|
Last edited on
|
|
| Victor714 (22) | |||
I believe i got the first part, now im stuck with the age
| |||
|
Last edited on
|
|||
| LowestOne (768) | |
|
Two ways. 1) extract the age as a string, check that the string is a number, and then convert the string to an integer. 2) extract the age as an int and check to see that the extraction was successful. I would prefer method 2. Cin is an istream object, and has many member functions other than >>. To see all of them: http://www.cplusplus.com/reference/iostream/istream/ When you use >> to extract a number, and the user enters letters, you set a fail bit on the cin object. You will want to extract the number and check if cin is good. If it isn't, clear it, ignore the input (it would still be there), and try again. | |
|
|
|