project

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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;

// Function prototypes
void BuildDeck( int deck[], const int size );
void PrintDeck( int deck[], const int size );
void PrintCard( int card );
void Deal( int deck[], int play[][3] );
void PickUp( int deck[], int play[][3], int column );
void SecretCard( int deck[] );
string Cap_Name(string word);

int main(void)
{
    
    /* declare and initialize variables */
    int column = 0, i = 0;
    int SeeTheDeck = 0;
    int PlayAgain = 0;
    string name;
    
    
    /* Declare a 52 element array of integers to be used as the deck of cards */
    int deck[52] = {0};
    
    /* Declare a 7 by 3 array to receive the cards dealt to play the trick */
    int play[7][3] = {0};
    
    
    
    /* Generate a random seed for the random number generator. */
    srand(time(NULL));
    
    
    
    /* Openning message.  Ask the player for his/her name */
    cout << "Hello, I am a really tricky computer program and " << endl
    << "I can even perform a card trick.  Here's how." << endl
    << "To begin the card trick type in your name: ";
    cin >> name;
    
    /* Capitalize the first letter of the person's name. */
    
    name = Cap_Name(name);
    
    cout << endl << "Thank you " << name << endl;
    
    do
    {
        /* Build the deck */
        BuildDeck(deck, 52);
        
        
        /* Ask if the player wants to see the entire deck. If so, print it out. */
        cout << "Ok " << name << ", first things first.  Do you want to see what " << endl << "the deck of cards looks like (1 = y/0 = n)? ";
        cin >>  SeeTheDeck;
        
        if (SeeTheDeck == '1')
        {
            cout << endl;
            PrintDeck(deck, 52);
        }
        
        
        
        
        cout << endl << name << " pick a card and remember it..." << endl;
        
        /* Begin the card trick loop */
        for(i = 0; i < 3; i++)
        {
            /* Begin the trick by calling the function to deal out the first 21 cards */
            
            Deal(deck, play);
            
            
            /* Include error checking for entering which column */
            do
            {
                /* Ask the player to pick a card and identify the column where the card is */
                cout << endl <<"Which column is your card in (0, 1, or 2)?: ";
                cin >> column;
            } while(column < 0 || column > 2);
            
            /* Pick up the cards, by column, with the selected column second */
           
            
            PickUp(deck, play, column);
            
            
        }
        
        /* Display the top ten cards, then reveal the secret card */
        
        SecretCard(deck);
        
        
        
        
        /* if the player wants to play again */
        cout << name << ", would you like to play again (y =1 /n = 0)? ";
        cin >> PlayAgain;
    } while(PlayAgain == 'y');
    
    /* Exiting message */
    cout << endl << endl << "Thank you for playing the card trick!" << endl;
    
    
    return 0;
    
    
} // End of main()

 void BuildDeck( int deck[], const int size)
{
    int used[52] = {0};
    int card = 0, i = 0;
    
            
        
    
    
    /* Generate cards until the deck is full of integers */
    while(i < 52)
    {
        /* generate a random number between 0 and 51 */
        card = rand() % 52;
        
        /* Check the used array at the position of the card.
         If 0, add the card and set the used location to 1.  If 1, generate another number */
    if(used[card] != 1)
        {
            
        }
    }
    return;
 }

 void PrintDeck( int deck[], const int size )
 {
    int i;
    
    /* Print out each card in the deck */
    
     for (i = 0; i < 51; i++)
    {
        
        PrintCard(deck [i]);
        cout << endl;
    }
    
    
 }
 void Deal( int deck[], int play[][3] )
 {
    int row = 0, col = 0, card = 0;
     
     /* deal cards by passing addresses of cardvalues from
     the deck array to the play array                   */
    cout << endl;
    cout << "   Column 0           Column 1           Column 2";
    cout << "======================================================="
    << endl;
    
     
     
     
     
     
     for (row = 0; row < 7; row ++)
     {
         for (int column = 0; column <  3; column++)
             
         {
             cout.width(5);
             play[row][column] = deck [card];
             PrintCard(deck[card]);
             card++;
         }
         cout << endl; 
     }
 
    
    
    
    
    return;
 }

void PrintCard( int card ) //Card will be between 0 and 51
{
    int rank = 0;
    int suit = 0;
    
    // Determine the rank of the card and print it out i.e. Queen
    rank = card % 13;
    
    
    // Determine the suit of the card and print it out i.e. of Clubs
    suit = card / 13;
    
    switch (rank)
    {
        case 0:
            if(rank == 0)
            cout << "  Ace";
            break;
        case 1:
             if (rank == 12)
            cout << " King";
            break;
        case 11:
             if (rank == 10)
            cout << " Jack";
            break;
        case 12:
             if (rank == 11)
            cout << "Queen";
            break;
        default:
            cout << setw(5) << rank;
    }
    
    switch (suit)
    {
        case 0:
            if (suit == 0)
            cout << " of Clubs   ";
            break;
            
        case 1:
             if (suit == 2)
            cout << " of Hearts   ";
            break;
        case 2:
             if (suit == 1)
            cout << " of Diamonds ";
            break;
            
        case 3: //check and make sure though
           cout << " of Spades    ";
            break;
            
    }
    
    
    return;
}

void PickUp( int deck[], int play[][3], int column )
{
    int card = 0, row = 0;
    int first = 0, last = 0;
    
    
    switch(column)
    {
        case 0:
            first = 1;
            last = 2;
            break;
            
        case 1:
            first = 0;
            last = 2;
            break;
            
        case 2:
            first = 0;
            last = 1;
            break;
    }
    
    
    
    for(int row = 0; row < 7; row ++)
    
    
        {
            deck[card++] = play[row][first];
        }
    
    for(int row = 0; row < 7; row++)
    {
        deck[card++] = play[row][column];
    }
    
    for(int row = 0; row < 7; row++)
    {
        deck[card++] = play[row][last];
    }
    
    
    return;
}

void SecretCard( int deck[] )
{
    int card = 0;
    
    cout << endl <<"Finding secret card...";
    for(card = 0; card < 10; card++)
    {
        PrintCard(deck[card]);
        cout << endl;
    }
    
    cout << endl <<"Your secret card is: ";
    PrintCard(deck[card]);
    cout << endl;
    return;
}

    // Capitalize the first letter in a string
string Cap_Name(string word)
{
    
    if (!word.empty())
    {
        word[0] = toupper(word[0]);
    }
    return word;
    
   [code]

}[/code]



So this is the code i have so far for my project card trick. I feel like its almost done however. I am having issues when it comes to running it the entire thing together with the capitalizing of the first letter of what ever name the user my input. if i comment it out then the program runs but it breaks as soon as i input my name and closes out. Any help would be greatly appreciated.



so for some reason i cant get the code tags to work so ill post a shell to make the code viewable i was able to fix my problem but something is wrong with one of my loops where it wont run correctly Short URL: cpp.sh/4crg
Last edited on
closed account (48T7M4Gy)
The immediate problem is could you please put code tags (<> in the format box on the right of the screen) around your listing. It should then come up in a purple box which enables the code to be compiles on line and it will have line numbers. :)

PS you can then tell us what lines are causing you trouble. Thanks
Topic archived. No new replies allowed.