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

all i request is simple: help me solve my \"Roadblock\". the roadblock in my cod

ID: 3552192 • Letter: A

Question

all i request is simple: help me solve my "Roadblock".

the roadblock in my code is that i'm supposed to read in the number of cards (ranging from 2 to 5) and the card values into my blackjack program, and output the card values;

If my card values are <= 21, the program outputs the value of the cards to a result file called "output.txt"
If my cards are of invalid type, the program outputs "Invalid card type"
If my card values are >21, the program outputs "busted" to the result file.

Here's the full explanation:

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

Explanation / Answer

#include <iostream>

#include<fstream>

#include<string.h>

using namespace std;

//*n is the value of no:of cards(N)

//takes input from user

void getInput(char cards[], int *n, ifstream &fin)

{   

fin >> *n;

cout<<"Reading cards from file: ";

for(int i=0;i<*n;i++) fin >> cards[i];

for(int i=0;i<*n;i++) cout << cards[i]<<" ";

cout<<endl;   

}

  

//given a card computes its value

int getValueofCard(char card)

{

if(card >='2' && card <= '9') return (card - '0');

else if(card == 't' || card == 'j' || card == 'q' || card == 'k') return 10;

else if(card == 'T' || card == 'J' || card == 'Q' || card == 'K') return 10;

else if(card =='a'||card=='A')return 1;

else return 0;   

}

  

//given the cards computes score

int computeValue(char cards[], int *n,int *flag)

{

int value; //value of a card

int sum = 0;//total

int aceCount = 0; //counter for aces

for(int i=0;i<*n;i++)

{

value = getValueofCard(cards[i]);

if(value == 0)

{

*flag=1;

return 0;

}

if(value == 1) aceCount++;

sum += value; //adding all cards with aces as ones

}

while(aceCount && sum + 10 <= 21) //if you have any aces and using ace as 11 doesnt exceed 21

{

sum += 10;

aceCount--;

}

return sum;

}


int main()

{

//opening input file

ifstream fin("input.txt");

if(!fin)

{

cout<<"Cannot open input.txt ";

return 1;

}

  

//opening output file

ofstream fout;

fout.open("output.txt");

if(!fout)

{

cout<<"Cannot open output.txt ";

return 1;

}

  

char cards[5];//array to store cards

int n; //number of cards

int score; //score of the hand

char opt; //option chose by user

int flag;

while(1)

{

  

flag=0;

getInput(cards, &n, fin);

score = computeValue(cards,&n,&flag);

if(flag ==1)

{

fout<<"invalid card type"<<endl<<endl;

}

else

{

if(score > 21) fout << "Busted"<<endl<<endl;

else fout<<"Score is "<<score<<endl<<endl;

}

  

while(1)

{

cout<<"Do you want to continue?(y/n) ";

cin >>opt;

if(opt == 'y' || opt == 'Y') break; //if yes stop asking to continue again

else if(opt == 'n' || opt == 'N') return 0; //if no exit the program

}

}

fin.close();

fout.close();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote