How to get proper result for Escape Velocity?

Hello,
I am doing a project where I need to find the escape velocities for given planets. I have a formula ready, but it keeps showing me only 0. What is wrong with my formula?
Here is the masses of the planets in order(kg): 0.642 1898 86.8 0.0146
Here is the diameters of the planets in order (m):6792 142984 51118 2370

Here is also part of the directions given to me : "The escape velocity is determined by the total energy going to zero at infinity, see physics text if needed. The total energy is the
sum of Kinetic and gravitational Potential which for a mass(in kg), m of ship leaving, cancels in solution of V, M =mass of Planet(kg)
, R =Radius of the planet (meters), escape velocity, V (meters/sec) 1/2mV2
-GmM/R = 0 at infinity, solve for V in function.
with the gravitational constant G=6.67 x 10-11 (Unit implies meters and kg)
The function returns V from solving for it when it gets M and R sent to it."

Here is my code so far:
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*G3 Lab#16 4/23/22
In this program */
#include <iostream>		//Required for cin, cout, cerr
#include <fstream>		//Required for ifstream, ofstream.
#include <string>		//Required for string.
#include <cmath>		//Required for Pow
using namespace std;
int
main ()
{
  int G (6.67 * pow (10, -11)),EscVelEarth(1.12*pow(10,4));	// Declare and initialize objects (variables and i0 objects pointing to files).

  string filename;
  ifstream planets;		// read a file and use for getting data in
  ofstream report;		// create and write to a file
// first we will create a data file
  double MarsMass (0), MarsRadius (0), JupiterMass (0), JupiterRadius (0),
    UranusMass (0), UranusRadius (0), PlutoMass (0), PlutoRadius (0),
    MarsDiameter (0), JupiterDiameter (0), UranusDiameter (0),
    PlutoDiameter (0), GreaterorLessMars, GreaterorLessJupiter,
    GreaterorLessUranus, GreaterorLessPluto, EscapeVelocityMars,
    EscapeVelocityJupiter, EscapeVelocityUranus, EscapeVelocityPluto,
    GreaterorLess, IsGreaterThan, IsLessThan;
  cout << "Enter the name of the data file you want";	// Prompt user for name of input file, "birds".
  cin >> filename;
  report.open (filename.c_str ());
  cout <<
    " Enter the masses of Mars, Jupiter, Uranus, and Pluto (in kg)." << endl;
  cin >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass;	// get the values from keypad
  report << MarsMass << " " << JupiterMass << "  " << UranusMass << " " << PlutoMass << endl;	// send values to data file
  cout <<
    "Now enter the diameter of Mars, Jupiter, Uranus, and Pluto" << endl;
  cin >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter;
  report << MarsDiameter << " " << JupiterDiameter << "  " << UranusDiameter
    << " " << PlutoDiameter << endl;
  report.close ();		// close created data file
  cout << "Enter the name of the data file you want to analyse";	// Prompt user for name of input file.
  cin >> filename;

  planets.open (filename.c_str ());	// Open file and check if it exists.
  if (planets.fail ())
    {
      cerr << "Error opening input file\n";
      exit (1);
    }

  report.open ("MonaesReport.txt");	// open report file.
  planets >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter;	// initial input read the first data point.

  while (!planets.eof ())	// While not at the end of the file read and accumulate information
    {

      MarsRadius = MarsDiameter / 2;
      JupiterRadius = JupiterDiameter / 2;
      UranusRadius = UranusDiameter / 2;
      PlutoRadius = PlutoDiameter / 2;

      EscapeVelocityMars =
	sqrt (2 * G * MarsMass * MarsRadius) / (MarsRadius);
      EscapeVelocityJupiter =
	(sqrt (2 * G * JupiterMass * JupiterRadius)) / (JupiterRadius);
      EscapeVelocityUranus =
	(sqrt (2 * G * UranusMass * UranusRadius)) / (UranusRadius);
      EscapeVelocityPluto =
	(sqrt (2 * G * PlutoMass * PlutoRadius)) / (PlutoRadius);

      if (EscapeVelocityMars > EscVelEarth)
	{
	  GreaterorLessMars = IsGreaterThan;
	}
      else
	(EscapeVelocityMars < EscVelEarth);
      {
	GreaterorLessMars = IsLessThan;
      }
      if (EscapeVelocityJupiter > EscVelEarth)
	{
	  GreaterorLessJupiter = IsGreaterThan;
	}
      else
	(EscapeVelocityJupiter < EscVelEarth);
      {
	GreaterorLessJupiter = IsLessThan;
      }
      if (EscapeVelocityUranus > EscVelEarth)
	{
	  GreaterorLessUranus = IsGreaterThan;
	}
      else
	(EscapeVelocityUranus < EscVelEarth);
      {
	GreaterorLessUranus = IsLessThan;
      }
      if (EscapeVelocityPluto > EscVelEarth)
	{
	  GreaterorLessPluto = IsGreaterThan;
	}
      else
	(EscapeVelocityPluto < EscVelEarth);
      {
	GreaterorLessPluto = IsLessThan;
      }



      planets >> MarsMass >> JupiterMass >> UranusMass >> PlutoMass >> MarsDiameter >> JupiterDiameter >> UranusDiameter >> PlutoDiameter;	// input next go back to while
    }				// end while
  cout << "Planet Radii" << " " << "PLanet Masses (kg)" << " " <<
    "Planet Escape Velocities" << "          " <<
    "Earth Escape Velocity (1.12*10^4m/s)" << GreaterorLess << endl;
  cout <<
    "___________________________________________________________________________________________________________________________________"
    << endl;
  cout << MarsRadius << "      " << MarsMass << "      " << EscapeVelocityMars
    << "          " << "                   " << GreaterorLessMars << endl;
  cout << JupiterRadius << "      " << JupiterMass << "          " <<
    EscapeVelocityJupiter << "          " << "                " <<
    GreaterorLessJupiter << endl;
  cout << UranusRadius << "       " << UranusMass << "        " <<
    EscapeVelocityUranus << "          " << "                     " <<
    GreaterorLessUranus << endl;
  cout << PlutoRadius << "        " << PlutoMass << "        " <<
    EscapeVelocityPluto << "          " << "                     " <<
    GreaterorLessPluto << endl;


  report.setf (ios::fixed);
  report.setf (ios::showpoint);
  report.precision (2);
// Print summary information to report file

// Close file and exit program.
  report.close ();
  planets.close ();
  return 0;
}				//end main 
Last edited on
I think you need to review all of your course material so far, there is so much wrong with this code.

