Hello, I am having difficulty with part of the code. I've only been learning C++ for a week or two, so bear with me. :)
Basically, this is the start of a program that does the quadratic formula, and this part of the code covers the input for "a".
I want the code to do this:
If I type "0" or something non-valid such as "j" into the "a" input prompt, I want it to read-out the ERROR message and then bring me back to inputting another value for "a".
I used to have a "goto" pair of lines, but I feel like that will just get messy as I account for more Errors, so I tried to do a do/while loop that I recently read about in my C++ book.
using namespace std;
cout << "Enter a value for a: ";
cin >> a;
if (a == 0 || cin.fail())
cout << "ERROR: \"a\" must be a number and non-zero." << endl << endl;
while (a == 0 || !cin.fail());
With this code, when I type in "0" at the "a" input, it does what I want: It outputs the ERROR message and then brings me back to input another value for "a".
However, if I did a non-number character, such as "j", I get this message:
Which repeats forever. My question is why doesn't it let me go to the " cin >> " part after outputting the text once, allowing me type what "a" is again? Note that this happens whether or not I have the ! in front of cin.fail, so I think the problem is with my while clause, but I'm not sure.
Thanks for reading, I hope you can help. (Also, if there is a way to simplify or a better way to get same result, please do tell.)