Compiler and Debug close w/o reason or error message

Hi guys :D
So, I'm writing a simple blackjack card game program, and it's been going pretty well up until I started using vectors. I need them because the size of someone's hand changes and so does the size of the deck, right?
Here's the code:
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cstdlib>
#include <iostream>
#include <strings.h>
#include <vector>

using namespace std;
using std::string;
using std::endl;
using std::vector;

//classes
class CPU
{
      public:
      string sName;
      int nDiff;
      int Getdifficulty(void); 
      string Getname(void);
};

class player
{
     public:
     string sName;
     string Getname();
};

class card
{
      public:
      int nSuit;//0 = clubs 1 = hearts 2 = diamonds 3 = spades
      int nVal;//0=A 1=2 etc... 12 =K
};

class deck
{
      public:
      vector<card> deckofcards;
      string Getcardvalue(int j);
      string Getcardsuit(int f);
      bool batman;
      void Setdeck(void);
      void Showcards(void);
      void Dealcards(void);
};

class hand
{
      public:
      int Sizeofhand;
      vector<card> han();
};

/* END CLASSES
variable deffinition
*/
player p;
deck d;
CPU k;
void Giveinfo(CPU k, player p);
void Setup(player p, CPU k, deck d);

int main(int argc, char *argv[])
{
    Setup(p,k,d);
    d.Showcards();
    system("PAUSE");
    return EXIT_SUCCESS;
};

/*
FUNCTIONS ARE BELOW
*/


string player::Getname(){
       string r;
       cout << "Please type your name (with no spaces)" << endl;
       cin >> r;
       return r;
};

string CPU::Getname(){
       string r;
       cout << "Please type the name of your CPU opponent (no spaces)" << endl;
       cin >> r;
       return r;
};

int CPU::Getdifficulty(void)
{
    int d;
    bool iscorrect;
    while (iscorrect != true)
    {  
        cout << "Please type 1, 2, or 3 for the level \n of difficulty you would like." << endl;
        cin >> d;
        iscorrect=true;
        if (d>3) {
           cout << "That's more than 3, lets try this again..." << endl;
           iscorrect=false;
        }
        else if (d<1) {
             cout << "That's less than 1, lets try this again..." << endl;
             iscorrect=false;
        };
    };
    return d;
};

void Giveinfo(CPU k, player p)
{
     cout << "Your name is " << p.sName << "." << endl;
     cout << "Your opponent is a CPU with a level of " << k.nDiff << "." << endl;
     cout << "His name is " << k.sName << "." << endl;
     return;
};


void deck::Setdeck(void){
     int f;
     //DECLARE DECKOFCARDS SIZE HERE NOW NOW NOW
     deckofcards.resize(52);
     for(f=0; f <52; f++){
         deckofcards[f].nSuit = f%4;
         deckofcards[f].nVal = f%13;
     };
     batman=true;
};

void deck::Showcards(void){
     if (batman=true){
        for (int g=0; g<52; g++){
            cout << Getcardvalue(deckofcards.at(g).nVal) << " of " << Getcardsuit(deckofcards.at(g).nSuit) << endl;
        };
     };
};

string deck::Getcardvalue(int j){
       j=j%13;
       switch (j){
              case 0:
                   return "Ace";
                   break;
              case 1:
                   return "Two";
                   break;
              case 2:
                   return "Three";
                   break;
              case 3:
                   return "Four";
                   break;
              case 4:
                   return "Five";
                   break;
              case 5:
                   return "Six";
                   break;
              case 6:
                   return "Seven";
                   break;
              case 7:
                   return "Eight";
                   break;
              case 8:
                   return "Nine";
                   break;
              case 9:
                   return "Ten";
                   break;
              case 10:
                   return "Jack";
                   break;
              case 11:
                   return "Queen";
                   break;
              case 12:
                   return "King";
                   break;
       };
};

string deck::Getcardsuit(int f){
       f=f%4;
       switch (f){
              case 0:
                   return "Clubs";
                   break;
              case 1:
                   return "Hearts";
                   break;
              case 2:
                   return "Diamonds";
                   break;
              case 3:
                   return "Spades";
                   break;
       };
};

void Setup(player p,CPU k, deck d){
     p.sName=p.Getname();
     k.sName=k.Getname();
     k.nDiff=k.Getdifficulty();
     Giveinfo(k,p);
     d.batman = false;
     d.Setdeck();
};

void deck::Dealcards(void){
};


The problem occurs after the Getdifficulty() function in Setup(player p,CPU k, deck d), if that helps.
I've tried a lot of different things, but it still seems to crash.
Any help would be greatly appreciated, thanks all!
Did you try to step in and debug yet?
1
2
3
4
5
6
7
8
9
//Problem1: 
bool iscorrect;  // uninitialized.  On my system the while loop didn't execute.
    while (iscorrect != true)
    {  

//problem 2: 
int main(int argc, char *argv[]) 
{ 
    Setup(p,k,d); // passes d by value which means a copy is made and modified. 


Now, when setup returns, the global object d is unmodified and is uninitialized. Only a copy of it was modified by the Setup function. You need to pass it by reference. The showcards function is throwing a std::out_of_range error because it is trying to access an empty deck of cards.
I would recommend a couple of things.
a) Change your classes to properly declare member variables private and declare public interface member functions.
b) Add try..catch for safety when using std::vector<T>::at to access elements. The purpose of using at instead of operator[] is because the exception handling is implemented in at. If you don't want to catch an exception, don't bother using the at member function. Either way, you should implement some validation to your code when using dynamic sized arrays.
c) Always initialize local variables at their point of creation.
Thanks a ton for the stuff in the first post, it helped a lot!
I don't really understand some of the things you said in your second post though. What do you mean by declaring member variables private? I want to change them with non-member functions later on so I have them as public. Is this just me interpreting public/private incorrectly? What changes with a private declaration?
Also, how do I "add validation"? This is my first program in C++ so I'm a little slow, sorry.
I try to initialize local variables at creation, but the debugger gives me errors whenever I try to do that :\

How would I give vector<card> deckofcards; a size at initialization? like this? vector<card> deckofcards(52);

Thank you so much for the advise, I really appreciate the help.
Topic archived. No new replies allowed.