All my variables are using the default constructor?

How do I set the values of my array to the variables in the class?

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
57
58
59
60
61
62
63
64
65
66
67
68
  #include <cstdlib>
#include <iostream>

using namespace std;

class Card {
      private:
              string suit;
              int value;
      public:
             Card (){
                  suit = "x";
                  value = 0;
                  };
             Card (int v, string s){
                  value = v;
                  suit = s;
                  };
             void setValue(int v){
                 value = v;
                 };
             int getValue(){
                 return value;
                 };
             void setSuit(string s){
                  suit = s;
                  };
             string getSuit(){
                    return suit;
                    };
             void printCard(){
                  if ((value <=10)&&(value > 1)){
                            cout << value << " of ";
                            }
                  else if (value == 1){
                            cout << "Ace of ";
                            }
                  if (value >= 11){
                            if(value == 11){
                                     cout << "Jack of ";
                                     }
                            if (value == 12){
                                      cout << "Queen of ";
                                      }
                            if (value == 13){
                                      cout << "King of ";
                                      }
                            }
                  cout << suit << endl;
                  };
};
                  

int main(int argc, char *argv[])
{
    Card myCards[52];
    for (int suit = 1; suit <= 4; suit++){
        for(int value = 1; value <= 13; value++){
                if (suit == 1){
                         myCards[(suit * 13 - 1) + value] = Card(value, "Spades");
                         myCards[value].printCard();
                         }
                }
        }

    system("PAUSE");
    return EXIT_SUCCESS;
}
The index you use at line 60 range from 13 to 64, so you'll need to fix them in order to access the right elements of the array.

Your default constructor is fine though and you have the right idea for initializing each member of the array.

Something you can consider doing for the loop is:
1
2
3
4
5
6
7
8
for (int i = 0; i<52; i++) {
	int suit = i/13+ 1;
	int value = i%13+ 1;
	string strSuit; //do whatever you have to do get the string
	myCards[i].setValue(value);
	myCards[i].setSuit(strSuit);
	myCards[i].printCard();
}
Last edited on
Topic archived. No new replies allowed.