0

So I've been trying to make a circle class that can calculate the Area, Diameter, and Circumference of a circle. I wish to be able to do this so I can easily just input the raidus in my main and print the values out. I tried making a friend function, but I believe I have done this wrong? what is wrong with my code, and what should I change? Thank you!

#include "stdafx.h"
#include <iostream>
using namespace std;

class Circle {
public:
    int pi, radius, diameter, circumference, area;
    Circle() { radius, diameter, circumference, pi, area = 0; };
    istream& operator >>(istream& in, Circle& c);
private:

    double pi = 3.14;
    double radius = 0;
    double area =  pi * radius * radius
    double diameter = radius * 2;
    double circumference = 2 * pi * radius;
};

void main()
{

    cout << "enter radius radius" << endl; 
    cin << c.radius;
    cout << c.diameter;
    cout << c.circumference;
    cout << c.area;

    system("pause");

}

Edited by Name_1

4
Contributors
4
Replies
66
Views
3 Months
Discussion Span
Last Post by David W
1

There are a lot errors with your code. First off you have two sets of class vaiables in Circle with the same name:. You can't do that. You compiler should complain I would think. This will cause issues with your constructor. Either us a default constructor (no arguments) and add a setter method. Additionally you can have another constructor like this: Circle( double radius, double diameter, double circumference )'
Ignore PI in constructor. It is a global constant. Also, area is a computable value. You only need to compute it the first time when needed. Think about this, retry to write your code, and then post here again. I could give you the corrections directly, but what will you learn then?

0

So, before I continue, thank you for your offering your help! Anyway after reading through you're comments this is what I came up with. I took the constructor you gave me and put it into public. While looking through the error list I saw that all of my main objects had a similar error: "left of _____ must have class/struct/union" with this in mind I tried putting Circle before said objects instead of c., and removed the earlier "friend" attempt. What can I do to fix this? Thank you again!

#include "stdafx.h"
#include <iostream>
using namespace std;

class Circle {
public:
    Circle(double radius, double diameter, double circumference);

    double pi = 3.14;
    double radius = 0;
    double diameter = radius * 2;
    double circumference = 2 * pi * radius;
    double pi * radius * radius;
};

void main()
{

    cout << "enter radius radius" << endl; 
    cin << Circle.radius;
    cout << Circle.diameter;
    cout << Circle.circumference;
    cout << Circle.area;

    system("pause");

}
0

You forgot area = on line 13.
The Circle class you defined is a type, an object. It only comes alive if you call its constructor.
You didn't do that in your main(). At least have something like Circle c; in main.
Now c has come 'alive' and is 'constructed'. You can now call its fields or methods.
The variable c is OK here, but in a 2000 lines of code c is not OK, give it a more meaningful name like MyCircle or something.

2

Also ... in C++ you usually do NOT want redundant parameters in a class!

For example, in a circle, if you know one of: radius, diameter, circumference, area
you can calculate all the other ...

And note that PI (pi) is a constant value defined as the ratio of the circumference of any circle to its diameter,

You may like to see this demo code that also loops until
the user inputs valid numeric input
and also
as long as more() returns true.

// test_class_Circle.cpp //  // 2017-07-30 //

// you had ...

/*
#include "stdafx.h"
#include <iostream>
using namespace std;

class Circle
{
public:
    Circle(double radius, double diameter, double circumference);
    double pi = 3.14;
    double radius = 0;
    double diameter = radius * 2;
    double circumference = 2 * pi * radius;
    double pi * radius * radius;
};

void main()
{
    cout << "enter radius radius" << endl;
    cin << Circle.radius;
    cout << Circle.diameter;
    cout << Circle.circumference;
    cout << Circle.area;
    system("pause");
}
*/

// This is a nice student way it could/should be done in C++ //

#include <iostream>
#include <string>
#include <cmath> // re. acos //

const double PI = acos(-1.0 );

class Circle
{
private:
    double radius;
public:
    // default ctor...
    Circle() : radius(0) {}
    // ctor with passed in radius ...
    Circle( double radius ) : radius(radius) {}

    double get_radius() const { return radius; }
    double get_diameter() const { return radius * 2; }
    double get_circumference() const { return 2.0 * PI * radius; }
    double get_area() const { return PI * radius * radius; }
} ;

bool more() // deafaults to yes / more in jinless n or N entered //
{
    std::cout << "More (y/n) ? ";
    std::string line;
    getline( std::cin, line );
    if( line.size() != 0 && (line[0] == 'n' || line[0] == 'N') )
        return false;
    // else //
    return true;
}

int main() // NOTE that in standard C/C++ main returns an int //
{
    using std::cout;
    using std::cin;

    for(  ; ; )
    {
        cout << "Enter radius: ";
        double r;
        if( cin >> r && cin.get() == '\n' )
        {
            if( r > 0.0 )
            {
                Circle cir( r ); // construct a Circle cir with radius r //
                cout << "cir.get_radius() = " << cir.get_radius() << '\n'
                     << "cir.get_diameter() = " << cir.get_diameter() << '\n'
                     << "cir.get_circumference() = " << cir.get_circumference() << '\n'
                     << "cir.get_area() = " << cir.get_area() << '\n';
                if( ! more() )
                    break;
            }
            else cout << "Only positive numbers are valid input here.\n";
        }
        else // bad input ... so get again
        {
            cin.clear(); // clear cin error flags from bad numerice input //
            cin.sync();  // 'flush' the cin input stream //
            cout << "Only numbers are valid input here.\n";
        }
    }
}

Edited by David W

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.