Segmentation Fault with pointers

Hello again everyone :), this is a program for my class that is just a simple card game. I am experiencing a crash or "Segmentation fault" and I have narrowed it down to line 148 with some couts and I cant figure it out.

Thanks guys, please keep in mind this is still a work in progress :)


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
213
214
215
216
217
# include <iostream>
# include <string>
# include <time.h>

using namespace std;


/*Player Class*/
class Player{

private:
int  cards[];
int cardsTotal;
double Balance;
int bet;
public:
Player(){
Balance = 1000; cards[2];}

~Player(){};

int setCards(int card1, int card2){
cards[1] = card1;
cards[2] = card2;
}

double setBalance(double money){
Balance = money;
}

int setBet(int bet1){
bet = bet1;
}

void reduceBalance(double bet){
Balance =- bet;
}

int getCard() const{
return cards[1];
}

int getCard2() const{
return cards[2];
}

int getTotal() const{
return cardsTotal;
}

int cardTotal(){
cardsTotal = cards[1] + cards[2];
}

double getBalance() const{
return Balance;
}


};


void rand_seed()
{
int seed = static_cast<double>(time(0));
srand(seed);
}



/*Soon to be linked list for round recording*/
struct RoundRecord{
        bool win;
        int bet;
};

/*keeps track of how many cards are left*/
struct Deck{
int numberofCards;
};


/*Computer's AI soon to be improved*/
int Computer(Player* c, Deck d1){
int win = 0;
int ComputerBet;

/*creates a card number 1 - 10*/
int ComputerCard1 = (rand () % 9) + 1;
int ComputerCard2 = (rand () % 9) + 1;

d1.numberofCards - 2;
c->setCards(ComputerCard1, ComputerCard2);
 if ((ComputerCard1 == 3 && ComputerCard2 == 8) or (ComputerCard1 == 8 && ComputerCard2 == 3)){
        win = 1;
        ComputerBet = (rand() % 500) + 500;
        c->setBet(ComputerBet);}
else{
        c->cardTotal();
        ComputerBet = (rand() % 100) + 500;}
}


/* validates menu answers */
void validate (char response){
if (cin.fail())
        {
        {
        cout << "Your response must be y or n please try again." << endl;
        exit(1);
        }
}

/*intro screen*/
char game_Intro(){
char answer;
cout << " ************************************** " << endl;
cout << " *                Suddah              * " << endl;
cout << " *              Version 1.0           * " << endl;
cout << " ************************************** " << endl;
cout << " Would you like to play? (y|n) " << endl;
cin >> answer;
validate(answer);
return answer;
}

char instruction(){
char answer = 'n';
cout << "Each player is givin 1,000 dollars at the start of the game." << endl;
cout << "In addition they are givin ten sets of cards.  Whoever has  " << endl;
cout << "the better set or cards wins!                               " << endl;
cout << "* for the game to be over keep betting agenst the computer and see who runs out of money first!    " << endl;
return answer;
}

char Help(){
char answer;
cout << "Do you know how to play? (y|n) " << endl;
cin >> answer;
validate(answer);
if (answer == 'n'){
instruction();
}
return answer;
}

int Suddah(Player* p, Player* c, Deck d1){
int bet;
int cardNumber1 = (rand() % 9) + 1;
int cardNumber2 = (rand() % 9) + 1;

p->setCards(cardNumber1, cardNumber2);
d1.numberofCards - 2;

cout << "Lets begin! " << endl;
cout << "your balance is: " << p->getBalance() << endl;
cout << "Your cards are:" << p->getCard() << " of diamonds, and " << 

p->getCard2() << " of hearts." << endl;
p->cardTotal();

cout << "Place your bet" << endl;
cin >> bet;
p->setBet(bet);
p->reduceBalance(bet);
Computer(c, d1);
if((p->getTotal() > c->getTotal()) or (cardNumber1 == 3 && cardNumber2 == 8) or (cardNumber1 == 8 && cardNumber2 == 3)){
cout << "Congrats you win the round!" << endl;}
else{
cout << "Sorry maybe next round :( " << endl;}
}


int main(){
bool game = true;
bool round = true;

char response;
char begin_Response;

double money = 1000;

Player* p;
Player* c;
Deck d1;

rand_seed();

response = game_Intro();
if (response == 'y'){
begin_Response = Help();}
{
        if (begin_Response == 'n')
        p = new Player;
        c = new Player;
        d1.numberofCards = 20;
                while(game){
                        while(round){
                        Suddah(p, c, d1);
                        
                        if(p->getBalance() == 0 || c->getBalance() == 0){
                                game = false;
                                round = false;
                                 }
                           }
}

cout << "Thanks for Playing! :) " << endl;

delete p;

delete c;

return 0;
}
}
im sorry it is line 152 now after adding some white space :)
class Player cannot have an array declared like

int array[];

without a length specified.

Thanks for the response but that didn't work, it seems even after I changed that and made the subscripts from 0 to 1 not from 1 to 2 it is still crashing and now I have narrowed it down to line 23 or 24 its crashing while its declaring?
made the subscripts from 0 to 1 not from 1 to 2
You sure about that?
1
2
/*(line 23)*/cards[1] = card1;
cards[2] = card2;

Last edited on
actually yes I did, i didn't upload the new code though didn't have time this morning :P sorry, also I figured out what it was and it was actually my logic in my main function!
Thanks for all the help :)
Rawr
not sure why you declare Player* p like that when u just as might could allocate on the stack

1
2
Player p;
Player c;


then when passing to your functions, declare them pass by reference

 
Suddah(Player& p, Player& c, Deck d1)


Avoid passing pointers if you don't really need to because you then normally need check the pointers since if somebody else one day uses your function and passes NULL it will crash. With reference you can be sure that you get the objects.

note also that you are passing a copy of d1, not the actual d1 so any changes to d1 in your function are done on a copy of the original and not given to the caller.

 
Suddah(Player& p, Player& c, Deck &d1) 


otoh would make a difference when you modify d1.numberofCards after you modify the statement

 
d1.numberofCards - 2


which does currently nothing




Well, it's still possible to have dynamic objects and pass them through references:
1
2
3
//Suddah(Player& p, Player& c, Deck d1)
//Player *p, *c; Deck *d1
Suddah(*p, *c, *d1);


And sometimes it's useful being able to pass null pointers. What will you do then?
Last edited on
Line 192: You have an opening brace for no reason.
Line 193: You have no opening brace here, so Player *p is only ever allocated if reponse = 'n'. If response is something else this will cause a crash in the suddah function on line 152.
Line 23/24: Index should start at 0/1 not 1/2

When allocating you should do.
1
2
3
4
5
Player* p = 0;
p = new player();

if (p != 0)
 delete p;
Topic archived. No new replies allowed.