Need Help with Black Jack Program

As indicated above, I am in a "roadblock" if i dare say so.
I have compiled a program that scores a blackjack hand.

Here's how the question goes:

In blackjack, a player receives from two to five cards. (The player decides how many, but
that has no effect on this exercise.) 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 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.

You read in the number of cards (ranging from 2 to 5) and 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 ten, 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 uppercase 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.

Use lots of functions. 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.

Use the following as your sample input. You should input the values from a file and
output the results to a file. Make sure you perform error checks on all your input data.
The number of cards should range from 2 to 5 only.


Sample Input (the number above the cards (2 to Ace) indicate the number of cards in hand)

2
j j
3
q k j
4
2 5 t 2
5
5 5 5 2 2
4
5 3 k q
3
a a a
2
a a
4
a a 8 2
3
a k q
2
a t
2
a q
3
f t 3
5
g 3 5 2 3
2
1 k
3
j k q

Sample output should be like this:

20

busted

19

19

busted

13

12

12

21

21

21

wrong input data 

wrong input data 

wrong input data 

busted


This is my code. I used the sample input above, HOWEVER i ended up with a different result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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: \n"; 
    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'); 
    if(card == 't' || card == 'j' || card == 'q' || card == 'k') return 10; 
    if(card == 'T' || card == 'J' || card == 'Q' || card == 'K') return 10; 
    else return 1;  
} 
  
//given the cards computes score 
int computeValue(char cards[], int *n) 
{ 
    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 == 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\n"; 
        return 1; 
    } 
      
    //opening output file 
    ofstream fout; 
    fout.open("output.txt"); 
    if(!fout) 
    { 
        cout<<"Cannot open output.txt\n"; 
        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 
    while(1) 
    { 
        getInput(cards, &n, fin); 
        score = computeValue(cards,&n); 
        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 yes stop asking to continue again
            if(opt == 'y' || opt == 'Y') break; 
            //if no exit the program
            else if(opt == 'n' || opt == 'N') return 0;  
        } 
    } 
     fin.close();
     fout.close();
     return 0; 
} 


My Output after i compiled the program (so long as the score is the same it is correct):

Score is 20

Busted

Score is 19

Score is 19

Busted

Score is 13

Score is 12

Score is 12

Score is 21

Score is 21

Score is 21

Score is 14

Score is 14

Score is 21

Busted



I have not a clue how to deal with invalid car types :S

Sorry for any inconvenience
Last edited on
Topic archived. No new replies allowed.