Getting a value from object passing it to 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 ve evaluated later. I want to call the array player1[5]. I tried to use pointer but I get the following error

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 #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();
         }



    return 0;
} 
Last edited on
Line 20: toString is declared as returning nothing (void).
Line 49: You're trying to assign the result of toString (nothing) to a pointer (getCard).
Thanks..@AbtstractionAnon
closed account (zqMDizwU)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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;

   int toString() {
	   if (face != NULL && suit !=NULL)
        cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
	   return 0;
	}

};

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++) {  
		 int getCard = deck[j].toString();
         }
	
	system("pause>nul");
  
} 
Line 21: What is the purpose of this if statement? 0 is a valid value for both face and suit.

Line 23: What is the purpose of returning 0?

Line 30: Why are you using new to allocate a single char here? You have a memory leak because you never delete the char that you allocated.

Line 51: All 5 cards drawn are going to be stored in the same variable, overwriting each other. Since toString only ever returns 0, getCard will always contain 0.



Topic archived. No new replies allowed.