Please Help Newbie with validation problem

Discussion in 'C++' started by Morgan, Feb 11, 2007.

  1. Morgan

    Morgan Guest

    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();
    }
     
    Morgan, Feb 11, 2007
    #1
    1. Advertising

  2. Morgan

    Morgan Guest

    Crap, I somehow managed to double post.

    Sorry folks.


    On Feb 11, 12:21 pm, "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.
    >
    > 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();
    > }
     
    Morgan, Feb 11, 2007
    #2
    1. Advertising

  3. Morgan

    Grizlyk Guest

    Morgan wrote:
    >
    > I've been working on this code for about 2 days now, and can't get it
    > to work.


    What does not work?

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
     
    Grizlyk, Feb 11, 2007
    #3
  4. Morgan

    osmium Guest

    "Morgan" writes:

    > 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.
     
    osmium, Feb 11, 2007
    #4
  5. Morgan

    Morgan Guest

    On Feb 11, 2:26 pm, "osmium" <> wrote:
    > "Morgan" writes:
    > > 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.


    Yes, ben suggested something along these lines and it worked nicely.
     
    Morgan, Feb 11, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    541
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,785
    Mike Hewson
    Jan 7, 2005
  4. Morgan
    Replies:
    16
    Views:
    803
    Grizlyk
    Feb 11, 2007
  5. bnp
    Replies:
    4
    Views:
    335
Loading...

Share This Page