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



LinkBack URL
About LinkBacks



