Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fill vector of objects with stuff 2

Status
Not open for further replies.

marcon

Programmer
Aug 30, 2001
21
US
This is part of my school work but the class is over and now I am just posessed with making it work.

I have a .dat file that looks like this :
1 D d1.bmp
except there are 52 of these groups of three.

I also have a vector of objects where I want to take each group of three data and stick them into 52 objects;

void ClassCardDeckArray ::AddCard(int rank, string suit, string bmpfile)
{



v[NoOfCards] = new CardDeck(rank, suit, bmpfile);
NoOfCards ++;

}

Now, when I open the file I fill only one object instead of filling all 52. Below is snippet where I open the file..


//snippet
fin.open("cards.dat");


if(!fin.is_open())
{
cerr << &quot;error cannot open file&quot; << endl;
exit(1);
}

fin >> rank;
fin >> suit;
fin >> bmp;



ClassCardDeckArray a(52);


a.AddCard(rank, suit, bmp);




Any general suggestions on how to loop thru the file and fill these objects? All the examples I seem to find grab file contents all at once.
I am thinking that maybe I should reference the fin (file input) in the class, something like while(fin.isopen????)...although I can see myself getting lost on that.
Any help at all greatly appreciated!
 
fstream fin(&quot;cards.dat&quot;,ios::in);

if(!fin){
cerr...
exit(1);
}

ClassCardDeckArray a(52);

while(fin>>rank>>suit>>bmp){
a.AddCard(rank,suit,bmp);
}


// there you go, this should iterate through the entire file
// and populate your vector
// good luck MYenigmaSELF
myenigmaself@yahoo.com
 
Using your code pretty much as is, and to get your thought process going, here's a hybred of pseudocode and c/c++.
In addition, think this: what happens if the last record contains only one or two of the three elements (perhaps a malformed record)?


while (not end of file)
{
fin >> rank;
fin >> suit;
fin >> bmp;

a[index to access more than the first element].AddCard(rank, suit, bmp);
}
 
In a vector the data structure handles the indexing, not the user. In the def of a vector there is actually no definite position and therefore no definate position, thus making &quot; a[index to access more than the first element].&quot; unecessary. And if the last element doesn't have all of the necessary input fin's failbit is set and we break out of the while loop. Same holds true for when we run into eof, only in this case it will be the eof bit being set. Just trying to keep things as simple as possible. MYenigmaSELF
myenigmaself@yahoo.com
 
oops, &quot;definite position and therefore no definate position&quot;->&quot;definite position and therefore no definate index&quot;

p.s. Correct me if I'm wrong on this. I'm going on the definition of a vector in Java which may be different than the formal definition in C. MYenigmaSELF
myenigmaself@yahoo.com
 
Sorry, I didn't read carefully enough.

If you are using the C++ templates, (i.e. <vector>), then myenigmaself's point is absolutely true.

If you are just using the term, &quot;vector&quot; to refer to a standard array (in this case, an array of classes), then my example code should be okay. Unfortunately, I haven't gotten much experience with templates (yet).

I've picked up several books on templates that I intend to curl up with when I get some time. [hah!]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top