Error Running with Strings

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:

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
#include <iostream>
#include <math.h>
#define PI 3.14159265 
using namespace 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;
}

Last edited on
Bazzy's article on converting numbers to strings and vice versa.
Zaita's article on using cin to get user input.

Zaita's guide is probably what you want, but Bazzy's article might provide some additional useful information. Enjoy~
Those two articles helped me but the the one made by Zaita is the exact article I'm looking for. Thanks.
Topic archived. No new replies allowed.