Showing answer to 2 decimal places in code c++


This Content is from Stack Overflow. Question asked by codingnoobs

I am writing a Cube class which I have to calculate volume also. The class can have a user input of FLOAT length, width and height in between minimum 1 to maximum 45.00. Otherwise if user inputs any other numbers its all sides would be 1.

so it will be like

 #define LIMIT_UNDER 1
 #define LIMIT_UPPER 45.00

class Cube{
    float height;
    float width;
    float length;

// Write setter getters here

 // default constructor 

 // default constructor puts default values
 height = width = length= LIMIT_UNDER; 

 Cube(float cHeight, float cWidth, float cLength){
 height = cHeight;
 width = cWidth;
 length = cLength;

 float getVolume(){
 return height * width * 


 int main(){
   // Default constructor 

  Cube cube;

  // Parametized called

   Cube cube2(2.00f, 
  2.57f, 5.40f);

  // Show volume to 2 
  //decimal places as 45.00 
   //is limit that has two 
   //decimal places

  cout << getVolume() << 
  fixed << 

My question is about should I use all floats (5.40f etc) to two decimal places and the calculations? As I did in main using setPrecision function

Thanks please help


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?