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 occuring outside the struct.. i know its not the best way to go about this, but theres nothign i can go. :(

Im stuck with the structs, I cant figure out whats wrong..

I have a struct

#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

//global constant(s)
const int maxCards = 52;

//Structs
struct card
{
    char *suit;
    char *rank;
    int cvalue;
    char location;
};

//Function List
void readPlayers(player *peoplePointer);
void shuffleCards(card *unshuffled, card* shuffled);

//program
int main()
{
    //create pointer and set initial value
    card * deckPointer = new card[52];
    card *deckHome = &deckPointer[0];
    for(int i=0;i<maxCards;i++)
    {
        (*deckPointer).suit=new char[8];
        (*deckPointer).rank = new char[7];
        deckPointer++;
    }
    deckPointer = deckHome;
    cardInit(deckPointer);
    readDeck(deckPointer);

    //sets default values for the card arrays
    for(int i=0;i<52;i++)
    {
        strcopy((*deckPointer).suit,"suit");
        strcopy((*deckPointer).rank,"rank");
        (*deckPointer).cvalue = 0;
        deckPointer++;
    }
    deckPointer = deckHome;
    return 0;
}

//Functions
void cardInit(card *deckPointer)
{
    card * deckHome = NULL;
    deckHome = &deckPointer[0];
    //set up card file to be read in
    ifstream fin;
    char *finName = new char[13];

    //get file name from user
    cout << "Enter file name...(cardFile.txt)" << endl;;
    cin >> *finName;

    //open the file
    fin.open(finName);

    //check if cardFile.txt opens correctly
    if(!fin.good())
    {
        cout << "Error with card file" << endl;
    }
    else
    {
        deckPointer = deckHome;
        while(fin.good())
        {
            for(int i=0;i<50;i++)
            {
                fin >> (*deckPointer).suit;
                fin >> (*deckPointer).rank;
                fin >> (*deckPointer).cvalue;
                deckPointer++;
            }
        }
    }
    delete [] finName;
}

    //Its a pretty simple program..and my dynamic memory works for 
    //the file name, but I cant figure out why it doesnt work for structs?
mpromonet
  • 11,326
  • 43
  • 62
  • 91
matt
  • 1
  • 5
  • please post a complete but reduced to minimal example. real code. and explain the problem. – Cheers and hth. - Alf Sep 24 '14 at 18:49
  • 1
    You shouldn't dynamically allocate things that you don't need to dynamically allocate. For example `(*deckPtr).suit = new char[8]` and `(*deckPtr).rank = new char[7]`. You're indentation also makes it extremely difficult to read your code. – Jason Sep 24 '14 at 18:53
  • Use variable names that tell if you have a pointer to a deck or a pointer to a card. I think you're mixing them up. I would have one deck pointer, and then use card pointers like iterators. – Kenny Ostrom Sep 24 '14 at 18:59
  • You are trusting the input file too much. I'd read the card into temporary storage, then assign it to the deck, rather than doing both at the same time. Simplify each function to one task and you can test the function by itself. – Kenny Ostrom Sep 24 '14 at 19:04
  • One question: Why are you dereferencing finName? `cin >> *finName` – dari Sep 24 '14 at 19:06
  • i updated the code, i derefernce the value so the user can enter the file name – matt Sep 24 '14 at 19:12
  • @matt That will only input one character. Use `cin >> finName` to read a whole string, like you do with the suit and the rank. – molbdnilo Sep 24 '14 at 19:33
  • There's nothing wrong with those "archaic" C-strings. Especially on modern, GNU-based systems you can easily do a trick or two with those C-strings that is neigh impossible to achieve with `std::string`. I really prefer the C-way in this area, even when programming in C++. – cmaster - reinstate monica Sep 24 '14 at 19:34
  • There is no function named `strcopy`. perhaps `strcpy`?? – David C. Rankin Sep 24 '14 at 20:45

1 Answers1

0

For your code to work, you will need a typename card.

to this end you will need to setup the following:

struct cardstruct { 
    char *suit; 
    char *rank; 
    int cvalue; 
};

typedef struct cardstruct card;

otherwise, when you declare the pointers, you would need to use the "struct" keword first. E.G.:

struct card *deckPtr = new struct card[52];
  • 1
    This is not necessary in c++. http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – dari Sep 24 '14 at 19:00