validation

D

Dan

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;
}
 
K

Kai-Uwe Bux

Dan said:
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
 
M

Mark A. Gibbs

Kai-Uwe Bux said:
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;
}

charming ui you have there ^_^

indi
 
P

Phlip

Mark said:
charming ui you have there ^_^

The snotty CMD.EXE admonition, "'blah' is not recognized as an internal or
external command, operable program or batch file," always reminds me of
Lilly Tomlin portraying a snotty telephone operator...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top