Please Help Newbie with validation problem

M

Morgan

Hi, newbie coder here

I've been working on this code for about 2 days now, and can't get it
to work. Ultimately I need a algorithm to loop between my pieces of
validation code so that a variable meets 3 different conditions.

What I have is three while loops, example:

cout << "Please enter the number of judges: ";

/*
The reason that there are two variables for the number of judges,
is because I need to find out if the user entered a floating point
value. You shouldn't have 3.5 judges, as the for loop which counts
the scores later on will never be able to reach the end condition.
What is happening is that I get floating point value of the number
of judges (num_judges_float). If the user entered an integer eg: 4,
and if you put that value into an integer variable (num_judges_int),
you just get 4. So if you minus the floating point value of judges
from the number of judges in the integer variable, you should yield
zero. Anything else means the user did not enter a integer so a while
loop traps the error.
*/

cin >> num_judges_float;
num_judges_int = num_judges_float;

while((num_judges_float - num_judges_int) > 0)
{
cout << "\nError: You entered the number of judges with a
decimal point.\n\n";
cout << "Please enter the number of judges with no decimal
point: ";

cin >> num_judges_float;
num_judges_int = num_judges_float;
header();
}

// The header() function is called whenever input is given by the
// user. This produces the affect of keeping the text banner at the
// top of the console window the whole time.

header();

while(num_judges_int < 1)
{
// can't have negative judges or zero judges!

cout << "Error: The number of judges cannot be negative or
zero : ";
cout << "\n\nPlease reenter the number of judges : ";
cin >> num_judges_int;
header();
}

while(num_judges_int < 3)
{
/*
We aren't able to compute an average in the case that the number
of judges is 1 or 2. This is because later in this program
we have
to take the highest and lowest scores from the total of
scores.
If we do this we have to divide the remaining total by the
number
of judges minus two to get an average.(two for the two
scores we're
getting rid of). In the case we have two judges, we would wind up
dividing the total into zero (impossible) or in the case that we
had only one judge, we would be dividing the total into a
negative
number which would then give a negative result. As these events
don't lead to an average, it seemed best to me to give the user
an error message to explain what was wrong.
*/

cout << "Sorry, this program cannot give an average if there
is less than three judges";
cout << "\n\nPlease reenter the number of judges : ";
cin >> num_judges_int;
header();
}
 
O

osmium

Morgan said:
I've been working on this code for about 2 days now, and can't get it
to work. Ultimately I need a algorithm to loop between my pieces of
validation code so that a variable meets 3 different conditions.

What I have is three while loops, example:

cout << "Please enter the number of judges: ";

/*
The reason that there are two variables for the number of judges,
is because I need to find out if the user entered a floating point
value. You shouldn't have 3.5 judges, as the for loop which counts
the scores later on will never be able to reach the end condition.
What is happening is that I get floating point value of the number
of judges (num_judges_float). If the user entered an integer eg: 4,
and if you put that value into an integer variable (num_judges_int),
you just get 4. So if you minus the floating point value of judges
from the number of judges in the integer variable, you should yield
zero. Anything else means the user did not enter a integer so a while
loop traps the error.
*/

cin >> num_judges_float;
num_judges_int = num_judges_float;

while((num_judges_float - num_judges_int) > 0)
{
cout << "\nError: You entered the number of judges with a
decimal point.\n\n";
cout << "Please enter the number of judges with no decimal
point: ";

cin >> num_judges_float;
num_judges_int = num_judges_float;
header();
}

// The header() function is called whenever input is given by the
// user. This produces the affect of keeping the text banner at the
// top of the console window the whole time.

header();

while(num_judges_int < 1)
{
// can't have negative judges or zero judges!

cout << "Error: The number of judges cannot be negative or
zero : ";
cout << "\n\nPlease reenter the number of judges : ";
cin >> num_judges_int;
header();
}

while(num_judges_int < 3)
{
/*
We aren't able to compute an average in the case that the number
of judges is 1 or 2. This is because later in this program
we have
to take the highest and lowest scores from the total of
scores.
If we do this we have to divide the remaining total by the
number
of judges minus two to get an average.(two for the two
scores we're
getting rid of). In the case we have two judges, we would wind up
dividing the total into zero (impossible) or in the case that we
had only one judge, we would be dividing the total into a
negative
number which would then give a negative result. As these events
don't lead to an average, it seemed best to me to give the user
an error message to explain what was wrong.
*/

cout << "Sorry, this program cannot give an average if there
is less than three judges";
cout << "\n\nPlease reenter the number of judges : ";
cin >> num_judges_int;
header();
}

I suggest rewriting so you have a function that gets one satisfactory number
and returns it. Call this function three times from main. A satisfactory
number is a positive integer, but you have divided the responsibility into
two parts and the input must meet both criteria. Since the while loops are
disjoint in your code, problems seem likely.
 
M

Morgan

I suggest rewriting so you have a function that gets one satisfactory number
and returns it. Call this function three times from main. A satisfactory
number is a positive integer, but you have divided the responsibility into
two parts and the input must meet both criteria. Since the while loops are
disjoint in your code, problems seem likely.

Yes, ben suggested something along these lines and it worked nicely.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top