Thread: passing value into an array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    passing value into an array

    I am writing a program for a poker game.. I created a class to get cards and create deck.. the problem I am having now is how to deal 5 cards to an array to be evaluated later. I want to call the array player1[5]. So I tried to create a pointer called GetCard, and was hoping to then pass the value into the player1[] array. I tried to use pointer but I get the following error

    49 31[Error] void value not ignored as it ought to be


    Code:
     #include <stdlib.h>
    #include <iostream>
    #include <ctime>
    #include <algorithm>
    using namespace std;
    
    const char* FaceString[13] = {"Ace", "2", "3", "4",
                                  "5", "6", "7", "8",
                                  "9", "10", "Jack", "Queen", "King"};
    
    const char* SuitString[4] = {"Spades", "Hearts", "Diamonds", "Clubs"};
    string player1[5];
    
    class Card
    {
    public:
        int face;
        int suit;
    
        void toString() {
            cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
        }
    
    };
    
    int main()
    {    char *getCard;
    	getCard = new char;
    	
        // Create a deck of cards
        Card deck[52];
    
        for (int i = 0; i < 52; i++) {    
            deck[i].face = i % 13;
            deck[i].suit = i % 4;    
        }
    
        // Shuffle
        srand (unsigned(time(0)));
        random_shuffle(&deck[0], (&deck[0]+52));   
    
        // Display cards
        for (int j = 0; j < 52; j++) {  
            cout<< "Card_" << (j+1) << ": ";
            deck[j].toString();
        }
    
    	for (int j = 0; j < 5; j++) {  
         getCard=deck[j].toString();//<<====Problem here trying to pass value to a pointer. 
             }
    
    
    
        return 0;
    }
    Last edited by Darius Dempsey; 04-16-2014 at 07:17 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There's several things wrong:
    1)toString should presumably return the card information as a string, not print it to cout. This is the main cause of your error.
    2)I have no clue what the getCard variable is supposed to do, but whatever it is, it's not what it's doing. Likely you want an array of cards that represents each hand, not a char *.
    3)You initialize the cards in a weird order. This is not strictly an error.
    4)You call new but never delete. Here you shouldn't be calling new at all, it doesn't make sense to initialize getCard like that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by King Mir View Post
    There's several things wrong:
    1)toString should presumably return the card information as a string, not print it to cout. This is the main cause of your error.
    2)I have no clue what the getCard variable is supposed to do, but whatever it is, it's not what it's doing. Likely you want an array of cards that represents each hand, not a char *.
    3)You initialize the cards in a weird order. This is not strictly an error.
    4)You call new but never delete. Here you shouldn't be calling new at all, it doesn't make sense to initialize getCard like that.
    well I did try it this way..where I tried to pass it to an array..
    Code:
    char player1[5];
    
    for (int j = 0; j < 5; j++) {  
            cout<< "Card_" << (j+1) << ": ";
            player1[j].toString();
           }
    but i get the following error
    [Error] request for member 'toString' in 'player1[i]', which is of non-class type 'char'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    player1 is an array of 5 char, therefore player[1] is a char, and a char does not have a toString member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Thanks @laserlight... Well I tried the array as a string too string player1[5], but It still didn't work.. So what should I use in order to pass the value to an array?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Darius Dempsey
    Well I tried the array as a string too string player1[5], but It still didn't work..
    You can't just try giving the array a type, cross your fingers, and hope that it works. You need to understand what you are doing.

    Why did you name this array player1? What is this array for? What does this array have to do with the Card class? Why are you trying to call toString on an element of this array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You can't just try giving the array a type, cross your fingers, and hope that it works. You need to understand what you are doing.

    Why did you name this array player1? What is this array for? What does this array have to do with the Card class? Why are you trying to call toString on an element of this array?

    I named the array Player1 because it is where I will store the 5 cards for player1, there will be a player2 as well. If I am doing it wrong..can you tell how I should be approaching this. I already tried researching this on google.."how to pass value from an object to an array?" if my concept is wrong I have no idea..that is why I am on this blog..I always try researching first..this is always my last resort..So If you can steer me int he right direction by giving me hints..links to read.. etc.. Your question above dont help me because I honestly dont understand why i cant pass it to the array..and Like i said i already did research it first..I still cant figure it out.. So can you tell me why as oppose to just questioning me..Thank you.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Darius Dempsey
    I named the array Player1 because it is where I will store the 5 cards for player1
    Well, that's great. The array is an array that "will store the 5 cards for player1". So why are you trying to store chars or strings instead of Card objects (or (smart) pointers to Card objects)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Well, that's great. The array is an array that "will store the 5 cards for player1". So why are you trying to store chars or strings instead of Card objects (or (smart) pointers to Card objects)?
    smart pointer<<== will have to look that up.. I tried using pointers, and references but It still would error out... I also tried another object.. but the output comes out funny. See below.

    Player 1 deck contains:
    ♀²( Clu─├
    ♀²( of ─├╠o(²
    ♀²( Spa─├╠
    ♀²( Dia─├╠o(
    ♀²( Hea─├╠

    here is the code added.. char* toPlayer(); to the class Card.

    Code:
    class Card
    {
    public:
        int face;
        int suit;
    
        void toString() {
            cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
        }
    
    char* toPlayer()
     {
      char player1Card[100]= {0};
      
            strcat(player1Card,FaceString[face]); // look up the strcat() function
            strcat(player1Card," of ");
            strcat(player1Card,SuitString[suit]);
            return player1Card; // make sure to declare this. this returns the 5 player cards
    
    };
    added this to main

    Code:
    cout << "Player 1 deck contains:" << endl;
          for (int j = 0; j < 5; j++)
            {
               player1[j]=deck[j].toPlayer(); // assign 5 cards to player 1.
               cout << player1[j] << endl;
            }

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    More errors:
    1) As laserlight says, a player's hand should be a hand of cards, or card pointers, not a hand of chars or strings. or anything like it.
    2)std::string is better that having an array of chars. use it if you need a string. It means you don't have to muck with strcat and friends.
    3)You're returning reference to a local array. This is not allowed. Arrays can only be used by reference -- this idiosyncrasy why using raw C arrays is a bad idea. Use std::array if you need an array generally, because that behaves like other objects in C++. Except for strings you want an std::string. You probably should use std::array for your deck, and for the player hand.
    4)It makes no sense to convert a card to a player. This is related to #1, but sane naming is the issue here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  2. Passing an Array
    By Jpeg6 in forum C Programming
    Replies: 5
    Last Post: 11-07-2010, 09:34 AM
  3. array passing
    By manwhoonlyeats in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2003, 12:51 PM
  4. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM
  5. help with passing 2d array...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-05-2002, 02:06 PM

Tags for this Thread