Perhaps a different order in the logic? After all, we have faith in the user to type the right thing on first try (have we?):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
double
input_prot( string prompt, double upper, string err, double lower )
{
double input;
while (true) {
cout << prompt;
if ( cin >> input ) {
if ( /*input is within limits*/ ) {
return input;
}
else {
// input not in range. Stream is ok.
}
}
else {
// invalid input, the stream is in error state
}
}
}
|
PS. My previous post was a hint about style.
Equality should be symmetric, i.e. if a==b, then b==a too.
Therefore, when you test foo==true, you can as well write true==foo.
In the latter form the left side is const and typing an assignment by mistake becomes a hard error.
A good compiler does warn when it sees an assignment within conditional expression. Does yours?
PS2. Another style detail. On custom types that have relational operators, canonical approach is to write < and == for them, and let the standard library templates to form the other operators for them. Therefore, > could be a bit more expensive than <. In case of double there is no difference, but being systematic is a good thing.