Starting with this:

int G (6.67 * pow (10, -11)),EscVelEarth(1.12*pow(10,4));
the mass of a planet is what??
pluto apparently ways 1.3x10^22 kg and 2370KM not M.

you can run the numbers with whatever they gave you or made up values etc but what you have looks very, very wrong.
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 <string>
#include <fstream>
#include <cmath>

#include <iomanip>

int main()
{
    double v{0};
    const double G{6.67E-11};
    double M{0};
    double d0{0};
    std::string heading;
    std::string name;
    
    std::ifstream file ("planets.txt");
    if (file.is_open())
    {
        while ( file >> name >> M >> d0 )
        {
            v = sqrt(2* G * M * 1e24 / (d0/2. * 1000.) );
            std::cout
            << std::setw(8) << name
            << std::setw(10) << std::right << v << " m/s\n";
        }
        file.close();
    }
    
    else std::cout << "Unable to open file";
    
    return 0;
}


MERCURY 0.330 4879
VENUS 4.87  12104
EARTH 5.97  12756
MOON 0.073  3475
MARS 0.642  6792
JUPITER 1898  142984
SATURN 568  120536
URANUS 86.8  51118
NEPTUNE 102  49528
PLUTO 0.0130  2376


 MERCURY      4248 m/s
   VENUS   10360.8 m/s
   EARTH   11174.4 m/s
    MOON   2367.43 m/s
    MARS   5021.83 m/s
 JUPITER     59511 m/s
  SATURN   35457.5 m/s
  URANUS   21284.6 m/s
 NEPTUNE   23440.5 m/s
   PLUTO   1208.21 m/s
Program ended with exit code: 0
I redid it, but now it is saying there is something wrong with the const double on line 9.
The error read : main.cpp: In function ‘double PlanetEscVel(double)’:


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
#include<iostream>		//Required for cin, cerr
#include<fstream>		//Required for ifstream, ofstream
#include<string>		//Required for string, c_str():
#include <cmath>		// needed by function for square root sqrt()
#include <iomanip>		// setprecision
using namespace std;

double PlanetEscVel (double PlanetDiameter) (double PlanetMass)	//Programmer function prototype
     const double gravity = 6.67e-11;	//needed before main as global variable, all functions can use
     int
     main ()
{
  double PlanetDiameter, PlanetMass, PlanetRadius;
  ifstream fin;			// input objects to get data from files
  ofstream fout;		// output object to send data to files
  string filename;		// as need for files.
  cout << "Please enter the name of the file you want to create.\n";
  cin >> filename;
  fout.open (filename.c_str ());
  cout <<
    "Please enter the first diameter and the mass(kgs) of the planet.\n";
  cin >> PlanetDiameter >> PlanetMass;
  while (PlanetDiameter != 0, PlanetMass != 0)
    {
      fout << PlanetDiameter << PlanetMass;
      cout << "Enter the diameter and mass of the next planet\n";
      cin >> PlanetDiameter >> PlanetMass;
    }
  fout.close ();
  cout <<
    " Now, enter the name of the file to read the diameters and masses of the planets. \n";
  cin >> filename;
  fin.open (filename.c_str ());
  if (fin.fail ())
    {
      cerr << "Could not open the file " << filename << endl;
      exit (1);
    }
// open the report output file
  fout.open ("MonaePlanetReport.txt");
// get first input data

  cout << "Planet Radius        Planet Mass\n";	// headings screen & file echo
  fout << "Planet Radius        Planet Mass\n";

  while (!fin.eof ())		//while not end of file
    {
      PlanetRadius = double (PlanetDiameter);	//Function call get proper velocity.
      PlanetRadius = PlanetDiameter / 2;
      cout << " " << PlanetRadius << " " << PlanetMass << endl;	// first echo to
      fout << " " << PlanetRadius << " " << PlanetMass << endl;
      fin >> PlanetRadius >> PlanetMass;	// get next value continue in while to eof()
    }

  fin.close ();
  fout.close ();
  return 0;
}				// end of main()

