gravity

I'm trying to compute the gravitational force of two objects.
I can get it to compile but can't get the program to display the result of the Constant or the expression (gForce =G * m1 * m2 / R^2). I've been looking at this for a few hours now so any info will help.

Here what I got 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

#include <iostream>
#include <iomanip>
#include <cmath> 


using namespace std;

const double GRAVITY = 0.000000000066742;

double gravForce(double mass1,double mass2,double dist)

{
          
     double gForce;
           
     gForce = (GRAVITY * mass1 * mass2)/ pow(dist,2);
     
     cout << fixed << showpoint << setprecision(2) << endl << endl << endl;
     
     cout << "RESULTS:" << endl;
     
     cout << "Mass of the 1st object is:"<< mass1 << " kg" << endl;
     
     cout << "Mass of the 2nd object is:"<< mass2 << " kg" << endl;
     
     cout << "Distance is:" << dist << " meters"<< endl;
     
     cout << "The Gravitational Constant is :" << GRAVITY << " Nm^2/kg^2" << endl;
     
     cout << "The Gravitational Force is:" << gForce <<" Newtons"<< endl;
     return gForce;
     }
int main() 
{
  double mass1,mass2,dist,force;
       
cout << "Computation of Gravitational Force between 2 objects " << endl;
cout << setw (24)<<"by"<<endl;

cout << setw(30)<<"Robert Fisher"<< endl<<endl;

cout << "Enter the mass of the first object (in kg): ";
    cin >> mass1; 
    
    cout << endl << "Enter the mass of the second object (in kg): ";
    cin >> mass2 ; 
    
    cout  << endl <<"Enter the distance between the centers of the two objects(in meters):";
    cin >> dist;
    
force = gravForce(mass1,mass2,dist);

cout << endl;

system("PAUSE");
  
}

It could be that 0.000000000066742 is too small for a double (I don't think it is but you never know). Try putting in 1.0 as a test and see if it works.
There's no problem with double. The problem is with how it's being printed.

This:

cout << fixed << showpoint << setprecision(2) << endl << endl << endl;

You're telling the computer to print a fixed point number, but only 2 digits of precision. So really small numbers like 0.000045 don't get printed properly because it's only printing the first 2 digits of precision (ie: 0.00)

Try removing that line.
I tried the fixes and can display the constant now but "gforce" is not working is my expression invalid? gForce = (GRAVITY * (mass1 * mass2) / (dist*dist))
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
#include <iostream>
#include <iomanip>
#include <cmath> 


using namespace std;

void gravForce()

{
       cout << "Computation of Gravitational Force between 2 objects " << endl;
cout << setw (24)<<"by"<<endl;

cout << setw(30)<<"Robert Fisher"<< endl<<endl;   
   
     return;
     }
int main() 
{
  const double GRAVITY = 6.6742E-11;
  
  double mass1,mass2,dist,force;       

  double gForce;//Gravitational force variable 
           
    gForce = (GRAVITY * (mass1 * mass2) / (dist*dist));
     
    gravForce();// Call function
             
    cout << "Enter the mass of the first object (in kg): ";
    cin >> mass1; 
    
    cout << endl << "Enter the mass of the second object (in kg): ";
    cin >> mass2 ; 
    
    cout  << endl <<"Enter the distance between the centers of the two objects(in meters):";
    cin >> dist;
         
     cout << "RESULTS:" << endl;
     
     cout << "Mass of the 1st object is: "<< setw(16) << mass1 << " kg" << endl << endl;
     
     cout << "Mass of the 2nd object is: "<<setw(16) << mass2 << " kg" << endl << endl;
     
     cout << "The Distance is: " << setw(26) << dist << " meters" << endl << endl;
     
     cout << "The Gravitational Constant is : " <<setw(9)<< GRAVITY << " Nm^2/kg^2" << endl << endl;
     
     cout << "The Gravitational Force is: " << setw(14) << gForce <<" Newtons"<< endl;


cout << endl;

system("PAUSE");
  
}







You calculate gForce before you allow the user to enter anything. On line 26 values of mass1 mass2 and dist are not defined. Move this line to line 38.

You're not doing this in the first piece of code. Why did you change it?
Last edited on
thanks,I didn't realize I did that the program is working great now.
Topic archived. No new replies allowed.