Question

I want to put fixed point notation to my program but when I do the final output is always 0.00

I tried to put this

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

in different parts of the program, but nothing changes.

Here is my program:

#include <iostream>
#include <cmath>

using namespace std;

void get_input(double& body1, double& body2, double& distance);
void get_output(double m1, double m2, double d);
double compute_GAF(double m1, double m2, double d); //takes the masses of two bodies and the distance and computes gravitational attractive force between them
const double G = 6.673 * (1/pow(10, 11)); //global constant for Gravity

int main()
{
    double m1, m2, d;
    char ans;

    do
    {

        cout << "Thanks for using the Gravitational Attractive Force Calculator\n";
        cout << endl;

        get_input(m1, m2, d);

        get_output(m1, m2, d);


        cout << "Do you want to calculate again? (Y/N)" << endl;
        cin >> ans;
        cout << endl;

    } while (ans == 'y' || ans == 'Y');

    return 0;
}

void get_input(double& body1, double& body2, double& distance)
{
    using namespace std;

    cout << "Enter the mass of the two objects with a space in between:\n";
    cin >> body1 >> body2;
    cout << "Enter the distance between the objects:\n";
    cin >> distance;
    cout << endl;

    return;
}

void get_output(double m1, double m2, double d)
{
    using namespace std;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "The Gravitational Attractive Force of the two objects is " << compute_GAF(m1, m2, d);

    if (compute_GAF(m1, m2, d) == 1)
        cout << " dyne.\n";
    else
        cout << " dynes.\n";
    cout << endl;
    return;
}

double compute_GAF(double m1, double m2, double d)
{
    double F;

    F = (G*m1*m2) / pow(d, 2);

    return F;
}

Sorry for my bad english and my bad programming skills.

Était-ce utile?

La solution

You are probably setting the precision too low for the number being calculated (i.e. you need more decimal places).

Try cout.precision(20); instead, for example.

Also, you don't need those using namespace std; statements inside your function bodies. Just once at the top will suffice.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top