Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Please Help Newbie ( While Loop Validation Problem)

Reply
Thread Tools

Please Help Newbie ( While Loop Validation Problem)

 
 
Morgan
Guest
Posts: n/a
 
      02-11-2007
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!

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      02-11-2007

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

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      02-11-2007
"Morgan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com
> 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.

--
John Carson



 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      02-11-2007
Morgan wrote:
> 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
 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      02-11-2007
On Feb 11, 12:49 pm, "Grizlyk" <(E-Mail Removed)> wrote:
> 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";
}


 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      02-11-2007
On Feb 11, 1:01 pm, "John Carson" <(E-Mail Removed)>
wrote:
> "Morgan" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ps.com
>
>
>
> > 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.


Sure, I appreciate that, I was trying to give the user different error
messages though.

>
> > 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.


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)

 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      02-11-2007
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";
}

 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      02-11-2007
On Feb 11, 1:08 pm, benben <benhonghatgmaildotcom@nospam> wrote:
> Morgan wrote:
> > 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.


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 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
> // ...
> }


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)

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      02-11-2007
"Morgan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
> 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.)


--
John Carson



 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      02-11-2007

"Morgan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> /*
> 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Opinion poll: for loop vs while loop with Iterators. Daniel Pitts Java 14 12-23-2006 05:22 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
help with parameter not passed from for loop to while loop pauljturner99@gmail.com ASP General 1 07-19-2006 11:06 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments