Please Help Newbie ( While Loop 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.

I don't want my homework done for me, I would like a system, an
algorithm so I can figure this out. Quite honestly I've tried
everything (very little!) I know, I've functionalised the code, I've
used loops within loops, segmented the code so that the error messages
and validation and input occur separately.

I see what the problem is, obviously the code needs a way of referring
back to the other validation code when a variable succeeds the test I
give it. But I'm all out of ideas :-(

What I have is three while loops, example:

code below:

///////////////////////////////////////////////////////////////////////////

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

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();
// just sticks a ascii banner on top of console window
}

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();
}

Thanks for any help, I'd greatly appreciate it! :)
 
G

Grizlyk

Morgan said:
code below:

///////////////////////////////////////////////////////////////////////////

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

It can not be compiled outside of function, post a part of code, that you
try to compile.
 
J

John Carson

Morgan said:
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.

I don't want my homework done for me, I would like a system, an
algorithm so I can figure this out. Quite honestly I've tried
everything (very little!) I know, I've functionalised the code, I've
used loops within loops, segmented the code so that the error messages
and validation and input occur separately.

I see what the problem is, obviously the code needs a way of referring
back to the other validation code when a variable succeeds the test I
give it. But I'm all out of ideas :-(

What I have is three while loops, example:

The second is superflous since it checks a condition covered by the third.
code below:

///////////////////////////////////////////////////////////////////////////

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

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();
// just sticks a ascii banner on top of console window
}

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();
}

Thanks for any help, I'd greatly appreciate it! :)

You could do it in a couple of ways. while() loops can test multiple
conditions. Thus you can have (omitting the superfluous second condition)

while(((num_judges_float - num_judges_int) > 0 || num_judges_int < 3)
{
// stuff
}

You therefore continue in the while loop for as long as at least one
condition fails. Inside the while loop, you can do if tests for each failure
and issue an appropriate message.
 
B

benben

Morgan said:
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.

You shouldn't really have three loops each keeps asking for new values.
This is because simply by allowing a loop to update a variable you are
allowing the variable to betray the previous loop. To see my point,
follow my comment on your code below

So a correct algorithm is:

while (true)
{

double x;
cin >> x;

if (first_check(x) && second_check(x) && third_check(x))
break;
}
I don't want my homework done for me, I would like a system, an
algorithm so I can figure this out. Quite honestly I've tried
everything (very little!) I know, I've functionalised the code, I've
used loops within loops, segmented the code so that the error messages
and validation and input occur separately.

I don't see your code "functionalised". What you need to do is to
localize each check so they are self-contained. The functions given in
the previous example--first_check, second_check and third_check can be
managed as:

bool first_check(double x)
{
// return true if x is valid
// ...
}

etc.

NOTE: to increase readability you may want to change names like
first_check() to something more sensible like is_integer(), etc.
I see what the problem is, obviously the code needs a way of referring
back to the other validation code when a variable succeeds the test I
give it. But I'm all out of ideas :-(

What I have is three while loops, example:

code below:

///////////////////////////////////////////////////////////////////////////

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

cin >> num_judges_float;

num_judges_int = num_judges_float;

Below is the first check, making sure the variable is integer
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();
// just sticks a ascii banner on top of console window
}

Below is the second check, making sure the variable is positive non-zero.
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();
}

However, by the end of this loop, you will have a positive, non-zero
number but it may not be integer.
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();
}

Thanks for any help, I'd greatly appreciate it! :)

Ben
 
M

Morgan

It can not be compiled outside of function, post a part of code, that you
try to compile.

Ok, I'll post the entire code, its not too long (if you get rid of my
very long comments!)

code >>

//////////////////////////////////////////////////////////

// Header files to include useful code such as the
// clrscr() (clear screen of text) function.

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

// Prototype declaration for a user defined function.

void header(void);

int main()
{
/*
Here seven variables are declared, six floating point variables and
one integer variable. One of the floats, total, is also intialised
to zero. This is so I don't wind up adding an undefined value in
memory
to a number later in the code. The other variables have their values
assigned to them, so there is no issue of using undefined values.
*/

float score, highest_score, lowest_score, average,num_judges_float,
total = 0;
int num_judges_int;

header();
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();
}

cout << "Please enter " << num_judges_int << " scores";



