-
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 ;
}
:confused:
-
what exactly is the problem?
I'm not quite sure what you're asking.
-
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???
-
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!!!
-
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.
-
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!).
-
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;
}
-
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.