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
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