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.
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
#include <iostream>
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 <<mass_1 << " and " << mass_2 << " is " << Grav_force << "dynes.";
        //asking user to try again.
        cout << "\n Would you like to calculate again?\n";
        cout << "(Y for yes anything else for no)\n";
        cin >> 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.
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

#include <iostream>
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 <<mass_1 << " and " << mass_2 << " is " << Grav_force << "dynes.";
        //asking user to try again.
        cout << "\n Would you like to calculate again?\n";
        cout << "(Y for yes anything else for no)\n";
        cin >> 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.