Accessing an Array of Struct type in Function

This is a discussion on Accessing an Array of Struct type in Function within the C++ Programming forums, part of the General Programming Boards category; Looking for some help from the community. I can't seem to find the exact answer to my question after browsing ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Question Accessing an Array of Struct type in Function

    Looking for some help from the community. I can't seem to find the exact answer to my question after browsing around.

    I am in the beginning stages of a program and after declaring a struct (cardStruct) then declaring an array of struct type (cardStruct deck[MAX_CARDS]), I am trying to access the array through my function FormCards.

    Here's my problem, hopefully you guys can point me in the right direction.

    Code:
    cards.cpp:39: error: variable or field 'FormCards' declared void
    cards.cpp:39: error: 'cardStruct' was not declared in this scope
    Here's my code:
    Code:
    #include <iostream>#include <cstdlib>
    #include <ctime>
    
    
    
    
    using namespace std;
    
    
    
    
    //Global Constants
    const int MAX_CARDS = 52; //Represents the size of the deck of cards
    const int MAX_SUIT = 13;
    
    
    //Function Declarations
    void FormCards ();
    
    
    int main ()
    {
    
    
        enum SuitType {CLUBS, HEARTS, SPADES, DIAMONDS}; //Enum type to represent each card's suit
        
        struct cardStruct
        {
            SuitType suit;
            int value;
            int point;
            
        };
        cardStruct deck[MAX_CARDS];
    
    
    }
    
    
    
    
    void FormCards (cardStruct deck[])
    {
        int i, k; //loop index
        int index = 0; //Used for assigning points and values in nested for loop
    
    
        //For loop assigns suit
        //13 cards per suit
        for (i = 0; i < MAX_CARDS; i++)
        {
            if (i < 13)
                deck[i].suit = DIAMOND;
            else if (i < 26)
                deck[i].suit = CLUB;
            else if ( i < 39)
                deck[i].suit = HEART;
            else
                deck[i].suit = SPADE;
        }
    
    
        //Nested for loop assigns values and points
        for (i = 0; i < MAX_SUIT; i++)
        {
            //assign values
            deck[index].value = k + 1;
    
    
            //assign points
            //HEART card sless than 10 has a point of 5
            //HEART cards of J, Q, K have points of 10
            if (deck[index].suit == HEART)
            {
                if (deck[index].value >= 10)
                    deck[index].points = 10;
                else
                    deck[index].points = 5;
    
    
            }
            //Queen of SPADE has a point of 100
                else if (deck[index].suit == SPADE && deck[index].value == 12)
                    deck[index].points = -100;
            //Rest of the cards will have 0 points
                else
                    deck[index].points = 0;
            index++; //Increase index by 1
        }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Try writing the code this way

    Code:
    enum SuitType {CLUBS, HEARTS, SPADES, DIAMONDS}; //Enum type to represent each card's suit
    
    struct cardStruct
        {
            SuitType suit;
            int value;
            int point;
             
        };
        
    
    int main ()
    {
         
        cardStruct deck[MAX_CARDS]; 
     
        return 0; /* You had forgot that, or you would write that later */
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thanks, I had a feeling it was the placement of the struct and enum. However, now my SuitType is not being recognized in the FormCards function. Is this a matter of it not being passed into the struct/array properly?

    Code:
    code.cpp: In function 'void FormCards(cardStruct*)':
    code.cpp:61: error: 'DIAMOND' was not declared in this scope
    code.cpp:63: error: 'CLUB' was not declared in this scope
    code.cpp:65: error: 'HEART' was not declared in this scope
    code.cpp:67: error: 'SPADE' was not declared in this scope
    code.cpp:79: error: 'HEART' was not declared in this scope
    code.cpp:88: error: 'SPADE' was not declared in this scope

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    No, it's a matter of you using different names.

    Given an enum value named CLUBS, the compiler cannot make sense of something named CLUB.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Aye...how embarrassing. Well that obviously fixed that. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-10-2012, 12:07 PM
  2. struct within a struct, odd behavior accessing variables
    By John Gaden in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2012, 05:19 AM
  3. accessing struct element from another struct
    By creek23 in forum C Programming
    Replies: 10
    Last Post: 06-24-2010, 02:56 AM
  4. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  5. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21