Defining Variables plus decimals.

Hello, I need help defining variables. I can't seem to get my code to output the variables in addition to the end-result "volume". Anybody have any tips that could help? Its greatly appreciated. Here is the question that I was assigned.

Write a program that accepts as input the mass, in grams, and density, in grams per cubic  centimeters, and outputs the volume of the object using the formula: volume = mass / density.  Format your output to two decimal places. 

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
#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std;  // So "std::cout" may be abbreviated to "cout"

int main()
{
	float g; //mass in grams
	float d; //density in grams/cubic centimeter
	float volume;

	{
		cout << "Please input the mass in grams: ";
		cin >> g;
		cout << "Please input the density in grams per cubic centimeter: ";
		cin >> d;
		volume = g / d;
		cout << "Volume:" << volume << endl;
	}
	return 0;

}
closed account (49iURXSz)
...get my code to output the variables in addition to the end-result "volume".


Take a look at the following:

cout << "Volume:" << volume << endl;

What does this code do? Once you have the answer to this, then the rest should fall into place.

Topic archived. No new replies allowed.