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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
square.h
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include <iostream>



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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
square.cpp
#include <iostream>
#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;
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
circle.cpp
#include <iostream>
#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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
main.cpp
#include <iostream>
#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:
1
2
// 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.