Blackjack card hand score

I need to create a program to score a blackjack hand. This is how it needs to be done, i dont need loops or switches, i just need to know why I get really big negative numbers.



#include <iostream>
using namespace std;

int main()

{

int total, cards;
char c1, c2, c3, c4, c5;
total=0;

cout << "Enter the number of cards you have\n";
cin >> cards;

cout << "Enter card values\n";

if (cards==2)
{
cin >> c1 >> c2;
}
else if (cards==3)
{
cin >> c1 >> c2 >> c3;
}
else if (cards==4)
{
cin >> c1 >> c2 >> c3 >> c4;
}
else if (cards==5)
{
cin >> c1 >> c2 >> c3 >> c4 >> c5;
}

if ((c1>='2')&&(c1<='9'))
{
total+=c1-'0';
}
else if ((c1=='t')||(c1=='j')||(c1=='q')||(c1=='k'))
{
total+=10;
}
else if (c1=='a')
{
total+=11;
}
total=total+(c1-'0');

if ((c2>='2')&&(c2<='9'))
{
total+=c2-'0';
}
else if ((c2=='t')||(c2=='j')||(c2=='q')||(c2=='k'))
{
total+=10;
}
else if (c2=='a')
{
total+=11;
}
total=total+(c2-'0');

if ((c3>='2')&&(c3<='9'))
{
total=c3-'0';
}
else if ((c3=='t')||(c3=='j')||(c3=='q')||(c3=='k'))
{
total+=10;
}
else if (c3=='a')
{
total+=11;
}
total=total+(c3-'0');

if ((c4>='2')&&(c4<='9'))
{
total+=c4-'0';
}
else if ((c4=='t')||(c4=='j')||(c4=='q')||(c4=='k'))
{
total+=10;
}
else if (c4=='a')
{
total+=11;
}
total=total+(c4-'0');
if ((c5>='2')&&(c5<='9'))
{
total+=c5-'0';
}
else if ((c5=='t')||(c5=='j')||(c5=='q')||(c5=='k'))
{
total+=10;
}
else if (c5=='a')
{
total+=11;
}
total=total+(c5-'0');

if ((total>21)&&(c1=='a'))
{
total-=10;
}
if ((total>21)&&(c2=='a'))
{
total-=10;
}
if ((total>21)&&(c3=='a'))
{
total-=10;
}
if ((total>21)&&(c4=='a'))
{
total-=10;
}
if ((total>21)&&(c5=='a'))
{
total-=10;
}

total = c1 + c2 + c3 + c4 + c5;

cout << "Your total score is " << total << "!\n";

}
Please see comments in the following code (and please use code tags in the future - the <> button.)

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

int main()

{

    int total, cards;
    char c1, c2, c3, c4, c5;
    total=0;

    cout << "Enter the number of cards you have\n";
    cin >> cards;

    cout << "Enter card values\n";

    if (cards==2)
    {
        cin >> c1 >> c2;
    }
    else if (cards==3)
    {
        cin >> c1 >> c2 >> c3;
    }
    else if (cards==4)
    {
        cin >> c1 >> c2 >> c3 >> c4;
    }
    else if (cards==5)
    {
        cin >> c1 >> c2 >> c3 >> c4 >> c5;
    }

    // whether you've entered 2 or 5 cards, the following code
    // assumes all 5 cards contain relevant values.

    if ((c1>='2')&&(c1<='9'))
    {
        total+=c1-'0';
    }
    else if ((c1=='t')||(c1=='j')||(c1=='q')||(c1=='k'))
    {
        total+=10;
    }
    else if (c1=='a')
    {
        total+=11;
    }

    // the following line shouldn't be here.
    total=total+(c1-'0');

    if ((c2>='2')&&(c2<='9'))
    {
        total+=c2-'0';
    }
    else if ((c2=='t')||(c2=='j')||(c2=='q')||(c2=='k'))
    {
        total+=10;
    }
    else if (c2=='a')
    {
        total+=11;
    }

    // the following line shouldn't be here.
    total=total+(c2-'0');

    if ((c3>='2')&&(c3<='9'))
    {
        total=c3-'0';
    }
    else if ((c3=='t')||(c3=='j')||(c3=='q')||(c3=='k'))
    {
        total+=10;
    }
    else if (c3=='a')
    {
        total+=11;
    }

    // the following line shouldn't be here.
    total=total+(c3-'0');

    if ((c4>='2')&&(c4<='9'))
    {
        total+=c4-'0';
    }
    else if ((c4=='t')||(c4=='j')||(c4=='q')||(c4=='k'))
    {
        total+=10;
    }
    else if (c4=='a')
    {
        total+=11;
    }
    // the following line shouldn't be here.
    total=total+(c4-'0');

    if ((c5>='2')&&(c5<='9'))
    {
        total+=c5-'0';
    }
    else if ((c5=='t')||(c5=='j')||(c5=='q')||(c5=='k'))
    {
        total+=10;
    }
    else if (c5=='a')
    {
        total+=11;
    }

    //the following line shouldn't be here.
    total=total+(c5-'0');

    if ((total>21)&&(c1=='a'))
    {
        total-=10;
    }
    if ((total>21)&&(c2=='a'))
    {
        total-=10;
    }
    if ((total>21)&&(c3=='a'))
    {
        total-=10;
    }
    if ((total>21)&&(c4=='a'))
    {
        total-=10;
    }
    if ((total>21)&&(c5=='a'))
    {
        total-=10;
    }

    // the following line shouldn't be here.
    // note that this line negates all of the
    // previous updates to total, and then does
    // something nonsensical to calculate the total
    total = c1 + c2 + c3 + c4 + c5;

    cout << "Your total score is " << total << "!\n";

}

ok i deleted those lines but i dont get your first comment
1
2
// whether you've entered 2 or 5 cards, the following code
// assumes all 5 cards contain relevant values. 


What's not to get? If I tell the program I want to enter 2 cards, and then enter two cards, the rest of the program treats c3, c4 and c5 the same way as it would if I'd entered 5 cards, and it shouldn't because c3, c4 and c5 have random junk in them.
Last edited on
ok i guess i get it, but how do i fix it?
im sorry, but im new to this
You've got a variable, cards, that contains the number of cards entered by the user. How do you think you can use that to fix the issue?
thats my problem, i dont know what to do, do my if statements do anything if i say, if (cards==2)...blah blah blah
i really need to get this done
Topic archived. No new replies allowed.