PDA

Click to See Complete Forum and Search --> : C++ Data Type


Epison07
October 26th, 2004, 10:08 PM
Ok, I remember from last year that you can create a data type of whatever you want and use that in a struct. I have looked through my C++ book, and I am kinda lost. I am not really sure how to ask you want I want to know...so I will just give an example, I guess.
Say I am creating a program for a company, and they want me to create an array of all of the employees. And under each employee they want some information, like name, gender, age, pay and so on. What I am trying to do is create some kinda datatype that will have a char, int, float and whatever else I want in it. I remember doing this, And I thought it was something like:

datatype thisdata;
struct thisdata{
int age;
char gender;
}

That just looks wrong. I am pretty sure I know how to use struct correctly, but how do I make an array struct. Is this just so easy I am confusing myself, or should I just try this with classes? Help :-?

-Ep

SwordFish_13
October 26th, 2004, 10:20 PM
Hi,

Both Structure and class will do ...............but if you are using C++ why not use the class ............i would suggest use classes make .........a class employee.

making an array of type strucute is easy ...........just like any other array
e.g

struct employ
{

char name[15];
char gender;
int age;
double salary;
};

void main()
{
employ e1[10]; // created a array of type employ

}


there would be many advantages of using a class instead of Structure here...........bettr grouping of data and the functuns that will operate on this data .



class employ
{
private:
char name[15];
char gender;
int age;
double salary;

public:
add_emp_detail();
delete_employ() ; //example functuns
};

void main()
{
employ emp1[10];

}


now that data e.g name , gender etc can only be accasible through these functions ..........you cannot access the data directly outside the class (unless using a friend class)..........gives better protection to the data and much more readible.

I hope this is what you looking for ........sorry if i misunderstood the question .............because yes the question is confusing ...................if this is not what you are looking for please refraze the question .

--Good Luck--- :smokes:

Epison07
October 26th, 2004, 10:37 PM
Ahhh, ok. This is what I am looking for. While replying to your post, I was trying to make another example, and I found out exactly what I am looking for. I remember it had data types it is, where you declared your own.
Ok, I am looking for an array in an array, but not a vector. Now, bare with me, cuz I forgot the syntax, and Im not even sure if this is the right way to do this. But something like:

datatype this_data //is datatype a keyword?
{
int this_int;
string this_string;
}

struct bob
{
int that_int;
datatype this_data;
};

or maybe it was two structs, one in another. I am not just looking for a struct array, but an struct array in another struct array. I dont think I did it with two structs. If I remember correcly it was something like what I have above.
Anywayz, thanks for your help. I know I am being confusing.

-Ep

P.S. How do you put your code in the "code" format?

SwordFish_13
October 26th, 2004, 10:44 PM
Hi,

datatype this_data //is datatype a keyword?

No .....................Struct and class are .

hmmmmmmmm i am even more confused but lets see


struct a
{
int x;
char y;
}

struct b
{
struct a sam[10];
int z;
}

void main ()
{

}



something like this............

P.S. How do you put your code in the "code" format?

[ Code ]

--Good Luck--:smokes:

Epison07
October 26th, 2004, 10:48 PM
Alright, Thanks.
I am going to play around with this for a little bit. I will post later and tell you how it all works out. Thanks again.

-Ep

Epison07
October 26th, 2004, 11:19 PM
Alright, I have been playing around and brain storming a little bit. And the way I am seeing it, I might give up on my project. First, let me explain my project. What I am trying to create, is a Lyrics Database in C++. I am trying to think of the best way to go about this. I think that a struct in a struct will be unnessarily complicated. Im not sure how I would do it with classes, because I would want to add a search fuction, where you would just do a linear search for the group. But if I use classes, I could have a variable for the group name, cd name, and then the song names. But how would I tie the lyrics to each song name.


class music
{
string group;
string cd_title;
string song1; //Just the song title
...
}


How would I go about adding the lyrics without adding variables for each songs lyrics in the class also. Could I instead have the group and title in the class, and then have a struct array with the song name and lyrics included in the class also?
Also, I was thinking maybe I would just have a different header file for each cd that I add to my database. But if I did that, I would have a shit load of includes at the top of my main program. Plus when I send my program to friends, I would have to give them all of the header files also, or else it wont work...right. Does it not need the header files if I just send them the compiled verus, and not the code.
Like I said, I have been brainstorming this for a while, and I am getting lost in my thoughts. About to give up if I dont find some sort of answer, or direction.

-Ep

Epison07
October 27th, 2004, 12:08 AM
Ok, I have been messing around with this some more. This time I was trying everything with classes, and I think I have a decent way to do this.


#include<iostream>
#include<string.h>
using namespace std;

class m
{
public:
int index;
string group;
struct cds
{
string title;
struct songs
{
int songNumber;
string songName;
string lyrics;
};
songs song[13];
};

cds cd[2];

};

int main()
{
const int SIZE = 5;
m music[SIZE];
music[0].index = 5;
music[0].group = "Senses Fail";
music[0].cd[0].song[3].songName = "Let It Enfold You";

cout<<music[0].group<<'\n';
cout<<music[0].cd[0].song[3].songName<<'\n';
system("PAUSE");
return 0;
}


One problem with this way is that I have to declare the amout of songs and cds for all groups I use, period. I tried using the variable index to change the amount of songs on any given cd, but it have me a parse error saying that the songs didnt exist. So I am not sure if I can change the number of songs for each cd, or if I will just have to set a high amout so no matter how many songs the cds will have on it, I will have enough slots.
I am still not sure if this is the most effencient way to do this, but yeah. I did it this way so now I can have multipule cds for each group. And within each cd I can have all of the songs and their lyrics. What do you think? Is there a better way to do this, before I actually get started on programming this?

-Ep