0

I have been trying to calculate velocity of satellite by formula v = -1/2 * GMm/r using c++ it compiled successfully but after getting all input it dies not show result and terminate abruptly. I tried to work around by re writing code again but all gone in vain.

 #include <iostream>
 using namespace std ;
 #define G 6.672 

 float calculate(float , float , float) ;

 int main(void)
 {
  float aa ;
  aa = (1/2) ;
  cout << aa ;
  cout << "Total energy of satellite " << endl ;
float ene ,m , M , r ;
cout << "Enter masses and radius " << endl ;
cin >> m  ;
cout << "2nd mass" << endl ;
cin >> M ;
cout << "radius " ;
cin >> r ;

ene = calculate(m ,M , r);
cout << ene ;

return 0 ;
system("PAUSE") ;
}
float calculate(float a , float b , float c)
{
      float result ;
      result = 0.5 * (G * a * b)/c ;
      return result; 
}
Arsala Kamal
  • 51
  • 1
  • 3
  • 9
  • Have you tried printing m, M and r? if r was 0 you'd get a division by 0. PS: horrible variable naming conventions m and M? I know it's possible, but it makes for hard to read/maintain code. – John3136 Jun 12 '13 at 06:16
  • 3
    It terminates because you have the system("PAUSE") after returning. It will never get called and the program terminates after it printed its output. – arne Jun 12 '13 at 06:16
  • As a side note, your line `aa=(1/2)` is equivalent to `aa=0` since 1/2 is an integer division, and only after that you get the conversion to float. See(http://stackoverflow.com/questions/8906722/c-programming-division). – mars Jun 12 '13 at 08:12

1 Answers1

0

system("PAUSE") ; is not reached as it occurs after return 0 ; which immediately exits the method.

However, you should avoid using system("PAUSE") in favour of blocking, waiting for input to come in as the following link suggests: http://www.gidnetwork.com/b-61.html

Patashu
  • 21,443
  • 3
  • 45
  • 53