First C++ Assignment, HELP!!! Decimal & $ Symbol

Feb 5, 2021 at 2:11am
Hi everyone!

I'm new to c++ this is my first assignment, so far I think I did good, i hope! But for my last code I'm suppose to show the results at 2 decimal points and the $ symbol. Please help!!! This is my code so far.

#include <iostream>
using namespace std;

int main()
{
const double PI = 3.14;
const double priceofSoil = 7.97;
const double priceOfGravel = 3.98;

// Display the header
cout << "Miguel Lopez" << endl << "Program 1 Greenhouse pot" << "\n";
cout << "Calculating the costs of soil and gravel for pots!" << "\n";

cout << "\n";

// Get diameter
float diameter;
cout << "Please enter the top diameter in inches: ";
cin >> diameter;

//How many pots to be calculated
float pots;
cout << "Please enter the number of pots: ";
cin >> pots;

// Calculate the bottom diameter
// Bottom diameter = top diameter - 2
float bottomDiameter;
bottomDiameter = diameter - 2;

// Calculate average diameter
// Average diameter = top diameter + bottom diameter / 2
float averageDiameter;
averageDiameter = (diameter + bottomDiameter)/2;

// Calculate height
// height = diameter
float height;
height = diameter;

// Calculate average radius
// Average radius = averageDiameter/2
float averageRadius;
averageRadius = averageDiameter/2;

// Calculate volume of pots in cubic feet
// volume = PI * Radius * Radius * Height
float volume;
volume = (PI * averageRadius * averageRadius * height)*pots/1728;
cout << "Volume of pots in cubic feet is: " << volume << endl;

// Calculate volume of soil and gravel in pot
// 80% of volume is soil and gravel
float volumeOfGravelAndSoil;
volumeOfGravelAndSoil = 0.80 * volume;

// Volume of Gravel
// Volume of gravel = 1 inch of height * radius * radius * PI
float volumeOfGravel;
volumeOfGravel = (1 * PI * averageRadius * averageRadius)*pots/1728;
cout << "The volume of Gravel in cubic feet is: " << volumeOfGravel << endl;

// Cost of Gravel
// Cost of Gravel = priceOfGravel * volumeOfGravel
float costOfGravel = priceOfGravel * volumeOfGravel;
cout << "The cost of the gravel needed for the pots is: " << costOfGravel << endl;

//Volume of Soil
// Volume of soil = volumeOfGravelandSoil - volumeOfGravel
float volumeOfSoil;
volumeOfSoil = (volumeOfGravelAndSoil - volumeOfGravel);
cout << "The volume of Soil in cubic feet is: " << volumeOfSoil << endl;

//Cost of Soil
float costOfSoil;
costOfSoil = volumeOfSoil * priceofSoil;
cout << "The cost of the soil needed for the pots is: " << costOfSoil << endl;
cout << "\n";

//Total for gravel and soil
float totalOfGravelAndSoil;
totalOfGravelAndSoil = costOfSoil + costOfGravel;
cout << "The total cost for both soil and gravel is: " << totalOfGravelAndSoil << endl;

cout << "\n";

cout << "Thank you! Goodbye";

cout << "\n";

return 0;
}
Feb 5, 2021 at 2:28am
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

The easiest way to show a currency symbol, like a $ dollar sign, is to put it in the string you output.

std::cout << "The total cost for both soil and gravel is: $" << totalOfGravelAndSoil << '\n';

C++11 (and later) has an I/O manipulator for specifying the currency symbol specific to a currency type or country:
https://en.cppreference.com/w/cpp/io/manip/put_money

The same header, <iomanip>, lets the programmer specify how many decimal digits a floating point numeric value displays, the precision.
https://en.cppreference.com/w/cpp/io/manip/setprecision

Topic archived. No new replies allowed.