this function causes the application to shut down

1
2
3
4
5
6
7
8
9
10
11
12
bool is_card_suit(string next_char)
{
        
     for(int i=0; i<NUMBER_OF_SUITS; i++)
         {
             if(SUITS[i]==next_char);
                {
                     return(true);
                }
         }
         return(false);
}


when I run the program with this function in it windows tells me that there is a problem that has caused the program to shut down unexpectedly. anyone know why?
Can you post the refering code? Ex. Definition of NUMBER_OF_SUITS, and SUITS, and where the function is called...
1
2
const string SUITS[NUMBER_OF_SUITS]={SPADES,CLUBS,DIAMONDS,HEARTS};
const int NUMBER_OF_SUITS=4;


it compiles without any errors then the console window opens and the error message pops up immediatly.
Last edited on
Try encasing the array elements in quotes.
const string SUITS[NUMBER_OF_SUITS]={"SPADES","CLUBS","DIAMONDS","HEARTS"};
 
const string SPADES="S",CLUBS="C",DIAMONDS="D",HEARTS="H",ACE="A",KING="K",QUEEN="Q",JACK="J", TEN="10";

i had this already too
Can you post your whole source?
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;
const int NUMBER_OF_SUITS=4,NUMBER_OF_CARD_VALUES=13,NUMBER_OF_CARDS_IN_A_HAND=5;
const string SPADES="S",CLUBS="C",DIAMONDS="D",HEARTS="H",ACE="A",KING="K",QUEEN="Q",JACK="J", TEN="10";
const string SUITS[NUMBER_OF_SUITS]={SPADES,CLUBS,DIAMONDS,HEARTS};
const string CARD_VALUES[NUMBER_OF_CARD_VALUES]={ACE,KING,QUEEN,JACK,TEN,"9","8","7","6","5","4","3","2"};
const char INPUT_FILE_NAME[]="player1.dat";
const char OUTPUT_FILE_NAME[]="output.dat";
const int MAX_BET=100;//maximum allowed bet
const string SPACE=" ";// sets a const for whitespace
const string DOLLAR_SIGN="$";//sets a const for dollar sign
const string INTERGERS[]={"0","1","2","3","4","5","6","7","8","9"};// list of 
// possible intergers
const int NUMBER_OF_CHARACTERS_BEING_CONVERTED_TO_STRING=1;

/*void clear_space(ifstream& in_stream,char& next_char)
//checks if next char is space and if it is it sets the next char to the 
// next character in the stream
//NEED TO WORK ON THIS LATER
{
     string string_variable=(string(NUMBER_OF_CHARACTERS_BEING_CONVERTED_TO_STRING,
                      next_char));
     while(string_variable==SPACE)
     {
                                
         in_stream.get(next_char);
         string_variable=(string(1,next_char));
         
          
     }
}*/

bool is_digit(string next_char)
// checks if next character is a digit it returns a bool
{
     for(int i=0;i<=9;i++)
        {
             if(next_char==INTERGERS[i])
             {
                  return(1);
             }
        }
        return(0);
}
     

bool is_bet(string new_string_variable)
//checks if next char is a possible bet
{    
     for(int i=0;i<10;i++)
     {
         if(new_string_variable==INTERGERS[i])
         {
              return(1);
         }
         return(0);    
     }
     
}
          
     
int is_card_value(string next_char)
//checks if a character is possibly a card value and returns true if it is
//returns false otherwise
{
    /* if(next_char=='a'||next_char=='A'||next_char=='j'||next_char=='J'
        ||next_char=='k'||next_char=='K'||next_char=='Q'||next_char=='q'
        ||isdigit(next_char))
        return (true);
     else 
        return (false);*/
        
     for(int i=0;i<NUMBER_OF_CARD_VALUES;i++)
           {
                if(CARD_VALUES[i] == next_char)
                  {
                       return(1);
                  }
                
           }       
        return(0);
        
}

/*bool is_card_suit(string next_char)
{
        
     for(int i=0; i<NUMBER_OF_SUITS; i++)
         {
             if(SUITS[i]==next_char);
                {
                     return(true);
                }
         }
         return(false);
}*/

bool is_card_suit(string new_string_variable)
{
     int i=0;
     while(i<4)
     {
               if(SUITS[i]==new_string_variable)
               {
                    return(true);
               }
     }
     return(0);
    
}
     
void open_input_file(const char INPUT_FILE_NAME[], ifstream& in_stream)
//opens input file using the const file name and passes the stream out of the
// function
{
     
     in_stream.open(INPUT_FILE_NAME);
     if(in_stream.fail())
     {
           cout<<"**input stream failed to open**/n";
     }
}     

void open_output_file(const char OUTPUT_FILE_NAME[], ofstream& out_stream)
//opens the output file and passes the stream out
{    
     out_stream.open(OUTPUT_FILE_NAME);
     if (out_stream.fail())
     {
           cout<< "**output stream failed to open**/n";
     }
}