/* velocity needed by vaulter to clear a pole of a given height*/
double
PlanetEscVel (double Radius)	//Function header.
{
  double PlanetEscVel, PlanetRadius, PlanetDiameter, PlanetMass;	//Declare local variables (not known to main)
  PlanetRadius = PlanetDiameter / 2;	//add 1 meter for clearance
  PlanetEscVel = sqrt (2 * gravity * PlanetMass * PlanetRadius) / PlanetRadius;	//1/2m V2 =mgh
  return PlanetEscVel;		// function returns value of velocity need to clear poles
}				// end of the function 
Last edited on
Your (first) problem(s) is/are on line 8, not line 9. And they'll lead to further anomalies on line 62.

No idea what your comment alludes to on line 65. Line 48 looks pretty unusual, too.
Last edited on
What is wrong with line 8? My goal is to create a function in the program that gives me the escape velocity. (And then make a table.) Also the comment was there from a template, it is irrelevant.
Last edited on
Blueshark1 wrote:
What is wrong with line 8?


Your line 8 is currently
double PlanetEscVel (double PlanetDiameter) (double PlanetMass)

It is intended to declare a function prototype, is it not? A function taking two parameters: PlanetDiameter and PlanetMass. The correct way of writing such a prototype is
double PlanetEscVel (double PlanetDiameter, double PlanetMass) ;
or even just
double PlanetEscVel ( double, double ) ;

Of course this will not then correspond to your actual function definition, which starts on lines 61 and 62 (no idea why you need two lines for this), which only has a single parameter and which is even more bizarrely called a Radius, not a Diameter. You have a lot of work to do on this function: not least on passing in the correct parameters.



Incidentally, your compiler may have reported line 9 as a problem simply because you didn't conclude the code on line 8 with a semicolon (;) to end the statement. In that case line 9 would have been seen as an extension of the erroneous line.



Also the comment was there from a template, it is irrelevant.

Bad comments can be worse than no comment at all. Use comments when necessary, use only comments which are accurate, and don't use comments when they state the obvious.



I've only just noticed:
/* velocity needed by vaulter to clear a pole of a given height*/

In the context of planetary escape velocities this made me smile.
Last edited on
Here is what I have now, I changed the while loop to a for loop because it repeated the "Enter the diameter and mass of the next planet\n" part over and over. But it won't put the values entered into a table. (According to the planet radius, planet mass, and Planet escape velocity headings). Is this any better?

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
#include<iostream>		//Required for cin, cerr
#include<fstream>		//Required for ifstream, ofstream
#include<string>		//Required for string, c_str():
#include <cmath>		// needed by function for square root sqrt()
#include <iomanip>		// setprecision
using namespace std;

double PlanetEscVel (double PlanetDiameter, double PlanetMass);//Programmer function prototype
     const double gravity = 6.67e-11;	//needed before main as global variable, all functions can use
     int
     main ()
{
  double PlanetDiameter, PlanetMass, PlanetRadius,PlanetEscVel,k;
  ifstream fin;			// input objects to get data from files
  ofstream fout;		// output object to send data to files
  string filename;		// as need for files.
  cout << "Please enter the name of the file you want to create.\n";
  cin >> filename;
  fout.open (filename.c_str ());
  cout <<
    "Please enter the first diameter and the mass(kgs) of the planet.\n";
  cin >> PlanetDiameter >> PlanetMass;
  for (k = 1; k <= 3; k = k + 1)
    {
      fout << PlanetDiameter << PlanetMass;
      cout << "Enter the diameter and mass of the next planet\n";
      cin >> PlanetDiameter >> PlanetMass;
    }
  fout.close ();
  cout <<
    " Now, enter the name of the file to read the diameters and masses of the planets. \n";
  cin >> filename;
  fin.open (filename.c_str ());
  if (fin.fail ())
    {
      cerr << "Could not open the file " << filename << endl;
      exit (1);
    }
// open the report output file
  fout.open ("MonaePlanetReport.txt");
// get first input data

  cout << "Planet Radius        Planet Mass       PlanetEscVel\n";	// headings screen & file echo
  fout << "Planet Radius        Planet Mass       PlanetEscVel\n";

  while (!fin.eof ())		//while not end of file
    {
      PlanetRadius = double (PlanetDiameter);	
       PlanetEscVel = sqrt (2 * gravity * PlanetMass * PlanetRadius) / PlanetRadius;
      PlanetRadius = PlanetDiameter / 2;
      cout << " " << PlanetRadius << "              " << PlanetMass << "             "  <<PlanetEscVel << endl;	// first echo to
      fout << " " << PlanetRadius << "        " << PlanetMass << "       " << PlanetEscVel << endl;
      fin >> PlanetRadius >> PlanetMass >> PlanetEscVel;	// get next value continue in while to eof()
    }

  fin.close ();
  fout.close ();
  return 0;
}				// end of main() 
@Blueshark,
You are writing random lines of code with no plan. It compiles, but that doesn't really make it much better.

