>> is the extraction operator. When we write std::cin >> rec ; , the value from the input stream buffer std::cin is extracted and stored in the variable rec.
Similarly, << is the insertion operator. When we write std::cout << rec ; , the value of rec is inserted to the output stream buffer std::cout.
IMHO !(std::cin >> rec.sale) and (rec.sale < 0) are two different conditions of bad input and need to be handled differently.
For the former case cin flags need to be cleared and bad input needs to be ignored before reading further input.
This does not need to be done for the latter case.
IMHO !(std::cin >> rec.sale) and (rec.sale < 0) are two different conditions of bad input and need to be handled differently.
Except that they don't need to be handled differently. They could be, of course, but you lose nothing by handling them in exactly the same manner. And, of course, there's nothing stopping one from differentiating between the two within the body of the loop and handling them differently.
(And, by treating them the same way, we handle the input "-1asdfasdf" in only one iteration of the loop.)
First it executes cin >> rec.sale; (read from the input buffer)
If there is no error in the read operation, it outputs "true" otherwise it outputs "false". This output is then caught by our if statement.
If you want, you can separate these two statements.
>Also, where do I put the cin?
(I don't understand your question, you already have cin in the if statement)
How to debug this?
It says redeclaration of "bool valid".
Because if I take out the "bool valid" on the second one. It will not allow me to input anything when I run it.