Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 20:30

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C++ issue

Discussion on C++ issue within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
C++ issue

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
o0Zero0o1 is offline  
Old 10/27/2012, 01:13   #2
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
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.).
Dr. Coxxy is offline  
Thanks
1 User
Old 10/27/2012, 10:53   #3
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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:
Nightblizard is offline  
Old 10/27/2012, 11:42   #4
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
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:
however, this is C++ and in my opinion bad style to use structs with functions since C++ offers classes for this.
Dr. Coxxy is offline  
Old 10/27/2012, 11:50   #5
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
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.
o0Zero0o1 is offline  
Old 10/27/2012, 13:15   #6


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
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).
MrSm!th is offline  
Old 10/27/2012, 15:05   #7
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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 ****** up.
Nightblizard is offline  
Old 10/28/2012, 00:31   #8
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
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 **** triangles using these structures , otherwise finding S and P is just maths ... this **** is driving me crazy ....
o0Zero0o1 is offline  
Old 10/28/2012, 01:07   #9
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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 **** triangles using these structures , otherwise finding S and P is just maths ... this **** 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.



Play a bit around with it and familiarize yourself with it and then try to use it in your problem.
Nightblizard is offline  
Thanks
1 User
Old 10/28/2012, 10:55   #10
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
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 ...
o0Zero0o1 is offline  
Old 10/28/2012, 14:30   #11
 
elite*gold: 0
Join Date: Jul 2010
Posts: 277
Received Thanks: 47
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);
cssuchti is offline  
Old 10/28/2012, 14:54   #12
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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).
Nightblizard is offline  
Thanks
1 User
Old 10/31/2012, 16:15   #13
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 15
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 ?
o0Zero0o1 is offline  
Old 10/31/2012, 16:30   #14
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
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;
Dr. Coxxy is offline  
Thanks
1 User
Old 10/31/2012, 23:48   #15
 
o0Zero0o1's Avatar
 
elite*gold: 0
Join Date: Feb 2010
Posts: 117
Received Thanks: 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(***,he is real pain in the a$$ -_- )

I'll post my progress as i have any more.
o0Zero0o1 is offline  
Reply


Similar Threads Similar Threads
Little issue
10/13/2011 - Dekaron Private Server - 4 Replies
Ladies and Gentlemen, i have a little and noob Problem. I Downloaded the Aloken server files with the Dekaronserver.exe, Dekaron.exe and the character.bak (ItsNobody database). I placed the dekaronserver.exe in the appropriate folder inside the virtual machine. I placed the dekaron.exe in the appropriate folder inside the A9 client. What i should do with the character.bak? i know it is something to do into databese, but i really dont know how i should start. Can someone tell me...
Issue with DB-Bot
07/14/2010 - SRO Private Server - 3 Replies
Hello, when I add a new skill in the bot or when I update a skill it always says: the selected item cannot be used together or the skill is already in use. But the old skill is gone in the game and in the bot but still it says that.. Really frustrating I play on ZSZC sro...
AC/Bot issue
07/15/2008 - MapleStory - 0 Replies
i have tryed many ac/bot that woked for many ppl but it doesn't for me. I download the ac/bot annd folo the intruction completly. i start up the ac/bot and then run my private server. in the server i click the hotkey but nothing happenes. it says the ac is enable but nothing happens. help me plz i'm really getting frustrated.
SV Issue
11/18/2007 - Conquer Online 2 - 7 Replies
Basically after i press start sv just does nothing apart from hop around a bit now and again. On co there is a msg 'can't jump that far' dont know if that has anything to do with it. sv was working fine before, then it did this and when i tried the next day it worked for some reason, now it wont work again. fyi im using sv 1.17 with ce on patch 4356, help very much appreciated as i have tried everything including fresh co install.



All times are GMT +1. The time now is 20:30.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.