Simple Function

I want to know what's wrong here
why the result appear like that ?

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
  #include <iostream>

using namespace std ;

double GravitionalForce (double , double , double);
double const G = 0.00000000006674 ;

int main ()
{
	char ch = 'y' ;
	double mass1 , mass2 , distance , result ;
	while (ch == 'y')
	{
		cout << "Please Enter the following intformation : " << endl;
		cout << "Mass of body 1 (in Kg) : " ;
		cin >> mass1 ;
		cout << "Mass of body 2 (in Kg) : " ;
		cin >> mass2 ;
		cout << "The distance between them (in m): " ;
		cin >> distance ;
		result  = GravitionalForce ( mass1  ,  mass2 ,  distance);
		cout << "The Gravitional Force in newtons is : " << result << endl ;
		cout << "Any more caculations ? (Y/N)" << endl;
		cin >> ch ;
	}

}

double GravitionalForce (double mass1  , double mass2 , double distance)
{
	double result = (double) (mass1*mass2*G) / (distance*distance) ;
	return result ;

}
Last edited on
why doesn't it appear just like a normal number !!
closed account (3qX21hU5)
Why the results appear like what? We need more information then just "I want to know whats wrong", because that doesn't help us. What is the result suppose to be like? What result are you getting? Where do you think the problem is? What have you tried to fix it? These are some questions you can answer to help get a better reply :).

Otherwise we don't know really what to look for. Though I did see two things you are doing that you don't need to do.

1) In your function you are casting variables to (double), double result = (double) (mass1*mass2*G) / (distance*distance) ;. The thing is though is every variable in the expression already is a double so there is no need for the explicit cast. Also when you do want to do a explicit cast I would recommend you use static_cast<type to cast>(what you want to cast) instead.

2) In your function you are declaring a temporary variable to hold the result of your expression double result. Granted it isn't illegal to do this, it just isn't needed. You can just return the result of your expression like this

return (double) (mass1*mass2*G) / (distance*distance);

Anyways them are just two minor things I saw just by glancing, let us know what you are having trouble with exactly and we will try and guide you through it.
Last edited on
Last edited on
ok thx
consider the input
mass 1 = 10kg
mass 2 = 10kg
distance = 10m
the result is : 6.674e-011
Last edited on
thx
booradley60 and all
done with setprecision
That's scientific notation.
http://en.wikipedia.org/wiki/Scientific_notation
http://en.wikipedia.org/wiki/Scientific_notation#E_notation
Read that 'e' as 'times ten to the...' So your result is 6.674 times ten to the -11 power.
Last edited on
closed account (3qX21hU5)
You need to follow booradley60's link.

You still really didn't give enough info, but from what you said I gather you aren't getting enough precision. How precise do you want it to print?

1
2
cout.precision(15);
cout << "The Gravitional Force in newtons is : " << fixed << result << endl ;


This will give you 15 digits precision.


OUTPUT
Please Enter the following intformation :
Mass of body 1 (in Kg) : 10
Mass of body 2 (in Kg) : 10
The distance between them (in m): 10
The Gravitional Force in newtons is : 0.000000000066740
Any more caculations ? (Y/N)
n

Process returned 0 (0x0)   execution time : 8.680 s
Press any key to continue.



Again I'm not to sure what you want the output to be so not sure what exactly is going wrong for you so I am just taking guesses.
Last edited on
all what I wanted is just to output all of the result just like your output
and Thanks for you
Topic archived. No new replies allowed.