Outcome not as expected

I am new to C++ and I have a program that is suppose to find the area of a Circle or a Square.

The program will run and request input, but the output is not what should be expected for finding the area. For example, choosing square and inputting 10 for the length of one side should output an area of 100. Instead it outputs 3.74921e+018

If you see something else that could or should be improved, I would appreciate the feedback.

Thank you

Shape base header file
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
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

#include <string>

class Shape        //Class name is "Shape"
{
    private:
        //Data member declarations
        std::string shapeID;        //ID or name of instance
        std::string shapeName;      //Circle, Square, Rectangle
        std::string unitOfMeasure;  //Inches or centimeters

    public:
            //Member function prototypes
        Shape();

            /*  Constructor for derived class will pass a constant string
                to the base class's constructor, so 2nd param is const.*/
        Shape(std::string &, const std::string &, std::string &);

        /*  The "get" functions are declared with trailing const because
            they are not allowed to modify any variables in the class.
        */
        std::string getShapeID() const;
        std::string getShapeName() const;
        std::string getUnitOfMeasure() const;

        /*  Each derived class must provide its own getPerimeter().
        */
        virtual double getArea() const = 0;

        void setShapeID(std::string &);
        void setShapeName(std::string &);
        void setUnitOfMeasure(std::string&);
        void print();           //Prints the information
};

#endif // SHAPE_H_INCLUDED

Shape .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//#include <iostream>
#include "Shape.h"

Shape::Shape() {}

Shape::Shape(std::string &name, const std::string &shape, std::string &unit)
{
    shapeID = name;
    shapeName = shape;
    unitOfMeasure = unit;
}
    std::string Shape::getShapeID() const { return shapeID; }

    std::string Shape::getShapeName() const { return shapeName; }

    std::string Shape::getUnitOfMeasure() const { return unitOfMeasure; }

    void Shape::setShapeID(std::string &name) { shapeID = name; }

    void Shape::setShapeName(std::string &shape) { shapeName = shape; }

    void Shape::setUnitOfMeasure(std::string &unit) { unitOfMeasure = unit; }


Header file for Square
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED

#include "Shape.h"  //Base class

class Square : public Shape   //Class Square inherits from Shape
{
    private:
        //Data member declarations
        double side;

    public:
        //Constructor: shapeID, side, unit of measure
        Square(std::string &, double, std::string &);

        // Virtual function from parent must be declared in class
        double getArea() const;

        //Member function prototypes
        double getside() const;     //Gets the side
        void setside(double);      //Sets the side
};

#endif // SQUARE_H_INCLUDED 

Square .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Square.h"  //Need header for Square in same dir as .cpp

using namespace std;

//Set Side
Square::Square(string &name, double si, string &unit) :
    Shape(name, "Square", unit)
    {
        side = si;
    }

    //Calculates Area
    double Square::getArea() const
    {
        return side * side;
    }

    double Square::getside() const { return side; }

    void Square::setside(double si) { side = si; }


Circle header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED

#include "Shape.h"  //Base class

class Circle : public Shape      //Class Circle inherits from Shape
{
    private:
        //Data member declarations
        double radius;

    public:
        // Constructor: shapeID, radius, unit of measure
        Circle(std::string &, double, std::string &);

        // Virtual function from parent must be declared in class
        double getArea() const;

        //Member function prototypes
        double getradius();     //Gets the Radius
        void setradius(double);   //Sets Radius (rad)
};

#endif // CIRCLE_H_INCLUDED 


Circle .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Circle.h" //Need header for Circle in same dir as .cpp

using namespace std;

const double PI = 3.14159;

 Circle::Circle (string &name, double r, string &unit) :
    Shape(name, "Circle", unit)
    {
        radius = r;
    }

    double Circle::getradius() { return radius; }

    double Circle::getArea() const
    {
        return (PI * (radius * radius));
    }

    void Circle::setradius(double r) { radius = r; }



main.cpp file
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
#include <iostream>
#include "Circle.h"  //Defines Circle
#include "Square.h"  //Defines Square

int main()
{   // This starts the main function
    using namespace std;

    char letter;

    //This array calls for the user to select either 1 for Circle or 2 for Square
        cout << "To find the area of a Circle or a Square," << "\n";
        cout << "select c for Circle, or s for Square: ";
        {
            cin >> letter;
                {
                    if (letter == 'c' )
                    {
                        double rad;
                        cout << "\nWhat is the radius in inches? ";
                        cin >> radius;
                        string name = "myCircle";
                        string unit = "in";
                        Circle C(name, 'radius', unit);
                        cout << "\nThe area of the circle is " << C.getArea() << " inches." << endl;
                    }

                    else if (letter == 's')
                    {
                        double side;
                        cout << "\nWhat is the length of one side in inches? ";
                        cin >> side;
                        string name = "mySquare";
                        string unit = "in";
                        Square S(name, 'side', unit);
                        cout << "\nThe area of the square is " << S.getArea() << " inches." << endl;
                    }
                }
        }
    cout << "\nGoodbye.\n";     //Just saying goodbye to the user
    return 0;
}   // This ends the main function
Last edited on
1
2
Circle C(name, 'rad', unit);
Square S(name, 'side', unit);


Look at lines 24 and 35, notice anything? Why do you have single quotes around variable names? Fixing this will fix your issues. The reason is that you are not passing the variables but some rubbish value, in your case the value 193628682 will be passed to the constructor of Square no matter what values the user inputs.


To find the area of a Circle or a Square,
select c for Circle, or s for Square: s

What is the length of one side in inches? 10

The area of the square is 100 inches.

Goodbye.

Last edited on
I removed the single quotes around the variable names. I also noticed I had "rad" instead of radius in the main.cpp, so updated that too.

It is working, thank you ChajusSaib.
Last edited on
Topic archived. No new replies allowed.