- #1
mattskie
- 10
- 0
Let me start by saying I know this is a funky way to program, but my teacher is requiring us to go about it this way.
also:
I CANT use std::string, classes, constructors for this project.
I am required to use this archaic method of c-style strings with dynamic memory allocation occurring outside the struct.. i know its not the best way to go about this, but there's nothign i can go. :(
I have a struct
struct card
{
char *suit;
char *rank;
int cvalue;
}
I've created a pointer of size 52 for my deck
card *deckPtr = new card[52];
card *deckHome = &deckPtr[0];
I then try to use
for(int i=0;i<52;i++)
{
(*deckPtr).suit = new char[8];
(*deckPtr).rank = new char[7];
deckPtr++
}
deckPtr=deckHome;
I am essentially trying to fill in these arrays from a card file, but I cannot make it past running the program, i get sa seg fault which I don't understand why.
I dynamically allocate memory in my card read in function..
void cardInit(card *deckPtr)
{
card *deckHome = &deckPointer[0];
ifstream fin;
char *finName = new char[13];
cin >> *finName
fin.open(finName)
.
.
.
while(fin.good())
{
for(deckPointer=&deckHome[0];deckPointer<&deckHome[52];deckPointer++)
{
fin >> (*deckPointer).suit;
fin >> (*deckPointer).rank;
fin >> (*deckPointer).cvalue;
}
}
Its a pretty simple program..and my dynamic memory works for the file name, but I can't figure out why it doesn't work for structs?
also:
I CANT use std::string, classes, constructors for this project.
I am required to use this archaic method of c-style strings with dynamic memory allocation occurring outside the struct.. i know its not the best way to go about this, but there's nothign i can go. :(
I have a struct
struct card
{
char *suit;
char *rank;
int cvalue;
}
I've created a pointer of size 52 for my deck
card *deckPtr = new card[52];
card *deckHome = &deckPtr[0];
I then try to use
for(int i=0;i<52;i++)
{
(*deckPtr).suit = new char[8];
(*deckPtr).rank = new char[7];
deckPtr++
}
deckPtr=deckHome;
I am essentially trying to fill in these arrays from a card file, but I cannot make it past running the program, i get sa seg fault which I don't understand why.
I dynamically allocate memory in my card read in function..
void cardInit(card *deckPtr)
{
card *deckHome = &deckPointer[0];
ifstream fin;
char *finName = new char[13];
cin >> *finName
fin.open(finName)
.
.
.
while(fin.good())
{
for(deckPointer=&deckHome[0];deckPointer<&deckHome[52];deckPointer++)
{
fin >> (*deckPointer).suit;
fin >> (*deckPointer).rank;
fin >> (*deckPointer).cvalue;
}
}
Its a pretty simple program..and my dynamic memory works for the file name, but I can't figure out why it doesn't work for structs?