Getting some negative values for volume and density. . .

Hi all,

I'm trying to write a program that reads the data below from an input file, planetsinput.txt, computes the density for each planet and prints the answer to the screen.

I currently have a working program that's printing out values, but for several of them, I'm getting negative values for density and/or volume, which is confusing.

Any help would be much appreciated.

NOTE: Jupiter and Venus are the planets with issues. Apologies for the weird output formatting, HTML has a problem with my line.


Here is the raw data from the original file...

1
2
3
4
5
6
7
8
9
Mercury	2,440	3.30 x 1023
Venus	6,052	4.87 x 1024
Earth	6,371	5.97 x 1024
Mars	3,390	6.39 X 1023
Jupiter	69,911	1.90 x 1027
Saturn	58,232	5.68 x 1026
Uranus	25,362	8.68 x 1025
Neptune	24,622	1.02 x 1026
Pluto	1,186	1.31 x 1022


Here is my 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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void Input();

int main()	{
    Input();
}


void Input()	{
    
    string Planet[9];
    int Radius[9];
    float Mass[9];
    const double pi = 3.14159;
    int i=0;
    
    double volume = ((4)*(pi)*(Radius[i]*Radius[i]*Radius[i]))/(3);
    double density = Mass[i]/volume;
    
    ifstream fin;
    
    fin.open("planetsinput.txt");
    if(fin.fail())	{
        cerr << "couldn’t open the file!" << endl;
        exit(1);
    }
    
    for (i=0; i<9; ++i)	{
        fin >> Planet[i] >> Radius[i] >> Mass[i];
        
        volume = ((4)*(pi)*(Radius[i]*Radius[i]*Radius[i]))/(3);
        density = Mass[i]/volume;
        
        cout << Planet[i] << endl;
        cout << "--------" << endl;
        cout << "Radius: " << Radius[i] << endl;
        cout << "Mass: " << Mass[i] << endl;
        cout << "Volume: " << volume << endl;
        cout << "Density: " << density << "\n\n" << endl;
    }
    
    fin.close();
}


And here is the output I'm getting right now:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

Mercury
Radius: 2440
Mass: 3.3e+23
Volume: 6.87749e+09
Density: 4.79826e+13


Venus
--------
Radius: 6052
Mass: 4.87e+24
Volume: -7.00988e+09
Density: -6.94734e+14


Earth
--------
Radius: 6371
Mass: 5.97e+24
Volume: 3.7639e+09
Density: 1.58612e+15


Mars
--------
Radius: 3390
Mass: 6.39e+23
Volume: 1.27135e+09
Density: 5.02614e+14


Jupiter
--------
Radius: 69911
Mass: 1.9e+27
Volume: -5.6568e+09
Density: -3.35879e+17


Saturn
--------
Radius: 58232
Mass: 5.68e+26
Volume: 6.70386e+09
Density: 8.47273e+16


Uranus
--------
Radius: 25362
Mass: 8.68e+25
Volume: 5.61276e+09
Density: 1.54648e+16


Neptune
--------
Radius: 24622
Mass: 1.02e+26
Volume: 7.96262e+09
Density: 1.28099e+16


Pluto
--------
Radius: 1186
Mass: 1.31e+22
Volume: 6.98783e+09
Density: 1.87469e+12


Program ended with exit code: 0
Last edited on
You are probably getting a negative value because in some multiplications the value exceeds the limit of double.
in some multiplications the value exceeds the limit of double.

Or more likely, exceeds the value of an integer, such as int Radius[9];
Exceeded the value of int, indeed. Thanks so much guys.
Topic archived. No new replies allowed.