for(int i = 0; i < num_judges_int; i++)
{
// This loop repeats until it has all the scores. If there
// are five judges,the loop will ask for five scores.

cout << "\n\nScore " << (i+1) <<" : ";
cin >> score;

header();

while(score < 0 || score > 6)
{
// Checks that the scores are between zero and six.
// It continues to ask for a correct score if the
// user makes successive mistakes.

cout << "\nError: Scores must be between 0 and 6.";
cout << "\n\nPlease reenter the score : ";
cin >> score;
header();
}

// Here the total variable adds up all the correctly
// entered scores.

total += score;

if(i == 0)
{
// Runs the first time that the for loop runs only.
// These variables are intialised from user input,
// this is because otherwise I would have to intialise
// highest_score to zero and lowest_score to six.
While
// that would work, it would also be inflexible,
whereas
// this algorithm to find a highest number and the
lowest
// number in a set of numbers will work in other
programs.

highest_score = score;
lowest_score = score;
}

// Gets highest score.
if(score > highest_score)
highest_score = score;

// Gets lowest score.
if(score < lowest_score)
lowest_score = score;
}

cout << "Lowest Score: " << lowest_score << endl;
cout << "Highest Score: " << highest_score << endl << endl;

// Subtracting highest & lowest score from total.

total = total - (highest_score + lowest_score);

// Gets average.

average = total / (num_judges_int - 2);

// Sets the number of significant digits to output.

cout.precision(2);

cout << "Average: " << average << endl << endl;
cout << "Press any key to end this program";

// getch() halts program and waits for a key stroke to go on executing
code.

getch();
return 0;
}

void header(void)
{
// clear screen of text.

clrscr();

// put in a heading/banner

cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << endl;
cout << "\t\t Celebrity Ice-Skating \n";
cout << endl;
cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
}
 
M

Morgan

The second is superflous since it checks a condition covered by the third.

Sure, I appreciate that, I was trying to give the user different error
messages though.
You could do it in a couple of ways. while() loops can test multiple
conditions. Thus you can have (omitting the superfluous second condition)

