Help needed

I'm having issues with this program; here's the description of what I have to do:

The Galactic Research and Development Division has discovered a number of new small lifeforms species inside the trunks of trees native to the exoplanet Caliente-116. They are carefully studying these creatures and recording the several characteristics so that these can be compared to lifeforms found on exoplanet Caliente-103. The exoplanet has 12 continents labeled with a letter, A through L. We have been asked to produce a program that records the data gathered on the lifeforms and computes the density of the lifeform.

The program should ask the user to enter:

the lifeform number (lifeforms are number starting with 116001),
letter indicating which continent on the exoplanet the tree was from
the height or length in centimeters,
the weight in grams, and
number of liters displaced when the creature was placed in the lifeforms status tank.
The program should then calculate and display the density (kg/m3) for the lifeform.

The formulas used are as listed in the program below. I'm not sure what I'm doing wrong, can someone help please?

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
#include <iostream>
using namespace std;

int main()
{
	double lifeFormNumber = 0.0;
	double lifeFormWeight = 0.0;// In grams
	double lifeFormHeight = 0.0;// In centermeters
	double lifeFormDisplacement = 0.0;

	char continent;
	
  
	cout << "Please enter the lifeform number: ";
	cin >> lifeFormNumber;
	cout << "Please enter the continent letter where the tree was found: ";
	cin >> continent;
	cout << "Please enter the lifeform's height: ";
	cin >> lifeFormHeight;
	cout << "Please enter the lifeform weight: ";
	cin >> lifeFormWeight;
	

	double weight = lifeFormWeight / 1000;
	double displacement = lifeFormDisplacement / 1000;
	double lifeFormDensity = weight / displacement;

	cout << "\nThe lifeform weight is: " << weight << " kg." << endl;
	cout << "The lifeform displacement is: " << displacement << " m^3." << endl;
	cout << "The density of this life form is: " << lifeFormDensity << " kg/m^3." << endl;
    
    cout << "Please press ENTER to end program."; 
    cin.sync();
    getchar();
    return 0;
} 
Last edited on
I would expect you to save all the different inputs, and do the calculations at the end. It doesn't make sense to do density on a single datapoint.

You need some kind of loop that reads all the data points and stores them, then you do the calculation at the end.
I haven't gotten into loops yet.

your displacement calculation is wrong:
line 9 - lifeFormDisplacement = 0.0;
line 25 - displacement = lifeFormDisplacement / 1000; this will always be 0;
line 26 - weight / displacement; Divide by 0 error.
Last edited on
So it seems that i forgot to ask the user to input the lifeform displacement, that should be fixed now. Take a look at the program now please.

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
#include <iostream>
using namespace std;

int main()
{
	double lifeFormNumber; //Lifeforms are number starting with 116001
	double lifeFormWeight; // In grams
	double lifeFormHeight; // In centermeters
	double lifeFormDisplacement; // Liters

	char continent;
	
  
	cout << "Please enter the lifeform number: ";
	cin >> lifeFormNumber;
	cout << "Please enter the continent letter where the tree was found: ";
	cin >> continent;
	cout << "Please enter the lifeform's height: ";
	cin >> lifeFormHeight;
	cout << "Please enter the lifeform weight: ";
	cin >> lifeFormWeight;
	cout << "Please enter the number of liters that the lifeform was displaced in: ";
        cin >> lifeFormDisplacement;

	double weight = lifeFormWeight / 1000;
	double displacement = lifeFormDisplacement / 1000;
	double lifeFormDensity = weight / displacement;

	cout << "\nThe lifeform weight is: " << weight << " kg." << endl;
	cout << "The lifeform displacement is: " << displacement << " m^3." << endl;
	cout << "The density of this life form is: " << lifeFormDensity << " kg/m^3." << endl;
    
    cout << "Please press ENTER to end program."; 
    cin.sync();
    getchar();
    return 0;
} 


I used a calculator to check the calculations and they seem to be right.
Last edited on
Topic archived. No new replies allowed.