Sup. We're learning about structs/classes now, and since I missed the lecture I'm seriously confused, tried reading up on them from the internet and I think I have a basic understanding of what they are, just confused about the syntax.
The problem at hand is to write a program that takes some numbers and then does some stuff with those numbers. Anyway, here is a simplified version of what I've written so far, if anyone could help me figure out what I'm doing wrong it'd be a great help :))
#include<iostream>
usingnamespace std;
struct Point {
int x;
int y;
};
struct Circle {
Point centre;
int radius;
};
void print_circle(struct Circle)
{
cout<<"Circle #1"<<endl;
cout<<"The Centre is: ("<<Circle.centre.x<<", "<<Circle.centre.y<<")"<<endl;
cout<<"The Radius is: "<<Circle.radius<<endl;
}
int main()
{
Circle c1;
cout<<"Please enter the centre and radius of circle #1: ";
cin>>c1.centre.x>>c1.centre.y>>c1.radius;
print_circle(c1);
return 0;
}
void print_circle(Circle circle) // circle is just an arbitrary name, Circle is the important part
or better:
void print_circle(const Circle& circle)
Don't know if you had references yet, so the second version may or may not make sense to you. If it doesn't, the first one also works.
If you don't understand why, just ask again. (basically, it's the same thing as any other function parameter, just now with your own types instead of with the predefined ones).
Hey man, thanks that was a great help. I've read about references but I'm not exactly sure how to use them. Here's the rest of the code, I think I need to use references again but I'm unsure as to how:
#include<iostream>
#include<cmath>
usingnamespace std;
double distance;
struct Point {
int x;
int y;
};
struct Circle {
Point centre;
int radius;
};
void print_circle(const Circle& circle)
{
cout<<"Circle #1"<<endl;
cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
cout<<"The Radius is: "<<circle.radius<<endl;
}
double& circle_distance(const Circle& circle1,const Circle& circle2)
{
double a, b, c;
a=abs(circle1.centre.y-circle1.centre.x);
b=abs(circle2.centre.y-circle2.centre.x);
c=pow(a,2)+pow(b,2);
distance=sqrt(c);
return distance;
}
int circle_test(const Circle& circle1, const Circle& circle2)
{
int x;
x=abs(circle2.radius-circle1.radius);
if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
return 0;
if (distance<x)
return 1;
if (distance>circle2.radius+circle1.radius)
return 2;
elsereturn 3;
}
int main()
{
Circle c1;
Circle c2;
cout<<"Please enter the centre and radius of circle #1: ";
cin>>c1.centre.x>>c1.centre.y>>c1.radius;
cout<<endl;
print_circle(c1);
cout<<"Please enter the centre and radius of circle #2: ";
cin>>c2.centre.x>>c2.centre.y>>c2.radius;
return 0;
}
The error is that in double& circle_distance(const Circle& circle1,const Circle& circle2) "distance" is undeclared. I don't know how to make it a global variable so I can use it in other functions. Also, in int circle_test(const Circle& circle1, const Circle& circle2) what would I have to write in the main code to get it to display different things if 0,1,2 or 3 are returned?