### 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.)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145`` ``````#include 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
 ``12`` ``````// 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.