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>
usingnamespace 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
main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
usingnamespace 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;
}
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 { ...
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.