C++ issue

10/27/2012 00:05 o0Zero0o1#1
I've been reading quite alot but i still cannot understand the use of structures.

I'm given the following exam:
You have to find the frontage and the perimeter of triangle ,that is composed in coordinate plane 0xy,by inputting edge ( the edge is C for example of A,B,C).

I have to be using structure for POINT and TRIANGLE.This means that the edge of the triangle is not regular variable but member-variable of structure.I'm obligated to define structure for point with member-variable xc and yc - coordinates of point.

I'm still struggling to understand what am i obligated to actually do.The part with the structures is really confusing me ... anyone can clarify it to me or show it with an example so i can try something and post my code here for checkup ..

By now i know only how to input the libraries and calculate the frontage and the perimeter ( the noobie stuff).

Thanks in advance
10/27/2012 01:13 Dr. Coxxy#2
Code:
struct Point
{
	float x;
	float y;
};

struct Triangle
{
	Point A;
	Point B;
	Point C;
};


// in code:

Point SomePoint = {10.0f, 10.0f};
// this is the same as writing:
// Point SomePoint;
// SomePoint.x = 10.0f;
// SomePoint.y = 10.0f;
Point SomeOtherPoint = {10.0f, 20.0f};
Point SomeThirdPoint = {20.0f, 10.0f};

Triangle SomeTriangle = {SomePoint, SomeOtherPoint, SomeThirdPoint};

printf("SomeTriangle.A == (%f/%f)\n", SomeTriangle.A.x, SomeTriangle.A.y);
a structure is like a new type which can hold multiple variables.

EDIT:
however - if you want to use C++ (and not C), you may prefer classes (which for example allows you to declare functions bound to the structure, so you may create Point.Length() function etc.).
10/27/2012 10:53 Nightblizard#3
Quote:
Originally Posted by Dr. Coxxy View Post
EDIT:
however - if you want to use C++ (and not C), you may prefer classes (which for example allows you to declare functions bound to the structure, so you may create Point.Length() function etc.).
The only difference between classes and structs in C++ is, that in structs everything is public by default and private in classes. Otherwise they behave the same.
See: [Only registered and activated users can see links. Click Here To Register...]
10/27/2012 11:42 Dr. Coxxy#4
Quote:
Originally Posted by Nightblizard View Post
The only difference between classes and structs in C++ is, that in structs everything is public by default and private in classes. Otherwise they behave the same.
See: [Only registered and activated users can see links. Click Here To Register...]
however, this is C++ and in my opinion bad style to use structs with functions since C++ offers classes for this.
10/27/2012 11:50 o0Zero0o1#5
I'm obligated to use structure in C++ ,using classes would make it more complex as we didn't pass the material.

So when i create the structures i'm not sure how to define side A/B/C using the structures .. well here is what my brain created ( note i'm really a beginner in C) :
Code:
#include <isotream.h>
#include <math.h>
void main()
{
struct Point
float x;
float y;
};
struct Triangle
{
point A;
point B;
point C;
};
while ((x>0) & (y>0));
cout <<"\n Please input value for x";
cin >> x;
cout <<"\n Please input value for y";
cin >>y;
Side A = x;
Side B = y;
Side C = sqrt((x*x)+(y*y));
cout << "\n The hypotenuse is:";
P = point A + point B + point C;
S = sqrt(P*(P - side A)*(P - side B)*(P - side C));
cout<<"\n S=";
cout<<"\n P=";
// end main

All in all , the program should build triangle by given edge ( C) then calculate it's S,P. Apart from that it has to record the output values in text file and read the file before every new executing(which i'm still digging on the net how-to).Still no clue how to combine structures so that on the display i have to enter edge of the triangle to get it build ...
Second day of learning C++ - burning brain.
10/27/2012 13:15 MrSm!th#6
Quote:
however, this is C++ and in my opinion bad style to use structs with functions since C++ offers classes for this.
That's not true, because there are still situations in which structs fit better and sometimes you want to have structs with more functionality (i.e. constructor or operators).
10/27/2012 15:05 Nightblizard#7
Quote:
Originally Posted by Dr. Coxxy View Post
however, this is C++ and in my opinion bad style to use structs with functions since C++ offers classes for this.
Well, too bad an opinion is not an actual argument.

Quote:
I'm obligated to use structure in C++ ,using classes would make it more complex as we didn't pass the material.
As I've said, there is no difference (beside everything is public by default in structs) between them.

