Thread: data validation

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Question data validation

    I have tried to input data validation in this code (which works dist 150, angle 37, vleocity 71) but everything I do messes up the code. I know I am no programmer and I solved the hair falling out problem (I have it braided. lol). Anthing ideas, hints... help


    Code:
    #include <iostream>
    #include <math>
    
    const float PRECISION = 0.001;     // allowable margin of error
    const double PI = 3.14159265;      // value of PI
    
    void PrintDescription();               // details of program
    void PlayGame(float, float&, float&);  // provided by user
    double ToRadian(float&);               // convert angle to radian
    double CalcDistance(float&, float&);   // gives projected distance
    int AlmostThere(float a , float b);    // how near or how far to target
    void HoldScreen(bool);
    
    float distance;
    double angle;
    double velocity;
    
    int main()
    {
        PrintDescription();
        PlayGame(distance, angle, velocity);
        HoldScreen(true);
    
        return 0;
    }
    //------------------------------------------------------------------------------
    // brief details of program
       void PrintDescription()
       {
           cout << "This program implements a target game. The user enters"
                   " the target distance,\n";
           cout << "the angle and velocity for launching a projectile. To win,"
                   " the projectile must\n";
           cout << "land at least 0.1% of the distance to the target.\n";
           cout << "The user receives five chances to win.\n";
    
        return;
       }
    //------------------------------------------------------------------------------
    //user is prompted for distance, angle and velocity. If the user hits the
    //target the  game is over, otherwise the user gets 4 more chances.
       void PlayGame(/* in */float distance,
                    /* out */float& angle,
                   /* in */float& velocity)
       {
           int attempts = 0;
           bool hitOrMiss = false;
           int projection;
    
           // get distance
           cout << "\nTo play enter the distance to the target.\n";
           cin >> distance;
           cin.ignore(100, '\n');
    
           while ((attempts < 5) && (hitOrMiss == false))
           {
                //get angle
                cout << "Enter the angle in degrees (example 45.0, 90.0).\n";
                cin >> angle;
                cin.ignore(100, '\n');
                angle = ToRadian(angle);  // convert angle to radian
    
                // get velocity
                cout << "Enter the velocity in feet per second.\n";
                cin >> velocity;
                cin.ignore(100, '\n');
                projection = CalcDistance(velocity, angle);
    
                //get results
                if (((projection) >= (distance - (distance * PRECISION)))
                && ((projection) <= (distance + (distance * PRECISION))))
                {
                   cout << "You WIN.\n";
                   hitOrMiss = true;
                }
                else
                {
                    cout << "Sorry, try again.\n";
                    AlmostThere(distance, projection);
                    HoldScreen(false);
                }
            attempts++;
            }
            return;
        }
    //------------------------------------------------------------------------------
    // convert angle to radian
       double ToRadian(/*inout */float& angle)
       {
           return(angle * PI) /180.0;
       }
    //------------------------------------------------------------------------------
    // gives projected distance
       double CalcDistance(/* inout */float& velocity,
                          /* inout*/float& angle)
       {
           return(((velocity * velocity) * (sin(2 * angle)))/32.2);
       }
    //------------------------------------------------------------------------------
     // how near or how far to target
       int AlmostThere(/* in */float a,
                      /* in */float b)
       {
           float target = a - b;
    
           if(target < 0)
           {
              cout << "You overshot by " << abs(target) << '\n';
           }
           else
           {
             cout << "You undershot by " << target << '\n';
           }
    
           return(fabs(target));
       }
    //------------------------------------------------------------------------------
     /* This function will hold the screen open for the user to continue or to */
     /* the program */
    
       void HoldScreen(/* in */bool credits)
       {
            if(credits)
            cout << "\nDesigned by Loranne Wish.\n";
            cout << "Press enter to continue.\n";
            cin.get();
    
            return ;
       }
    It's all about LOVE

  2. #2
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    what exactly is the problem?
    I'm not quite sure what you're asking.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    I have to verify that the user has entered valid data. that the numbers entered are not negative. so when I put in this:

    Code:
    cout << "Please enter the distance to the target.\n";
    cin >> distance;
    cin.ignore(100, '\n');
    
    while(distance < 0)
    {
        cout << "Distance must be greater than 0,  try again.\n':
        cin >> distance;
        cin.ignore(100, '\n');
    }
    
    
    
    cout << "Please enter the angle.\n";
    cin >> distance;
    cin.ignore(100, '\n');
    
    while(angle < 0)
    {
        cout << "angle must be greater than 0,  try again.\n':
        cin >> angle;
        cin.ignore(100, '\n');
    }
    
    
    cout << "Please enter the velocity.\n";
    cin >> velocity;
    cin.ignore(100, '\n');
    
    while(velocity < 0)
    {
        cout << "velocity must be greater than 0,  try again.\n':
        cin >> velocity;
        cin.ignore(100, '\n');
    }

    when I do this I always lose either the distance, or the ability to press enter and go to the next data input. I've tried putting this into a module and calling it, but that causes more problems then it is worth. And Yes if I put it in the PlayGame module it will work, but at the cost of having that module do more than one thing. I could take the hit (grade point loss) for cluttered, unconventional programming style. or lose points for not validating the data entered by the user, but that involves being jumped on by the professor (that is a nightmare of it's own).. What you reckon???
    It's all about LOVE

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    One thing that strikes me is that you are reading distance into a variable called distance, then angle... into a variable called distance.....

    Distance is thus lost, and you start looping against the presumably uninitialised value angle.

    You cut and pasted didn't you, and forgot the edit didn't you!!!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    i typed it in on this site this morning and yes I cut and pasted the one while statement. I do not have this in the program. like i said it works if it's in the PlayGame module, but will not it I put it into a module by itself and call it.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    That being the case, there is obviously something wrong with the way you are putting it into a seperate function isn't there. Now, why not show us the function code that fails, (instead of the code that would fail but doesn't because it isn't, or something like that!).
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    Code:
     void GetAngleVelocity(/* in */float& angle,
                           /* in */float& velocity)
      {
          //get angle
                cout << "Enter the angle in degrees (example 45.0, 90.0).\n";
                cin >> angle;
                cin.ignore(100, '\n');
    
                 while (angle < 0)
                 {
                     cout << "Angle must be greater than 0.\n";
                     cin >> angle;
                     cin.ignore(100, '\n');
                 }
    
                 // get velocity
                 cout << "Enter the velocity in feet per second.\n";
                 cin >> velocity;
                 cin.ignore(100, '\n');
    
                 while (velocity < 0)
                 {
                     cout << "Velocity must be greater than 0.\n";
                     cin >> velocity;
                     cin.ignore(100, '\n');
                 }
    
                 return;
       }

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I wrapped your function in a very simple container...

    Code:
    #include <windows.h>
    #include <iostream.h>
    
    void GetAngleVelocity(/* in */float& angle,
                           /* in */float& velocity)
      {
          //get angle
                cout << "Enter the angle in degrees (example 45.0, 90.0).\n";
                cin >> angle;
                cin.ignore(100, '\n');
    
                 while (angle < 0)
                 {
                     cout << "Angle must be greater than 0.\n";
                     cin >> angle;
                     cin.ignore(100, '\n');
                 }
    
                 // get velocity
                 cout << "Enter the velocity in feet per second.\n";
                 cin >> velocity;
                 cin.ignore(100, '\n');
    
                 while (velocity < 0)
                 {
                     cout << "Velocity must be greater than 0.\n";
                     cin >> velocity;
                     cin.ignore(100, '\n');
                 }
    
                 return;
       }
    
    int main()
    {
        float angle;
        float velocity;
    
        GetAngleVelocity(angle, velocity);
    
        cout << angle << " " << velocity << endl;
        return 0;
    }
    ... and when I run it it gives me the results below, which would seem to be right.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data validation
    By darren78 in forum C Programming
    Replies: 5
    Last Post: 02-19-2009, 12:14 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM