Card combination problem [updated at 8th floor]

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
#include <iostream>
#include <vector>

using namespace std;

public class Card {
 public:
  Card(string value, string suit);
  string value,suit;
};

int main() {
    vector<Card> deck;

    string value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    string suit[] = {"S","H","C","D"};

   for (int j=0; j<13; j++) {
        for (int i=0; i<4; i++) {
                deck.push_back(new Card(value[j],suit[i]));
        }
    }
    return 0;
}


Is it error because i using string?
I am not good in vector, keep finding how to combine value and suit into one card in simple way.


i oso tried append

1
2
3
4
string lv[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
string hs[] = {"S","H","C","D"};

    Card cards[52];

1
2
3
4
5
6
7
8
9
10
11
void combine(string cards[])
{
   int index = 0;
   for(int h=0; h<4; h++)
  {
    for(int i=0; i<52;i++)
  {
   cards[index] = lv[i].append(hs[h]);
   index++;
  }
  }



fail
Last edited on
Cards constructor is only declared and never defined, meaning there is no code for it, so when you call it at line 20 the compiler doesnt know what to do.
I guess that's ok to have string value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
but i would make it a char array.
deck.push_back(new Card(value[j],suit[i])); if you call new Card it will return a pointer to a card and your vector is just storing cards.
define the constructor and remove the new and you should be ok.
define?

1
2
3
Card(); 
   Card(string suit,string value);
 ~Card()


then delete the new?

or copy the

1
2
string value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    string suit[] = {"S","H","C","D"}

again inside
define?


To define the constructor inline use

1
2
3
4
5
public class Card {
 public:
  Card(string v, string s) : value(v), suit(s) {}
  string value,suit;
};


to define it outside of the class

1
2
3
4
5
6
7
8
9
public class Card {
 public:
  Card(string v, string s);
  string value,suit;
};

Card::Card(string v, string s) : value(v), suit(s)
{
}


(I renamed your parameters so they were different to the member variable names.)

then delete the new?


As you have declared your vector as

vector<Card> deck;

You have to push_back like this (without a new)

deck.push_back(Card(value[j],suit[i]));

If you want to push newed cards, you need to define the vector (to store Card pointers) as

vector<Card*> deck;

BUT then you'd have to remember to delete all the cards later, which is unnecessarily hard work for such a small class.

or copy the / again inside


Not sure I get this. Don't see any other problems.

AND you could swap your string array for a char const * const array, as it stand.

char const * const hs[] = {"S","H","C","D"};

Swapping to use a char array would need a little more work. And I would prob. do that in conjunction with a change of member variable type.

Andy

P.S. Using a string to store a single char is a bit overkill. So unless you plan to use "King", "Queen", etc. in the future, you might want to adjust your member's types.
Last edited on
error. still cant store a card. need find way to fix them then start think how to shuffle and display.

now is like this

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
#include <iostream>
#include <vector>

using namespace std;


public class Card {
 public:
  Card(string v, string s) : value(v), suit(s) {}
  string value,suit;
};

int main() {
    vector<Card> deck;

    string value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    string suit[] = {"S","H","C","D"};

    for (int j=0; j<13; j++) {
        for (int i=0; i<4; i++) {
                deck.push_back(Card(value[j],suit[i]));
        }
    }
    return 0;
}




|7|error: expected unqualified-id before "public"|
|14|error: `Card' was not declared in this scope|
|14|error: template argument 1 is invalid|
|14|error: template argument 2 is invalid|
|14|error: invalid type in declaration before ';' token|
|23|error: request for member `push_back' in `deck', which is of non-class type `int'|


Last edited on
dont put public in front of the keyword class.

public class Card should be class Card public classes r some other language
Last edited on
thanks guys.

now i move on to shuffle function and display all

1
2
3
4
5
6
7
8
9
10
11
12
void shuffle(string deck[])
{
    for(int i = 0; i<51; i++)
    {
        int ranNo = rand() % 52;
        swap(deck[i],cards[ranNo]);
    }

}

cout << deck[i];


@@
Topic archived. No new replies allowed.