Passing a structure to a fxn questions

This is a discussion on Passing a structure to a fxn questions within the C++ Programming forums, part of the General Programming Boards category; I am trying to use a structure to store the card value, the card name, and the suit name for ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    27

    Passing a structure to a fxn questions

    I am trying to use a structure to store the card value, the card name, and the suit name for a deck of 52 cards in a blackjack program.

    The structure is globally defined:
    Code:
    struct cards{
    		int cardval; // value of card
    		string suitname; // name of suit
    		string cardname; // name of card
    	}card[52];
    In the main function, card is passed to the function deckgen:
    Code:
    deckgen(card);//creates deck and names card values
    Deckgen is in a header file:
    Code:
    #ifndef	DECKGEN_H
    #define DECKGEN_H
    
    # include <iostream>
    # include <string>
    using namespace std;
    
    void deckgen(cards [52]);
    void deckgen(cards card[52])
    {
    	// Setting card values
    
    	int num;
    	int cardnum = 1;
    
    	for (int i = 0; i <= 3; i++)
    		{
    			for (int j = 0; j <= 12; j++)
    			{
    	
    				if (j < 9)
    					num = j+2;
    				else if (j <12)
    					num = 10;
    				else num = 11;
    				card[cardnum].cardval = num;
    
    	//Naming suits.
    	
    	switch(i){
    		case 0: 
    			card[cardnum].suitname = "clubs";
    			break;
    		case 1: 
    			card[cardnum].suitname = "diamonds";
    			break;
    		case 2: 
    			card[cardnum].suitname = "hearts";
    			break;
    		case 3: 
    			card[cardnum].suitname = "spades";
    			break;}
    	
    	//Naming Cards.
    
    	switch(cardrand){
    		case 0: 
    			card[cardnum].cardname = "two";
    			break;
    		case 1: 
    			card[cardnum].cardname = "three";
    			break;
    		case 2: 
    			card[cardnum].cardname = "four";
    			break;
    		case 3: 
    			card[cardnum].cardname = "five";
    			break;
    		case 4: 
    			card[cardnum].cardname = "six";
    			break;
    		case 5: 
    			card[cardnum].cardname = "seven";
    			break;
    		case 6: 
    			card[cardnum].cardname = "eight";
    			break;
    		case 7: 
    			card[cardnum].cardname = "nine";
    			break;
    		case 8: 
    			card[cardnum].cardname = "ten";
    			break;
    		case 9: 
    			card[cardnum].cardname = "jack";
    			break;
    		case 10: 
    			card[cardnum].cardname = "queen";
    			break;
    		case 11: 
    			card[cardnum].cardname = "king";
    			break;
    		case 12: 
    			card[cardnum].cardname = "ace";
    			break;}
    
    		cardnum ++;
    
    			}
    		}
    
    
    
    	
    
    }
    #endif
    My first problem is that I am getting the error messages:
    "error C2065: 'cards' : undeclared identifier"
    and
    "error C2182: 'deckgen' : illegal use of type 'void' " for line 8 of deckgen.

    Secondly, I do not think that my code now stores the variables correctly in the structure card[52]. Namely, I do not think these values can be used in other functions.

    How can I correct these problems?

    Thank you,
    J

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Code:
    struct cards{
    		int cardval; // value of card
    		string suitname; // name of suit
    		string cardname; // name of card
    	}card[52];
    Looks like you're trying to create an array of structs - in which case, that's not the way to do it - delete the highlighted piece, and somewhere in your code, have:

    Code:
    cards some_variable_name [array_size];
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Richie T,

    I have followed your suggestion, but am still getting the same error messages.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Try moving the definition of the struct inside your header - also, as a rule, implementation of a function is not normally stored in a header, but in another cpp file.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > The structure is globally defined:
    Where - in your main.cpp file?

    If it's after the deckgen.h header file inclusion (see below for more comment), then it's too late for the header file.

    > #ifndef DECKGEN_H
    Why do you have code in a header file?
    You should have deckgen.h, which contains only the prototype and the struct cards declaration.

    The deckgen.cpp includes deckgen.h and also the function implementation.

    You compile both with something like
    gcc main.cpp deckgen.cpp
    Or include both files in the project in your IDE.

    There's a FAQ on multi-source projects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Moving the definition of the structure to the inside of my header does not change the error messages.

    Also, I do not want to do this because I will be using the structure in other functions once the values are initialized.

    Your comment about where functions should be located has been noted. I will alter that once I solve the existing problems with my program.

    Do have any other suggestions?

    Thank you,
    J

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    The structure is defined outside of the main function.

    I will examine the FAQ on multi-source projects. If I get the program set up so I have .cpp and .h files organized as you suggested, will this take care of my error messages?

    Also, no one has addressed the issue of using the card values, card name, and suit names in the array of structures in different functions. Will I be able to use those variables as initilized in the deck generator in another function?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I think the problems would go away if you declared the struct before the function.
    This flows naturally from structuring the code in the usually preferred way of structuring code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Moving the structure to before the function did make my original errors go away, but now I get error messages for my structure declaration which says that suitname and cardname are missing a type specifier. This is leading to problems in my deck generation, where I get error messages saying that suitname and cardname are not members of card. I do include <string> in both my main .cpp file and in the header files. What could the problem be?

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Did you try:
    Code:
    struct cards
    {
      int cardval; // value of card
      std::string suitname; // name of suit
      std::string cardname; // name of card
    };
    (Or alternatively, a using declaration, although it's a good idea to avoid them, especially in headers)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 05:49 PM
  2. Passing a structure by address
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 03-24-2009, 12:43 AM
  3. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM
  4. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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