/*void read_player1_hand(ifstream& in_stream)
{    char next_char;
     string player1_hand[NUMBER_OF_CARDS_IN_A_HAND];
     while(!in_stream.eof())
     {
          in_stream.get(next_char);
          while(isdigit(next_char)
          { 
               player1_hand[i]+=(static_cast<string>(next_char))
          }     
          if(isalpha(next_char)
          {
               player1_hand[i]+=(static_cast<string>(next_char))
          }*/
 
void write_hand(string& player1_hand, ofstream& out_stream)
//writes hand to output
{
     out_stream<<player1_hand; 
}

void read_input_file(ifstream& in_stream, string& player1_hand)
//seperates hand into components. i.e. bet, card value and suit.
{
     string player1_hand_and_bet;
     string card_value[NUMBER_OF_CARDS_IN_A_HAND];
     string card_suit[NUMBER_OF_CARDS_IN_A_HAND];
     string bet;
     int value_count=0, suit_count=0;
     char next_char;
     int i=0;
     while(!in_stream.eof())
     {
          in_stream.get(next_char);
          {
               i++;
               cout<<next_char<<endl;
               string new_string_variable=(string(NUMBER_OF_CHARACTERS_BEING_CONVERTED_TO_STRING,next_char));
               cout<<i<<endl;
               cout<<"string variable:"<<new_string_variable<<endl;
               if(is_card_value(new_string_variable))
               {
                    
                    card_value[value_count]+=(new_string_variable);
                    value_count++;
                    cout<<card_value[value_count];
                    
               }
               else if(is_card_suit(new_string_variable))
               {
                    card_suit[suit_count]=(new_string_variable);
                    suit_count++;
               }
               else if(is_bet(new_string_variable))
               {
                   bet+=(string(NUMBER_OF_CHARACTERS_BEING_CONVERTED_TO_STRING,next_char));
               }
          }
     }
               
            
     
        
     
     for(int i=0;i<NUMBER_OF_CARDS_IN_A_HAND;i++)
     {
          
          player1_hand_and_bet+= (card_suit[i]);
          player1_hand_and_bet+= (card_value[i]);
          player1_hand_and_bet+= (SPACE);
     }
     player1_hand_and_bet+= (DOLLAR_SIGN);
     player1_hand_and_bet+= (bet);
     cout<<player1_hand_and_bet<<"is hand and bet\n";
     }
     
     
} 
 
void read_and_write_player1_hand(ifstream& in_stream,ofstream& out_stream)
{
  
     string card_value[NUMBER_OF_CARDS_IN_A_HAND];
     string card_suit[NUMBER_OF_CARDS_IN_A_HAND]; 
     string next_char;
     string player1_hand;
     string bet;
     read_input_file( in_stream,player1_hand);
     write_hand(player1_hand,out_stream);
          
}

void close_files(ifstream& in_stream,ofstream& out_stream)
//closes both files
{
     in_stream.close();
     out_stream.close();
}   





int main(int argc, char *argv[])
{   
    string player1_hand;
    ifstream in_stream;
    ofstream out_stream;
    open_input_file(INPUT_FILE_NAME, in_stream);
    open_output_file(OUTPUT_FILE_NAME,out_stream);
    read_input_file(in_stream,player1_hand);
    write_hand(player1_hand,out_stream);
    close_files(in_stream,out_stream);
 
    
    
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
my biggest problem right now is that when it gets to the while loop in the read input file function everything goes wrong and i cant figure out at all what wrong with those functions inside the while loop. the program only runs when those three functions inside the while loop are commented out i can figure out whats wrong.
the input file player1.dat looks like this

3C4H5D6SJC 20

and thats it
The code don't even compile because you have an extra } on line 213.

Inside is_card_suit you never update i so the loop run forever.


I love your constant names, NUMBER_OF_CHARACTERS_BEING_CONVERTED_TO_STRING :P
thanks but even when i was using the for loop it wasn't working i switched to
1
2
3
4
5
6
7
8
9
10
11
bool another_is_suit(string v)
{
     if(v==SPADES||v==HEARTS||v==DIAMONDS||v==CLUBS)
     { 
       return(true);
     }
     else
     { 
         return(false);
     }
}
I didn't have a full study on your program, but I think that the problem must come from your array of strings.

Arrays are low-level, so it is easier to do something wrong with it.
The compiler does not check that the code matches what you want to do (how could it anyway ?), it just checks that it
has a correct syntax. But at run-time, it is possible that things happen that are not legal (example: a buffer overflow).

I will copy-paste your program and take a look at it to get it working.
I telll you when I am done.

Waiting, take a look at std::vector : this managed container disables you to do wrong things ;)
Topic archived. No new replies allowed.