Also, you misinterpreted Dr. Coxxys Code (not entirely your fault though). You have to put structs outside of your main (or for that matter any) function.
Oh, and you might want to put your code inside [ code ] tags instead of [ quote ]. Then your formatting doesn't get fucked up.
10/28/2012 00:31 o0Zero0o1#8
So the structure must be above or after main .... and how to use the variables from the structure ... i mean how to us them in some function outside the structure ...

Because i have to calculate the S and P for N triangles ( n=unknown number) so i guess imma use cycle program with a pointer i ( so i++ on every cycle).

However i still do not understand how to build the damn triangles using these structures , otherwise finding S and P is just maths ... this crap is driving me crazy ....
10/28/2012 01:07 Nightblizard#9
Quote:
Originally Posted by o0Zero0o1 View Post
So the structure must be above or after main .... and how to use the variables from the structure ... i mean how to us them in some function outside the structure ...

Because i have to calculate the S and P for N triangles ( n=unknown number) so i guess imma use cycle program with a pointer i ( so i++ on every cycle).

However i still do not understand how to build the damn triangles using these structures , otherwise finding S and P is just maths ... this crap is driving me crazy ....
By declaring a struct you're making a new type like int or double. So you can make a variable of that type like with any other type. You access its underlying variables with the "." operator.

[Only registered and activated users can see links. Click Here To Register...]

Play a bit around with it and familiarize yourself with it and then try to use it in your problem.
10/28/2012 10:55 o0Zero0o1#10
Code:
#include <iostream>
#include <math.h>
using namespace std;
struct Coordinate
{
float x;
float y;
};
struct Triangle
{
float SideA;
float SideB;
float SideC;
};
int main()
{
int i=0;
int N;
cout<<"\nInput x";
cin>>Coordinate.x;
cout<<"\nInput y";
cin>>Coordinate.y;
Triangle.SideA = Coordinate.x;
Triangle.SideB = Coordinate.y;
Triangle.SideC = sqrt((Coordinate.x*Coordinate.x)+(Coordinate.y*Coordinate.y));
cout<<"\nTriangle.SideC=";
P= Triangle.SideA+Triangle.SideB+Triangle.SideC;
cout<<"\nP=";
S = sqrt(P-(P-A)*(P-B)*(P-C));
cout<<"\nS=";
 for(int i=0; i<N; i++)
}
// end main
And i'm getting full of errors by the compiler ... says most of my variables are undeclared -_- .@nightblizard thanks btw i understood how to combine and use the structures ... but what is wrong in my code ... why the compiler is returning with errors regarding undeclared A B C x y and so on ... also it says something is wrong with my library math ...
10/28/2012 14:30 cssuchti#11
you have to declare the Variables before somehow like this:

Code:
struct _Coordinate
{
     float x;
     float y;
};
_Coordinate Coordinate = {0.0f, 0.0f};
now you could simple access the variables I declared above:

Code:
Coordinate.x = 10.00f;
printf("Coordinate.x: %4.2f\n", Coordinate.x);
10/28/2012 14:54 Nightblizard#12
Yeah, you have to declare a variable of type Triangle and Coordinate before you can use them. Look at line 11 in my example.
I declare a variable "foo" which type is "Foo". After that I have access to foo.bar and foo.meow (you access them via the variables name, not by the structs name).
10/31/2012 16:15 o0Zero0o1#13
Quote:
Originally Posted by cssuchti View Post
you have to declare the Variables before somehow like this:

Code:
struct _Coordinate
{
     float x;
     float y;
};
_Coordinate Coordinate = {0.0f, 0.0f};
now you could simple access the variables I declared above:

Code:
Coordinate.x = 10.00f;
printf("Coordinate.x: %4.2f\n", Coordinate.x);
Code:
_Coordinate Coordinate = {0.0f, 0.0f};
What is that suppoused to mean = {0.0f, 0.0f};
Is this some sort of value or ?
10/31/2012 16:30 Dr. Coxxy#14
a _Coordinate consists of 2 floats, x & y.
so Coordinate = {<x>, <y>};
you can acces them via:
Coordinate.x
Coordinate.y

_Coordinate Coordinate = {0.0f, 0.0f};
this is the same as
_Coordinate Coordinate;
Coordinate.x = 0.0f;
Coordinate.y = 0.0f;
10/31/2012 23:48 o0Zero0o1#15
Thanks alot guys i finally got it ... but it seems the task is going to be more complex ... i'm still getting it clear with the teacher(god,he is real pain in the a$$ -_- )

I'll post my progress as i have any more.