SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
More Flow Of Control
Exercise:
Programming Projects
Question:8 | ISBN:9780321531346 | Edition: 7

Question

Write a program that scores a blackjack hand. In blackjack, a player receives from two to five cards. The cards 2 through 10 are scored as 2 through 10 points each. The face cards—jack, queen, and king—are scored as 10 points. 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 ace can count as either 1 or 11, whichever is better for the user. For example, an ace and a 10 can be scored as either 11 or 21. Since 21 is a better score, this hand is scored as 21. An ace and two 8s can be scored as either 17 or 27. Since 27 is a “busted” score, this hand is scored as 17.

The user is asked how many cards she or 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, and ace. A good way to handle input is to use the type char so that the card input 2, for example, is read as the character '2', rather than as the number 2. Input the values 2 through 9 as the characters '2' through '9'. Input the values 10, jack, queen, king, and ace as the characters 't', 'j', 'q', 'k', and 'a'.(Of course, the user does not type in the single quotes.) Be sure to allow upper- as well as lowercase letters as input. After reading in 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.

You are likely to have one or more long multiway branches that uses a switch statement or nested if-else statement. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include<iostream>
using namespace std;
void main()
{
	int numOfCards,number,acecnt;
	char choice, card;
	do	
	{
	  number = 0;
	  acecnt =0;
       cout<<"Enter number of cards(2-5):";
	  cin>>numOfCards;
	  if(numOfCards<2 || numOfCards >5)
	  	cout<<"Invalid input";
	  else
	   {
		for(int j = numOfCards; j>0; j--)
		{
		  cout<<"Enter number of card: ";
		  cin>>card;
		  if(card>='2' && card<='9')
		  {
		 	number  = number + 2 + (int)(card- '2');
		  }
		  else if (card =='j' || card =='J'|| 
                     card =='t'|| card =='T'
				|| card =='q'|| card =='Q'||
                         card =='k'|| card =='K')
		  {
		    number+=10;
		   }
		  else if (card =='a'|| card == 'A')
		  {
		    acecnt++;
		  }	
	 	}
	   }
        if(acecnt)
	   {
		while(acecnt>1)
		{
		  number+= 1;
		  acecnt--;
		}
		if(number >10 && acecnt == 1)
	 	{
	     	number+= 1;
		}
		else if(number <10 && acecnt ==1)
		{
		  number+= 11;
		}
	   }
	   if(number>21)
		  cout<<"\n bursted";
	   else
		cout<<"\nScore: "<< number<<endl;
		cout<<"If you want to repeat calculations 
for different variables then press 'Y' or 'y':\n";
		cin>>choice;
	}while(choice =='y' || choice =='Y');
}

Output:

 

Enter number of cards(2-5):3
Enter number of card: 2
Enter number of card: 3
Enter number of card: 4

Score: 9
If you want to repeat calculations for different variables then press 'Y' or 'y': n
0 0

Discussions

Post the discussion to improve the above solution.