Vote count:
0
I'm trying to get this code to work to calculate surface area and volume of a box. It compiles but doesn't output the correct data. I think maybe the problem is within the void Box:: parts but have hit a wall. Or it could be the Height = Height but I couldn't get it to run without error any other way.
Here is the code:
#include <iostream>
#include "Box.hpp"
using namespace std;
int main()
{
Box box;
double boxHeight, boxWidth, boxLength;
cout << "This program will tell you the surface area and volume of a box" << endl;
cout << "Please enter the height " << endl;
cin >> boxHeight;
cout << "Please enter the width " << endl;
cin >> boxWidth;
cout << "Please enter the length" << endl;
cin >> boxLength;
box.setHeight();
cout << "The surface area is " << box.getSurfaceArea() << endl;
cout << "The volume is " << box.getVolume() << endl;
return 0;
}
void Box::setHeight(){
if(Height< 0)
cout << "Error. Must enter a positive number " << endl;
else
Height = Height;
}
void Box::setLength(){
if(Length< 0)
cout << "Error. Must enter a positive number " << endl;
else
Length = Length;
}
void Box::setWidth(){
if(Width< 0)
cout << "Error. Must enter a positive number " << endl;
else
Width = Width;
}
double Box::getSurfaceArea(){
return 2*(Length*Width) + 2*(Length*Height) + 2*(Width + Height);
}
double Box::getVolume(){
return Length*Width*Height;
}
And the hpp file is this:
#ifndef BOX_HPP_INCLUDED
#define BOX_HPP_INCLUDED
class Box
{
public:
void setHeight();
void setWidth();
void setLength();
double getVolume();
double getSurfaceArea();
private:
double Length;
double Width;
double Height;
};
#endif
asked 1 min ago
Surface Area / Volume
Aucun commentaire:
Enregistrer un commentaire