You are also trying to do the whole lot in one go, rather than developing it slowly. You have three fundamental tasks:-
- read data from file
- manipulate this data (i.e. calculate escape velocity)
- write out a pretty table.
Do those ONE AT A A TIME!

Decide whether you are going to read planetary data from the keyboard or from file - at the moment you are trying to do both.

You have no function definition to match your prototype for calculating escape velocity.

Why don't you have a look at @againtry's code, which does everything required of your coursework except putting the escape-velocity calculation in a separate function. Since he has reduced that to one line your function is not going to be very big. (Note also that he has also dealt with the units anomaly in your input data.)
I am trying to have the data be entered into the keyboard, and create a file. Then read the data from said file and calculate the radius and escape velocity and provide a table as well. Also, @againtry's code will not open a file. If it helps to understand where I am coming from, here is the template I am trying and failing to mimic in my own project.

Data Entry: 5.5, 5, 4.5, 4, 0

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
#include<iostream>		//Required for cin, cerr
#include<fstream>		//Required for ifstream, ofstream
#include<string>		//Required for string, c_str():
#include <cmath>		// needed by function for square root sqrt()
#include <iomanip>		// setprecision
using namespace std;
double vaulter_velocity (double pole_height);	//Programmer function prototype
const double gravity = 9.8;	//needed before main as global variable, all functions can use
int
main ()
{
  double pole_height, velocity;
  ifstream fin;			// input objects to get data from files
  ofstream fout;		// output object to send data to files
  string filename;		// as need for files.
  cout << "enter the name of the file to store pole heights \n";
  cin >> filename;
  fout.open (filename.c_str ());
  cout << "enter the first height\n";
  cin >> pole_height;
  while (pole_height != 0)
    {
      fout << pole_height << endl;
      cout << "enter another height\n";
      cin >> pole_height;
    }
  fout.close ();
  cout <<
    " enter the name of the file to read pole heights for the vaulter\n";
  cin >> filename;
  fin.open (filename.c_str ());
  if (fin.fail ())
    {
      cerr << "Could not open the file " << filename << endl;
      exit (1);
    }
// open the report output file
  fout.open ("vaulterreport.txt");
// get first input data
  cout << "Clearance(m) Velocity needed(m/s)\n";	// headings screen & file echo
  fout << "Clearance(m) Velocity needed(m/s)\n";
  fin >> pole_height;
  while (!fin.eof ())		//while not end of file
    {
      velocity = vaulter_velocity (pole_height);	//Function call get proper velocity.
      cout << " " << pole_height << " " << velocity << endl;	// first echo to
      fout << " " << pole_height << " " << velocity << endl;
      fin >> pole_height;	// get next value continue in while to eof()
    }
  fin.close ();
  fout.close ();
  return 0;
}				// end of main()

/* velocity needed by vaulter to clear a pole of a given height*/
double
vaulter_velocity (double height)	//Function header.
{
  double temp_velocity;		//Declare local variables (not known to main)
  height = height + 1;		//add 1 meter for clearance
  temp_velocity = sqrt (2 * gravity * height);	//1/2m V2 =mgh
  return temp_velocity;		// function returns value of velocity need to clear poles
}				// end of the function 
Last edited on
I think @againtry's code is a rather better starting point than an unrelated code about pole vaulting. Not least, because it does precisely what is necessary: no more, no less.

On the other hand, your pole-vaulter file is full of "state-the-bleedin' obvious" comments like
while (!fin.eof ()) //while not end of file



Blueshark1 wrote:
code will not open a file

That's not true: what do you think the line
std::ifstream file ("planets.txt");
does?

He has even given you that input file to try. Put it in your run directory and it will produce the correct output.
Topic archived. No new replies allowed.