PLEASE HELP

it only calculates one circle and i cant fix it
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
50
51
52
#include<iostream.h>
#include<string.h>

class circle{
private:
int r,ar,c;
public:
circle (int r1){r=r1;}
int area(){
ar=(r*r*3.14);
return ar;
}
int cir(){
c=(2*3.14*r) ;
return c;
}
void display(){
cout<<"\nFirst circle:\n";
cout<<"area = "<<area();
cout<<"\ncir = "<<cir();
cout<<"\n\nSecond circle:\n";
cout<<"area = "<<area();
cout<<"\ncir = "<<cir();
}
friend void compare(circle a, circle b);
};

void compare(circle a, circle b){
if (a.r > b.r) {
cout<<"\nThe first circle is bigger";
}
else if (a.r<b.r){
cout<<"\nThe second circle is bigger";
}
else cout<<"\nThey are both the same size";

}

void main(){
int r1,r2;
cout<<"Enter the radius of the first circle:\n";
cin>>r1;
cout<<"\nEnter the radius of the second circle:\n";
cin>>r2;
circle c1(r1);
circle c2(r2);
circle *c3;
c3=&c1,c2;
c3->display();

compare(c1,c2);
 }
First, use doubles not ints. There is a problem with functions returning int, but the calculation makes them double implicitly.

Read this for how to deal with doubles:

http://www.cplusplus.com/forum/beginner/99565/#msg535515


Line 39 is always int main() { if your compiler allows this - it means it is really old like Borland Turbo C++ - get a new one if you can - there are plenty of free ones.

You can avoid using the friend function by providing a public interface to your class. That is, have a public function which returns the value of r.

Hope all goes well.
Change line 6 to unsigned r; or double r; if you'd like to have a non-integer radius.

Change area to:
1
2
3
4
double area() const 
{
    return r*r * 3.14 ;
}


Change cir to:
1
2
3
4
double cir() const
{
    return r*2 * 3.14 ;
}


Most important, change display to:
1
2
3
void display(){
    cout << "Area: " << area() << "\nCircumference: " << cir() << '\n' ;
}


Previously you were just displaying one circle, twice.

Now change main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
    int r1,r2;
    cout<<"Enter the radius of the first circle:\n";
    cin>>r1;
    cout<<"\nEnter the radius of the second circle:\n";
    cin>>r2;
    circle c1(r1);
    circle c2(r2);
    circle *c3;
    c3=&c1,c2;     // this is equivalent to c3 = &c1;  c2 ;  So c3 points to c1 and only c1.
    c3->display(); // so this will display c1, and only c1.

    cout << "First circle:\n" ;
    c1.display() ;

    cout << "Second circle:\n";
    c2.display() ;

    compare(c1,c2);
 }



@theideasman ,
this question was given to me in my exam so when i asked the instructor why did she ask us to put int instead of double or unsigned she said it was easier for her when grading the exam ,so i just gave in !
and yes i do have turbo borland c++ i did not know there were newer ones since we use the sam one in our lab
also the friend function was a given in the question
lastly thank you very much
@cire thank you know i know where i went wrong !
its always the stupidest things with me :( ,at least i know i wont make the same mistake again . thank you again !
Topic archived. No new replies allowed.