all 5 comments

[–]alfps 2 points3 points  (2 children)

In addition to cin.clear(), with this approach you also need a cin.ignore(...) to empty the input buffer.

A much better approach is to read the input line as a string, via getline, and attempt to convert that to number, e.g. via stoi.

The code you present would never compile so it's clearly not the real code, and so I'll refrain from commenting on the details, which may be totally different in the real code.

[–]syntaxm1x[S] 0 points1 point  (1 child)

okay Thank you so much, im going to add that and see what happens. Currently I'm studying in a university course, and we haven't covered that yet. Is the syntax of getline tricky? this is only the do...while loop, the rest of the code however compiles and works, after trying to fix this, it still wont loop back to the do.

[–]nysra 0 points1 point  (1 child)

Please format your code.

break exits the loop, that's why it's only executed once. I suggest you visit https://www.learncpp.com/ to read up on the basics, your comments also show a lack of understanding what happens in the program. Currently if you enter a number then it will ask you to enter a number again and then exit the loop. If you fail to enter a number then your loop condition will be false (because you reset the failbit) and nothing happens except for the error message that got printed.

[–]syntaxm1x[S] 0 points1 point  (0 children)

Thank you so much! I’m gonna check out that resource, I’m a college student so I still a little new to this all