I made this code for a game called ProjectTrig http://www.mathplayground.com/ProjectTRIG/ProjectTRIGPreloader.html . Using this, I can accurately calculate The angle and velocity needed. My only problem is as the program asks the user to enter a number, what if the user entered a string instead? As a result, the program will start to function inaccordingly instead of running properly. I heard about converting int to string, but I dont know how it exactly works.
I also want to change cin >> to getline (cin,...) .Please help me to revised this code:
#include <iostream>
#include <math.h>
#define PI 3.14159265
usingnamespace std;
int main ()
{
double x,y,yo,angle,v1,v2,v3,velocity,retry,n,;
do
{
cout << "Enter angle:\n";
cin >> angle;
cout << "Enter yo:\n"; // The height of the cannon;
cin >> yo;
cout << "Enter x coordinate:\n"; // coordinates are the place where the target is located.
cin >> x;
cout << "Enter y coordinate:\n";
cin >> y;
v1= 2.21 * x;
v2= cos (angle*PI/180);
v3= sqrt (yo-y+(tan (angle*PI/180))* x);
velocity =v1/(v2*v3);
// As we noticed, I divided the formula into different 4 chunks.This makes it easier to calculate in order.
cout <<"Status:\n"<<v1<<endl<<v2<<endl<<v3<<endl<<endl<<endl; // Just to display the results of each.
cout << "VELOCITY = " << velocity<<endl<<endl<<endl;
cout << "Try again? (0 to end): ";
cin >> retry;
}
while (retry != 0);
return 0;
}