Dan wrote:
I have a very simple validation function, lets say for negative number:
The function is called during a series of questions,
Besides doing exit like I did, is there an easy way so that the program
ask the question again or go back to the previous, accounting that there
are menus
D
int Validate_positive(float &c)
{
if (c < 0)
{ cout<< "You have type a negative integer, Please try again with a
positive number! " <<endl;
exit(1); } //exits the program and starts over with a positive number
return 0;
}
Hi,
this routine seems to be the wrong place to do the fix. Somewhere in your
code there is probably a part that looks like this:
...
std::cout << "Please enter a non-negative number: "
<< std::endl;
std::cin >> my_very_dear_non_negative_float;
if ( Validate_positive( my_very_dear_non_negative_float ) {
...
}
Now, if you want to pester the user until a non-negative number is entered,
you will need a loop:
...
std::cout << "Please enter a non-negative number" << std::endl;
std::cin >> my_very_dear_non_negative_float;
while ( my_very_dear_non_negative_float < 0 ) {
std::cout << "Can't you read, I said non-negative! Try again dummy: "
<< std::endl;
std::cin >> my_very_dear_non_negative_float;
}
...
Best
Kai-Uwe