Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this blackjack program must have if/else statements in the show_score function t

ID: 3632669 • Letter: T

Question

this blackjack program must have if/else statements in the show_score function that will take into account if the hand is > 21 and an ace is held. The if/else statements will make the ace value 1 so the hand won't bust. Also, I think there is something wrong in the card_value switch statement, which was from another source, as the default value does not return a -1 if a wrong card is entered. Any help provided here will be appreciated:

#include <iostream>
using namespace std;
//function declarations
void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5);//Value of cards entered
int calc_hand (int c1, int c2, int c3, int c4, int c5);//Adds value of cards
void show_score(int hand);//outputs score
int card_value(char card);//takes type of card character and returns value to get_hand

int main( )
{
int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 =0 ;
int hand = 0;

get_hand( c1, c2, c3, c4, c5);
hand = calc_hand( c1, c2,c3, c4, c5);
show_score(hand);

return 0;
}
void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5)
{
int cards;
char input; //this is a char variable to hold input from the user
cout << "enter qty of cards ";
cin >> cards;

cout << "enter cards ";
c3=0,c4=0,c5=0;

cin>>(input); // this will get a single character from the user into the input variable
c1 = card_value(input);//this will assign the value returned by the card_value function and store it in c1

cin>>(input);
c2 = card_value(input);
if(cards >=3)
{
cin>>(input);
c3 = card_value(input);
}
if(cards >=4)
{
cin>>(input);
c4 = card_value(input);
}
if(cards >=5)
{
cin>>(input);
c5 = card_value(input);
}

}
int card_value(char card) // This function will return the card value of any of the cards
{ // If a wrong card is entered the function returns -1
switch(card)
{
case '1':
return 1;
case 'a':
return 11;
case 'A':
return 11;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'T':
return 10;
case 't':
return 10;
case 'J':
return 10;
case 'j':
return 10;
case 'Q':
return 10;
case 'q':
return 10;
case 'K':
return 10;
case 'k':
return 10;
default:
return -1;

}
}
int calc_hand (int c1, int c2, int c3, int c4, int c5)
{
return c1+c2+c3+c4+c5;
}
void show_score(int hand)
{
if (hand > 21)
cout << "busted ";
else
cout << "score is: " << hand << " ";
}

Explanation / Answer

#include using namespace std; //function declarations void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5);//Value of cards entered int calc_hand (int c1, int c2, int c3, int c4, int c5);//Adds value of cards void show_score(int hand);//outputs score int card_value(char card);//takes type of card character and returns value to get_hand int main( ) { int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 =0 ; int hand = 0; get_hand( c1, c2, c3, c4, c5); hand = calc_hand( c1, c2,c3, c4, c5); show_score(hand); return 0; } void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5) { int cards; char input; //this is a char variable to hold input from the user cout > cards; cout >(input); // this will get a single character from the user into the input variable c1 = card_value(input);//this will assign the value returned by the card_value function and store it in c1 cin>>(input); c2 = card_value(input); if(cards >=3) { cin>>(input); c3 = card_value(input); } if(cards >=4) { cin>>(input); c4 = card_value(input); } if(cards >=5) { cin>>(input); c5 = card_value(input); } } int card_value(char card) // This function will return the card value of any of the cards { // If a wrong card is entered the function returns -1 switch(card) { case '1': return 1; case 'a': return 11; case 'A': return 11; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'T': return 10; case 't': return 10; case 'J': return 10; case 'j': return 10; case 'Q': return 10; case 'q': return 10; case 'K': return 10; case 'k': return 10; default: return 0; //Your default is actually working, it returns a -1, which subtracts 1 //from the hand...try entering 3 cards: 5,6,z. The hand will be returned as 10 //I've changed it to return 0. That way if an invalid value is entered, the hand //is just the total of the valid values. } } int calc_hand (int c1, int c2, int c3, int c4, int c5) { int ace_count = 0;//counts how many Aces are in the hand if(c1==11) ace_count++; //add one to the ace count if c1 is an ace if(c2==11) ace_count++; //add one to the ace count if c2 is an ace if(c3==11) ace_count++; //add one to the ace count if c3 is an ace if(c4==11) ace_count++; //add one to the ace count if c4 is an ace if(c5==11) ace_count++; //add one to the ace count if c5 is an ace //check if one of the cards is an Ace...per your switch statement, //an Ace is assigned the value of 11, so we simply check if any card is 11 //the condition below determines if the hand is first over 21 //if the hand is over 21, then we check for an Ace, which is worth 11 if( (c1+c2+c3+c4+c5) > 21 && (c1==11 || c2==11 || c3==11 || c4==11 || c5==11) ) { //determine how many Aces should be converted to 1 to //prevent busting while giving the highest hand int amount_over = c1+c2+c3+c4+c5-21;// this is the amount over 21 int amount_to_deduct = 0; if(amount_over 0) //if you have at least 1 ace amount_to_deduct = 10; else if(amount_over >10 && amount_over 1) amount_to_deduct = 20; else if(amount_over >20 && amount_over 2) amount_to_deduct = 30; else if(amount_over >30 && amount_over 3) amount_to_deduct = 40; else if(amount_over >40 && amount_over 4) amount_to_deduct = 50; return c1+c2+c3+c4+c5-amount_to_deduct; //if an Ace was found, let it be worth } //1 rather than 11. We do this by subtracting from the total return c1+c2+c3+c4+c5; //otherwise, if no Ace is in the hand, simply return the total } void show_score(int hand) { if (hand > 21) cout