1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <cmath>
using namespace std;
float calcDistance(float, float);
void getInput(float&, float&);
const float pi = 3.14159265;
int main()
{
float userDistance, velocity, angle, actualDistance;
int tries = 0;
bool check = 1;
cout << "Enter distance of the target: ";
cin >> userDistance;
cout << endl << "Enter velocity of projectile: ";
cin >> velocity;
cout << endl << "Enter angle of projectile: ";
cin >> angle;
cout << endl << endl;
while(check)
{
actualDistance = calcDistance(velocity, angle);
if(userDistance <= actualDistance)
{
if(abs(actualDistance - userDistance) <= actualDistance * .01)
{
cout << "hooray, you win!";
check = 0;
}
else
{
cout << "Your projectile missed by " << actualDistance - userDistance << endl << endl;
getInput(velocity, angle);
tries++;
}
}
if(userDistance >= actualDistance)
{
if(abs(userDistance - actualDistance) <= actualDistance * .01)
{
cout << "hooray, you win!";
check = 0;
}
else
{
cout << "Your projectile missed by " << userDistance - actualDistance << endl << endl;
getInput(velocity, angle);
tries++;
}
}
if(tries == 5)
{
cout << "You are out of tries." << endl;
check = 0;
}
}
system("pause");
return 0;
}
//****************************************************************************
// value return to actualDistance
float calcDistance(float velocity, float angle)
{
// converting angle to radians
float radians, convertDistance;
radians = (angle * pi)/180.0;
convertDistance = ((velocity*velocity)*sin(2*radians))/32.2;
return convertDistance;
}
//*****************************************************************************
// void function to prompt for input
void getInput(float& velocity, float& angle)
{
cout << "Input new velocity: ";
cin >> velocity;
cout << endl << "Input new angle: ";
cin >> angle;
cout << endl << endl;
}
|