Write a program that scores a blackjack hand. In blackjack, a player receives fr
ID: 3535443 • Letter: W
Question
Write a program that scores a blackjack hand. In blackjack, a player receives from 2 to 5 cards. The player decides how many, but hat has no effect on this problem. The cards 2 through 10 are scored as 2 through 10 points. The face cards – jack, queen, and king are scored as 10 points. The ace is either 1 or 11, whatever is better for the user. The goal is to come as close to a score of 21 as possible without going over 21. Hence, any score over 21 is called “bustedâ€. The user is asked how many cards she/he has and the user responds with one of the integers 2, 3, 4, or 5. The user is then asked for the card values. Card values are 2 through 10, jack, queen, king or ace. After reading the values the program should convert them from character values to numeric card scores, taking special care for aces. The output is either a number between 2 and 21 (inclusive) or the word Busted. The user will determine when to end the program.
Explanation / Answer
#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 ";
cin >> cards;
cout << "Enter card values ";
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 << "! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.