while(((num_judges_float - num_judges_int) > 0 || num_judges_int < 3)
{
// stuff

}

You therefore continue in the while loop for as long as at least one
condition fails. Inside the while loop, you can do if tests for each failure
and issue an appropriate message.

Sorry John, but I tried exactly that and it didn't work. However I see
what you mean. Tell you what, I'll post the code like you said as
above,

and maybe there was a problem with it I didn't see the first time I
tried that method of doing it.

I'll post the entire code with the changes you suggested, so it will
work in your complier. (in a separate post, it will be little bit long
I'm

afraid)
 
M

Morgan

ALTERED CODE below for John Carson:

BTW: To recieve lots of errors, try typing in eg: zero, followed by a
number with a decimal point.

-------------------------------------------------

// Header files to include useful code such as the
// clrscr() (clear screen of text) function.

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

// Prototype declaration for a user defined function.

void header(void);

int main()
{
/*
Here seven variables are declared, six floating point variables and
one integer variable. One of the floats, total, is also intialised
to zero. This is so I don't wind up adding an undefined value in
memory
to a number later in the code. The other variables have their values
assigned to them, so there is no issue of using undefined values.
*/

float score, highest_score, lowest_score, average,num_judges_float,
total = 0;
int num_judges_int;

header();
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 ||
(num_judges_int < 3))
{

if((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();
}
else if(num_judges_int < 3)
{
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();
}
}

// 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();

cout << "Please enter " << num_judges_int << " scores";



for(int i = 0; i < num_judges_int; i++)
{
// This loop repeats until it has all the scores. If there
// are five judges,the loop will ask for five scores.

cout << "\n\nScore " << (i+1) <<" : ";
cin >> score;

header();

while(score < 0 || score > 6)
{
// Checks that the scores are between zero and six.
// It continues to ask for a correct score if the
// user makes successive mistakes.

cout << "\nError: Scores must be between 0 and 6.";
cout << "\n\nPlease reenter the score : ";
cin >> score;
header();
}

// Here the total variable adds up all the correctly
// entered scores.

total += score;

if(i == 0)
{
// Runs the first time that the for loop runs only.
// These variables are intialised from user input,
// this is because otherwise I would have to intialise
// highest_score to zero and lowest_score to six.
While
// that would work, it would also be inflexible,
whereas
// this algorithm to find a highest number and the
lowest
// number in a set of numbers will work in other
programs.

highest_score = score;
lowest_score = score;
}

// Gets highest score.
if(score > highest_score)
highest_score = score;

// Gets lowest score.
if(score < lowest_score)
lowest_score = score;
}

cout << "Lowest Score: " << lowest_score << endl;
cout << "Highest Score: " << highest_score << endl << endl;

// Subtracting highest & lowest score from total.

total = total - (highest_score + lowest_score);

// Gets average.

average = total / (num_judges_int - 2);

// Sets the number of significant digits to output.

cout.precision(2);

cout << "Average: " << average << endl << endl;
cout << "Press any key to end this program";

// getch() halts program and waits for a key stroke to go on executing
code.

getch();
return 0;
}

void header(void)
{
// clear screen of text.

clrscr();

// put in a heading/banner

cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << endl;
cout << "\t\t Celebrity Ice-Skating \n";
cout << endl;
cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
}
 
M

Morgan

You shouldn't really have three loops each keeps asking for new values.
This is because simply by allowing a loop to update a variable you are
allowing the variable to betray the previous loop.

Agggh! (tears hair out)
So a correct algorithm is:

while (true)
{

double x;
cin >> x;

if (first_check(x) && second_check(x) && third_check(x))
break;
}

Lets see, am I reading this correctly?

you have an infinite loop, in which we ask for the input, and stick it
in a variable.

In actual fact I'll have two variables, the float version and the
integer version of the loop.

Now, make an conditional asking if all 3 tests work out.
(to one test function two arguments would have to be passed, in order
to figure out if the input is an integer or a float.)
I don't see your code "functionalised". What you need to do is to
localize each check so they are self-contained. The functions given in
the previous example--first_check, second_check and third_check can be
managed as:

bool first_check(double x)
{
// return true if x is valid
// ...
}

Ok, this is looking good. I'll try it out and get back to you.

btw: Why are you using doubles instead of floats, any particular
reason or do you just want it to be as accurate as possible?
(I'm used to using floats only)
 
J

John Carson

Morgan said:
ALTERED CODE below for John Carson:

BTW: To recieve lots of errors, try typing in eg: zero, followed by a
number with a decimal point.

[snip]

The problem is here in your while loop.
cin >> num_judges_float;
num_judges_int = num_judges_float;

while((num_judges_float - num_judges_int) > 0 ||
(num_judges_int < 3))
{

if((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();
}
else if(num_judges_int < 3)
{
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;

Here you write from user input to an int. However, the fact that the last
entry was an int doesn't mean that the next one will be --- and in your
example of a zero followed by a decimal the assumption doesn't hold.

In ALL cases of erroneous input, you must go through the same procedure,
namely

cin >> num_judges_float;
num_judges_int = num_judges_float;

(of course, there are other possible types of erroneous input that this
procedure won't work with, but they are presumably outside the scope of the
exercise.)
 
D

Duane Hebert

Morgan said:
/*
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.
*/

If you want to validate the user input, why not just read
it in as a string. If the number isn't an int, issue an error.
This is fairly simple with a stream.

As for the values, it seems like you want to get the highest,
lowest and average. Why not put them in a set. Then you easily
have the highest and lowest. The average is just calculated by
iterating through the set, summing it and dividing by the count.

Not sure what the problem is that requires at least three judges though.
Seems like highest,lowest and average are pretty trivial with 1 judge<g>
 
M

Morgan

You shouldn't really have three loops each keeps asking for new values.
This is because simply by allowing a loop to update a variable you are
allowing the variable to betray the previous loop. To see my point,
follow my comment on your code below

So a correct algorithm is:

while (true)
{

double x;
cin >> x;

if (first_check(x) && second_check(x) && third_check(x))
break;
}




I don't see your code "functionalised". What you need to do is to
localize each check so they are self-contained. The functions given in
the previous example--first_check, second_check and third_check can be
managed as:

bool first_check(double x)
{
// return true if x is valid
// ...
}

etc.

NOTE: to increase readability you may want to change names like
first_check() to something more sensible like is_integer(), etc.










Below is the first check, making sure the variable is integer



Below is the second check, making sure the variable is positive non-zero.



However, by the end of this loop, you will have a positive, non-zero
number but it may not be integer.








Ben


IT WORKS IT WORKS!!!!!!!!!!

Woo hoo!!!!

Thank you sir!

Happy times are here again... lalalala!
 
M

Morgan

ALTERED CODE below for John Carson:
BTW: To recieve lots of errors, try typing in eg: zero, followed by a
number with a decimal point.

[snip]

The problem is here in your while loop.


cin >> num_judges_float;
num_judges_int = num_judges_float;
while((num_judges_float - num_judges_int) > 0 ||
(num_judges_int < 3))
{
if((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();
}
else if(num_judges_int < 3)
{
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;

Here you write from user input to an int. However, the fact that the last
entry was an int doesn't mean that the next one will be --- and in your
example of a zero followed by a decimal the assumption doesn't hold.
Exactly.

In ALL cases of erroneous input, you must go through the same procedure,
namely

cin >> num_judges_float;
num_judges_int = num_judges_float;

(of course, there are other possible types of erroneous input that this
procedure won't work with, but they are presumably outside the scope of the
exercise.)

Yes, this is my first C++ project, I doubt I have to worry about more
estoric things like in the realm of overflows and stuff.

Anyhow, thanks for your help, I see my code clearer than before. :)
 
M

Morgan

If you want to validate the user input, why not just read
it in as a string. If the number isn't an int, issue an error.
This is fairly simple with a stream.

Well, because it may be simple, but I haven't done any work on strings
yet, so I don't know what function I would use (like I said, I'm very
new to this :)
As for the values, it seems like you want to get the highest,
lowest and average. Why not put them in a set. Then you easily
have the highest and lowest. The average is just calculated by
iterating through the set, summing it and dividing by the count.

A set? Do you mean an array? (started that in class two days ago!)
Not sure what the problem is that requires at least three judges though.
Seems like highest,lowest and average are pretty trivial with 1 judge<g>

LOL! True. Like a dictatorship.

However I was told to take away the highest and lowest from the total
before getting an average. That would mean I would have to minus the
scores of two judges, effectively minusing two judges themselves.
I think that the purpose of this silliness was to make sure that
the average wasn't thrown out by just one or two really high scores or
really low scores. You know, like they do with population census or
with political polls.

In that case having just one judge would yield a negative value eg:

total = average/ (number of judges - 2)

Two judges would then yield me trying to divide a number into
infinity. (good luck!) :)
 
D

Duane Hebert

Morgan said:
Well, because it may be simple, but I haven't done any work on strings
yet, so I don't know what function I would use (like I said, I'm very
new to this :)

Ok but you seem to be assuming that the error will be that you
want an int and get a float. What if the user enters "q"? What about
"q3" or "3q"?

A set? Do you mean an array? (started that in class two days ago!)

No I mean std::set. I assumed that by now schools were teaching
stl but maybe not. You should at least be familliar with some sort of
character strings before being required to deal with keyboard input though.
LOL! True. Like a dictatorship.

However I was told to take away the highest and lowest from the total
before getting an average. That would mean I would have to minus the
scores of two judges, effectively minusing two judges themselves.
I think that the purpose of this silliness was to make sure that
the average wasn't thrown out by just one or two really high scores or
really low scores. You know, like they do with population census or
with political polls.

Ok so you need to remove the outliers.
In that case having just one judge would yield a negative value eg:

total = average/ (number of judges - 2)

Two judges would then yield me trying to divide a number into
infinity. (good luck!) :)

Ok. So first you want the number of judges and you want to validate
that it's a valid int and > 2. Trying to read it in as an int is going to
give you problems since you're converting characters to an int
implicitly. You need to be aware of all of the possible problems
with this.

What you need to do is write your problem out in pseudo code.
Then break each sub problem into a function. Then solve it.
Something like:
Get the number of judges.
Validate it as an int > 3.

Get a number of values equal to the number of judges.
Validate each as a number.

Find the highest and discard it.
Find the lowest and discard it.
Find the average.

Before you can do any of the last three, you
need to make sure that the values are valid.
 
M

Morgan

Ok but you seem to be assuming that the error will be that you
want an int and get a float. What if the user enters "q"? What about
"q3" or "3q"?

Then I totally admit I'm screwed. Although we haven't done strings
yet, (apart from getting one character inputs from the keyboard)
I did try to use a function called typedef() to figure out if a string
was being entered. However I then found that (naturally) all keyboard
entries would be strings!

So to find a way to separate the character strings and the number
strings from each other is what I would need.
No I mean std::set. I assumed that by now schools were teaching
stl but maybe not. You should at least be familliar with some sort of
character strings before being required to deal with keyboard input though.

I agree, but there is little I can do about the way I'm being taught.
Is there a function in C++ to find out if keyboard entry is a string?
What header file do I use? What (basic) issues with strings should I
be aware of?
Ok so you need to remove the outliers.




Ok. So first you want the number of judges and you want to validate
that it's a valid int and > 2. Trying to read it in as an int is going to
give you problems since you're converting characters to an int
implicitly.

Yes, which was why I read it in as a float first, and then assigned
the float value to an integer variable. That way I was able to minus
the float from the int and figure out if it equaled zero. (if it was
zero it was an int, if not, a float)
You need to be aware of all of the possible problems
with this.
What you need to do is write your problem out in pseudo code.
Then break each sub problem into a function. Then solve it.
Something like:
Get the number of judges.
Validate it as an int > 3.

Get a number of values equal to the number of judges.
Validate each as a number.

Find the highest and discard it.
Find the lowest and discard it.
Find the average.

Before you can do any of the last three, you
need to make sure that the values are valid.

No problem, ben made a nice algorithm that I intend to play with later
and it worked out a treat. (it was based on functionally breaking the
problem like you suggested.)
 
D

Duane Hebert

Most of your other questions can be answered by getting a good
book detailing c++ methodologies. The "thinking in c++" series
are pretty popular. There's something called STL that you may
find interesting.
No problem, ben made a nice algorithm that I intend to play with later
and it worked out a treat. (it was based on functionally breaking the
problem like you suggested.)

At this point, you probably should concentrate on learning
how to solve problems and not get too wrapped up about
a particular assignment. I know that you get graded on the
assignments so it's a pretty standard situation.

But in this case, for example, you've already learned about
breaking problems into their functional parts.
This is probably a more useful lesson than learning how to
average some numbers.

If you're instructors are at all decent,
this is their intent. At any rate, good luck
with your studies.
 
G

Grizlyk

Morgan said:
Ok, I'll post the entire code, its not too long (if you get rid of my
very long comments!)
A.

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();
}


You need to read any about structured programming: how to implement ordinary
algorithms. I can, of course, to write the example here, but next time you
will find trouble again. You can not learn it from newsgroup. Look

1.

You two times write the same code
cin >> num_judges_float;
num_judges_int = num_judges_float; ....
cin >> num_judges_float;
num_judges_int = num_judges_float;
header();

The only reason to move first step outside of loop is if first iteration is
different from other iterations, to eliminate "if(!loop_counter)" test
inside large loop or while you are walking over all values, that for()
counter can hold.

In your example very easy to change first part and forget to change second,
it is better to do all together

for(;;)
{

header();
cin >> num_judges_float;


//I do not say that you take the best way to check
num_judges_int = num_judges_float;
if( (num_judges_float - num_judges_int) > 0 )break;

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: ";
}

2.
The last piece of code above it ia a function, so make it as function,
because function is more abstracted thing that plain code.

//test signed value
//inline because always combined with get_integral below
inline float get_float()
{
for(;;)
{

header();

float num_judges_float=0;
cin >> num_judges_float;
if(num_judges_float>0)return num_judges_float;

//message: negative is invalid
}
}

//test integral value
unsigned get_integral()
{

for(;;)
{

header();

//call here get_float
const float num_judges_float=get_float();

const unsigned num_judges_int = num_judges_float;
if( (num_judges_float - num_judges_int) > 0 )
return num_judges_int;

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: ";
}
}

3.
Do not write "int" if negative value of logical variable is nonsence, due to
"int" is easy to write than "unsigned". Write "typedef undigned uint;" in
you header. Do not create negative IOports, negative memory size, negative
number of incoming letters and so on.


B.
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();
}

Here the same - make a function get_judges, call inside it the get_integral.
If get_integral will always combined with get_judges make get_integral as
inline.


C.

And so on
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top