### Function not Outputting Correctly.Need opinion. plz.

Program not outputting right Not sure why. Function outputs `nan` when it is supposed to be a number.

This program is supposed to have a function definition that takes the arguments of two body masses and returns the gravitational force.

Gravitational Force formula is: g*mass_1*mass_2/ Distance^2
with "g" being a defined constant of : g=6.673*10^-8.

If someone could shed some light would be great-full.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include using namespace std; int main () { //function declaration double force(int mass_1, int mass_2); char ans; //short descirption cout << "Hello, this program will determine the gravitational\n"; cout << "force between two bodies with masses.\n"; do { using namespace std; int mass_1,mass_2,distance; double Grav_force = force(mass_1,mass_2);//making sure I can call function cout << "Enter the mass of the first body:\n"; cin >> mass_1; cout <<"Enter the mass of the second:\n"; cin >> mass_2; cout << "Finally enter the distance:\n"; cin >> distance; //outputtin results. cout << "The gravitational force of\n"; cout <> ans; } while (ans == 'Y' || ans == 'y'); { cout << "Program Terminated.\n"; } return 0; } double force(int mass_1,int mass_2) { int distance; //seting decimal place 2 spots over cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //declaring gravitational constant const float g = 6.673e-8; return ( g * mass_1* mass_2 / distance * distance ); }``````
If u guys can go through my code and identify my problem .. it would be great.
You might want to actually get the input before you call the function with variables that have junk in them. (Pay attention to your compiler's warnings!)

What value do you expect the variable on line 43 to have when you use it on line 50? (Pay attention to your compiler's warnings!)

force is no place to be adjusting the format settings for cout.

Your constant g is not correct.

You need some parentheses on line 50 to specify order of operation.
ok I called the function after I receive my input.
but now when I run the program it just outputs a bunch of zeros!.
I don't understand how my constant g is wrong?. can you elaborate pls?
This is my new code.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` #include using namespace std; int main () { //function declaration double force(int mass_1, int mass_2); char ans; //short descirption cout << "Hello, this program will determine the gravitational\n"; cout << "force between two bodies with masses.\n"; do { using namespace std; int mass_1,mass_2,distance; cout << "Enter the mass of the first body:\n"; cin >> mass_1; cout <<"Enter the mass of the second:\n"; cin >> mass_2; cout << "Finally enter the distance:\n"; cin >> distance; double Grav_force = force(mass_1,mass_2);//function call //seting decimal place 2 spots over cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //outputtin results. cout << "The gravitational force of\n"; cout <> ans; } while (ans == 'Y' || ans == 'y'); { cout << "Program Terminated.\n"; } return 0; } double force(int mass_1,int mass_2) { int distance; //declaring gravitational constant const float g = 6.673e-8; return ( (g * mass_1* mass_2 ) / ( distance * distance ) ); }``````
 I don't understand how my constant g is wrong?. can you elaborate pls?

It's not. I missed the "dynes" part.

 but now when I run the program it just outputs a bunch of zeros!

Keep in mind that the mass you indicate is in grams and the distance in centimeters. A small paper clip is about half a gram? You'll need some pretty big masses to see any gravitational force.
Last edited on
lol ok Thank For Your Help. I think I figured it out. :)
Topic archived. No new replies allowed.