In: Computer Science
Write a C++ 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. Write a C++ 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. Write a C++ 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. Write a C++ 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.
#include<iostream>
using namespace std;
int main()
{
   int noOfCard;
   int score;
   int count;
   char choice='Y';
   char input;
   while(choice =='y' || choice =='Y') // if user enter
yes
   {
   score = 0; // to retract sum everytime user wants to
play again
   count =0; // retract count
cout<<"Input must be between 2 and 5: ";
   cin>>noOfCard; // input
   if(noOfCard<2 || noOfCard >5)
cout<<"Wrong Input"<<endl; // wrong input
   else
   {
       for(int i = noOfCard; i>0; i--)
// get input cards till count of cards no enter
       {
       cout<<"Input card number:
";
       cin>>input;
       if(input>='2' &&
input<='9') // calculate score according to it
       {
          score = score + 2 +
(int)(input- '2');
       }
       else if (input =='j' || input
=='J'||
input =='t'|| input =='T'
          
    || input =='q'|| input =='Q'||
input =='k'|| input =='K')
       {
       score=score+10;
       }
       else if (input =='a'|| input ==
'A')
       {
       count++;
       }
      }
   }
if(count)
   {
       while(count>1)
       {
       score= score+1;
       count--;
       }
       if(score >10 && count ==
1)
      {
      score= score+1;
       }
       else if(score <10 &&
count ==1)
       {
       score= score+11;
       }
   }
   if(score>21) // if score is greater than 21 means
busted
{
      
cout<<"Busted"<<endl;
}
   else // print score then
       {
       cout<<endl<<"Score:
"<< score<<endl;
       }
       cout<<"Press 'Y' OR ' y':
"<<endl;
       cin.ignore();
       cin>>choice;
   }
   cout<<"Thank You for playing"<<endl;
}
OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP