### A little help with calculating the area of a square and circle

Hi all,

I am having a bit of trouble when I run my program calculating the area of a square and circle. Everything works out fine but if I have the length of one side be 40 it comes back with area is 1600Calculating the area. How do I get that calculating the area off my screen and just have it say your area is 1600. I never even put that line in the cout line of my coding. The coding is a little long so I'm sorry if you have to read a lot but I could really use some help.

 ``123456789101112131415161718`` ``````circle.h #ifndef CIRCLE_H_INCLUDED #define CIRCLE_H_INCLUDED #include using namespace std; class Circle { public: Circle (int radius =0); int getRadius() const; void setRadius(int radius); double getArea() const; private: int radius; }; #endif // CIRCLE_H_INCLUDED ``````

 ``1234567891011121314151617181920`` ``````square.h #ifndef SQUARE_H_INCLUDED #define SQUARE_H_INCLUDED #include using namespace std; class Square { public: Square (int length =0); double getLength(); void setLength(int length); double getArea(); private: double length; }; #endif // SQUARE_H_INCLUDED ``````

 ``1234567891011121314151617`` ``````square.cpp #include #include "Square.h" using namespace std; Square::Square(int len) { length = len; } double Square::getLength() const { return length; } double Square::getArea() const{ return length * length; } ``````

 ``12345678910111213141516171819202122`` ``````circle.cpp #include #include "Circle.h" using namespace std; Circle::Circle(int r) { radius = r; } int Circle::getRadius() { return radius; } void Circle::setRadius(int r) { radius = r; } double Circle::getArea() { return radius * radius * 3.14; } ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````main.cpp #include #include "Circle.h" #include "Square.h" using namespace std; int main() { double radius = 0; double length = 0; Square square; Circle circle; int option; do { cout << "Calculating the Area" << endl; cout << "Object you are getting the area of:" << endl; cout << "1: Square" << endl; cout << "2: Circle" << endl; cout << "3: Exit" << endl; cout << "Please enter your choice: "; cin >> option; switch(option) { case 1: { cout << "Please enter the length of one side of the square: " << endl; cin >> length; Square square(length); cout << "Your area of the square is: " << square.getArea(); break; } case 2: { cout << "Please Enter the radius of the circle: "; cin >> radius; circle.setRadius(radius); cout << "Your area of the circle is: " << circle.getArea(); } } } while (option != 3); cout << "Bye!" << endl; } ``````
Not sure what the exact question is.

Line 33 in main.cpp
 `` `` ``cout << "Your area of the square is: " << square.getArea();``

is the one which generates the output:
 `Your area of the square is: 1600`

Then control passes to the start of the do loop at line 18 and adds the words "Calculating the Area" on the same line.

If you add a newline after printing the area it becomes more legible.
 `` `` ``cout << "Your area of the square is: " << square.getArea() << '\n';``

Put a new line (or a couple of new lines) after printing out the area. For example:
 ``12`` ``````// cout << "Your area of the square is: " << square.getArea(); cout << "Your area of the square is: " << square.getArea() << "\n\n" ;``````

Also note that the const qualifier eg. `int getRadius() const;`
must be present in both the declaration and the definition. eg. `int Circle::getRadius() const { ... `

Thank you guys very much.

I am a beginner in the world of C++ and I am just getting the hang of how this works; so thank you guys very much. I compiled it and everything is working as it should with a few minor changes.
Topic archived. No new replies allowed.