How can the values of this function get associated to the values for calc_hand f
ID: 3631721 • Letter: H
Question
How can the values of this function get associated to the values for calc_hand function. I'm stuck at associating card_value because of "char" won"t be accepted
Function:
int card_value(char card)
{
switch(card)
{
case '1':
return 11;
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;
}
}
calc_hand program:
#include <iostream>
using namespace std;
void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5);
int calc_hand (int c1, int c2, int c3, int c4, int c5);
void show_score(int hand);
int main( )
{
int c1, c2,c3, c4, c5;
int hand;
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)
{
cout << "enter cards ";
cin >>c1 >> c2;
c3=0;
c4=0;
c4=0;
}
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
/* I didn't have any problems with the card_value function accepting characters you just need to be sure to assign the value returned by the value as I did below in the get hand function */ int card_value(char card) { switch(card) { case '1': case 'a': 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; /* you can have multiple values execute the same code if you have them stacked like this also if it were anything other than returns being executedyou would want a break; statement so the switch does not continue executing each case below the first one that you wanted */ case 'T': case 't': case 'J': case 'j': case 'Q': case 'q': case 'K': case 'k': return 10; default: return -1; } } //calc_hand program: #include using namespace std; void get_hand(int& c1, int& c2, int& c3, int& c4, int& c5); int calc_hand (int c1, int c2, int c3, int c4, int c5); void show_score(int hand); int main( ) { /* you normally want to set variables to 0 when you initialize them because you can get junk values otherwise. the program was outputting some weird numbers for score when they were not being assigned in the following lines */ 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) { char input; //this is a char variable to hold input from the user coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.