Question about Structures

K

kazack

Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on structures. Trying to comprehend this
is harder than I thought it was going to be. I should of just skipped this
chapter and went right into pointers since they seem to be easier to use.
But anyways here i smy question:

you define a structure example:

struct phonerec
{
string fname;
string lname;
string number;
}phone[100];

If the above is right from what I understand phone will hold 100 fname,100
lname and 100 number?

to store data in in phone I would have to do somethinglike this if I
understood it all right.

phone[0].fname = "test value 1";
phone[0].lname = "test value 3";
phone[0].number = "123-456-4323"

Now from what I have read you can not use I/O streams to access structures,
only members of structures. so lets say that all 101 records are filled in
phone. If I wanted to output it all to a file I can do something like this:

outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << phone[x].fname;
outfile <<" ";
outfile << phone[x].lname;
outfile <<" ";
outfile << phone[x].number;
outfile << endl;
}
Is there an easier way to output the data from the structure to the file?

I guess I should of just typed in my code into the compiler to actually see
if this works myself first and if it didn't figue out why it didn't but
ususally when I read something and go over it I get the point it's when I
try to do something I didn't read on that I have problems with it.

Now the next question I have for the more experienced programmers is with
the above data structure, what I have defined above the same as the
following below and if so how is one more efficient than the other:

string fname[100];
string lname[100];
string number[100];

//assume all 101 of each have data in them

outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << fname[x];
outfile <<" ";
outfile << lname[x];
outfile <<" ";
outfile << number[x];
outfile << endl;
}

If all of the above is correct then I have understood the concept of how a
data structure works and is in the simple form, I did not yet read up on
structures within structures yet, this chapter is going to take me a little
longer than any of the other chapters in the book to grasp. I do truly wish
I could just say forget it and forget it even exists. Although structures,
Data Abstraction and classes were created for the purposes of making coding
easier but more complex than other alternatives.

Thank you for all replies to this message,
Shawn Mulligan
 
K

Karl Heinz Buchegger

kazack said:
Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on structures. Trying to comprehend this
is harder than I thought it was going to be.

Actually the idea behind structures is very simple:
Things that belong together should stay together.

Example: A date.
A date always consists of a day, month and year. A day alone, a month
alone a year alone does not specify a date. Only the triplet of all of
them does.

Thus I group them together:

struct Date
{
int Day;
int Month;
int Year;
}

So whenever I talk about a 'Date', I mean the triplet of Day/Month/Year.
And whenever I pass I date around, I pass the whole triplet around.
I should of just skipped this
chapter and went right into pointers since they seem to be easier to use.

Suprises are waiting for for.
In fact, pointers are not much of use, unless you have structures.
But anyways here i smy question:

you define a structure example:

struct phonerec
{
string fname;
string lname;
string number;
}phone[100];

If the above is right from what I understand phone will hold 100 fname,100
lname and 100 number?

In principle you are right. Deep in the internals of memory there are 100
fname, 100 lname and 100 number. But you should change your thinking.

phone consists of 100 phonerecs.
Each phonerec consists of 1 fname, 1 lname and 1 number.
to store data in in phone I would have to do somethinglike this if I
understood it all right.

phone[0].fname = "test value 1";
phone[0].lname = "test value 3";
phone[0].number = "123-456-4323"

Yep. There it is again:

phone all phones (there are 100 of them, so which one?)
phone[0] aha, the phonerec with index 0. But a phonerec consists
of fname, lname and number, So which one?
phone[0].number Aha. The number field from the phonerec with index 0
Now from what I have read you can not use I/O streams to access structures,
only members of structures.

That's not entirely true, but it makes no real difference.
The thing is: A structure is a collection of fields. All of them form
the structure. The stream I/O routines on the other hand are designed
to work with basic data types, like int, double etc.. But a struct
isn't such a basic data type, it consists of them eventually, but it isn't.
so lets say that all 101 records are filled in
phone. If I wanted to output it all to a file I can do something like this:

outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << phone[x].fname;
outfile <<" ";
outfile << phone[x].lname;
outfile <<" ";
outfile << phone[x].number;
outfile << endl;
}
Is there an easier way to output the data from the structure to the file?

No. That's the way you do it. You could write a function for outputing one
record and call that function in the loop. That would be a better organization
most of the time, but basically it's just the same thing: you output each
field individually (Who says that you need to output the whole structure?
What about formatting of the individual fields? What about seperating text?)
I guess I should of just typed in my code into the compiler to actually see
if this works myself first and if it didn't figue out why it didn't but
ususally when I read something and go over it I get the point it's when I
try to do something I didn't read on that I have problems with it.

Reading is just one thing. You actually need to practice programming.
Knowing about if, for, while and all the other keywords is not enough.
Beeing able to combine things is what programming is all about. It is
like plaing chess. Just knowing how the pieces can move is not enough
to make you a chess player.
Now the next question I have for the more experienced programmers is with
the above data structure, what I have defined above the same as the
following below and if so how is one more efficient than the other:

At your level just forget ybout efficiency right now. Learn to walk,
then learn to run, then learn to run fast, then try to win the NY marathon.
Not the other way round.
string fname[100];
string lname[100];
string number[100];


Those are 3 arrays. There is no connection between them.
//assume all 101 of each have data in them

They don't. An array

string fname[100]

has exactly 100 elements. Their valid indices are 0 .. 99
(count them, there are exactly 100 numbers in 0 .. 99)
outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << fname[x];
outfile <<" ";
outfile << lname[x];
outfile <<" ";
outfile << number[x];
outfile << endl;
}

If all of the above is correct then I have understood the concept of how a
data structure works and is in the simple form, I did not yet read up on
structures within structures yet,

That's not a big deal:

// A Date consists of a Day, a Month, a Year
struct Date
{
int Day;
int Month;
int Year;
};

// A Name consists of a FirstName, a LastName
struct Name
{
string FirstName;
string LastName;
};

// a person is described by
// a Name (which contains a firstname and a lastname
// a Birthdate ( which consists of a day, a month, a year
struct Person
{
Name TheName;
Date BirthDate;
};
this chapter is going to take me a little
longer than any of the other chapters in the book to grasp. I do truly wish
I could just say forget it and forget it even exists.

Don't.
For one it's your entry point into a whole new world of dynamic data structures.
Also they make coding easier and simpler.
Also maintenance is easier.
Consider that you want to upgrade your program which uses aboves Person structure.
You want to add an address to the person. Simple just add a structure

struct Address
{
string Town;
string Street;
int Streetnumber;
}

and upgrade the person:

struct Person
{
Name TheName;
Date BirthDate;
Address TheAddress;
};

And throughout the whole program whenever you have a person object,
you also have it's address. If you wouldn't have used structures, you
would have needed to add the address to all the function calls and
parameter lists.
Although structures,
Data Abstraction and classes were created for the purposes of making coding
easier but more complex than other alternatives.

Not really. In the end everything becomes simpler. I don't need to worry any
longer to pass Day/Month/Year to a function. I just pass it a Date and know
that it receives everything needed to descrive a date.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top