Basic Blackjack Game

This is a basic blackjack game. It is stuck saying that I can't have else statements without an if, but I have an if. Also, it still delivers another card even if the user selects n. I'm stuck, please help??


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
      #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int main()
    {
    
    int num1;
    int num2;
    int num3;
    cout << "First Cards: ";
   unsigned seed = time(0);       
   srand(seed);

    num1 = (1 + rand() % 10);   // Lines 17 through 22 deliver the first two cards to the player
    cout << num1 << ", ";
    num2 = (1 + rand() % 10);
    cout << num2 << endl << endl;                                
    cout << "Total: " << num1 + num2 << endl << endl;
    num3 = num1 + num2;

    int num4;
    char response;
                                                          
do  {    
    
    if ( num3 > 21)                             // Supposed to end the loop and deliver the message: Bust!
    {
            break;
    }
    {
        cout << "Bust!" << endl;
    }
    else if (num3 == 21)                    // Supposed to end the loop and deliver the message Congratulations!
    {
            break;
    }
    {
        cout << "Congratulations!" << endl;
    }
    else
    {
    cout << "Do you want another card? (y/n): ";  
    cin >> response;
    }
    cout << endl;
    num4 = (1 + rand() % 10);                           
    cout << "Card: " << num4 << endl << endl;
    cout << "Total: " << num3 + num4 << endl << endl;      
    num3 = num3 + num4;                                     
        
    }
while (response == 'y'); 

return 0;
}

    
    
Last edited on
@mt280

I didn't check this, but it should be ok.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do  {    
    
    if ( num3 > 21)     // Supposed to end the loop and deliver the message: Bust!
    {
            cout << "Bust!" << endl;
    }
    else if (num3 == 21) 
    {
           cout << "Congratulations!" << endl;
    }
    else
    {
    cout << "Do you want another card? (y/n): ";  
    cin >> response;
    }
    cout << endl;
    num4 = (1 + rand() % 10);                           
    cout << "Card: " << num4 << endl << endl;
    cout << "Total: " << num3 + num4 << endl << endl;      
    num3 = num3 + num4;                                     
        
    }
while (response == 'y'); 
Last edited on
Thank you, I fixed it. But I am still having an issue with the initial input. If i select n, it still deals a card. other than that, it works perfectly...

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
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int main()
    {
    
    int num1;
    int num2;
    int num3;
    cout << "First Cards: ";
   unsigned seed = time(0);       
   srand(seed);

    num1 = (1 + rand() % 10);   // Lines 17 through 22 deliver the first two cards to the player
    cout << num1 << ", ";
    num2 = (1 + rand() % 10);
    cout << num2 << endl << endl;                                
    cout << "Total: " << num1 + num2 << endl << endl;
    num3 = num1 + num2;

    int num4;
    char response;
                                                          
do  {    
    
    if ( num3 > 21)     // Supposed to end the loop and deliver the message: Bust!
    {
            cout << "Bust!";
		break;
    }
    else if (num3 == 21) 
    {
           cout << "Congratulations!";
		break;
    }
    else
    {
    cout << "Do you want another card? (y/n): ";  
    cin >> response;
    
    cout << endl;
    num4 = (1 + rand() % 10);                           
    cout << "Card: " << num4 << endl << endl;
    cout << "Total: " << num3 + num4 << endl << endl;      
    num3 = num3 + num4;                                     
    }
    }
while (response == 'y'); 
return 0;
}
Why don't you check response after line 42 before you add num4 to num3 ?
Topic archived. No new replies allowed.