My c++ program error

Can someone please help me fix my c++ program?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

class Square
{
public:    

Square(float side_l, float side_w);
Square();
~Square();  
void setLength(float side_l);
void setWidth(float side_w);
float getLength();
float getWidth();
double findArea();
double findPerimeter() ;  

private:
float length;
float width;
};    
const int NUMBEROFOBJECTS = 4;  

void main()    

{
Square box[NUMBEROFOBJECTS];
 
for (int pos = 0; pos < NUMBEROFOBJECTS; pos++)  
{
cout << "Information for box number " << pos + 1 << endl << endl;  
cout << "The length of the box is " << box[pos].getLength() << endl;
cout << "The width of the box is " << box[pos].getWidth() << endl;
cout << "The area of the box is " << box[pos].findArea() << endl;
cout << "The perimeter of the box is " << box[pos].findPerimeter() << endl << endl;  
}    

system("pause");
}    
void Square::setLength(float side_l)
{
length = side_l;
}  
void Square::setWidth(float side_w)
{
width = side_w;
}      
float Square::getLength()
{
return length;
}  
float Square:: getWidth()
{
return width;
}  
double Square::findArea()
{
return length * width;
}  
double Square::findPerimeter()
{    
return ((2 * length) + (2 * width));
}

Square::Square(float side_l, float side_w)
{
length = side_l; width = side_w;
}
Square::Square()
{
length = 1; width = 1;
}
Square::~Square()
{

}


Can someone please help me fix my c++ program?

Want to tell us what needs fixing?

And why this:
#include "stdafx.h"
?
Delete this line.
First, all your class functions/definitions should be before your main() function (right under your class declaration)

Second, you shouldn't include stdafx.h, it is unnecessary

Third, Square box[NUMBEROFOBJECTS]; is invalid (you could achieve the same thing by using vectors and the STL, plus using the STL will give you more results [http://www.cplusplus.com/doc/tutorial/polymorphism/ ]), other wise you need to use multiple class definitions ex. Square box1;Square box2

Fourth, you don't need to use double unless used for high precision calculations

Fifth, your main() function should be a type int and should return 0

Sixth, it is a good idea to use the code tags so it can be more easily read
Last edited on
First, all your class functions/definitions should be before your main() function

What? This isn't necessary.

Third, Square box[NUMBEROFOBJECTS]; is invalid

Again... what? What do you mean by "invalid"?
Last edited on
popa6200 wrote:
First, all your class functions/definitions should be before your main() function (right under your class declaration)

No. They're just fine where they're at.


popa6200 wrote:
Second, you shouldn't include stdafx.h, it is unnecessary

It is unnecessary. It isn't a problem, however, and may be a boon if the OP is using precompiled headers.


popa6200 wrote:
Third, Square box[NUMBEROFOBJECTS]; is invalid

No, it isn't.


popa6200 wrote:
Fourth, you don't need to use double unless used for high precision calculations

The default type for floating point operations should be double, unless you have some kind of pressing need to use float.

See: http://programmers.stackexchange.com/questions/188721/when-do-you-use-float-and-when-do-you-use-double


popa6200 wrote:
Fifth, your main() function should be a type int and should return 0

Presumably you mean the return type should be int. The type of main is different than its return type. In C++, return 0; is implied if nothing is returned from main.


popa6200 wrote:
Sixth, it is a good idea to use the code tags so it can be more easily read

I can definitely agree with that.
Topic archived. No new replies allowed.