Black Jack scoring program

I am trying to figure out how to handle the input for a program that will score a black jack hand.
I am pretty lost here, having never programmed before.

The book I have suggests using a char for input.

Should I use a "for" statement with a "switch" nested inside of it, and then case identifiers for 2 thru 10 and J, Q, K, A?
How do I assign the int values needed for each card?
This is where I really get messed up. How do I keep each card value separate?
Perhaps my basic thinking is wrong and I shouldn't have this in a loop at all?

I am not asking for someone to give me code or write this for me.
I just need a nudge in the right direction.
I am pretty darned confused right now.

EDIT:
I think I might have it figured out.
Here is where I am headed, in case anyone would like to comment.
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
int main()
{
    int number_of_cards(0), deuce(0), three(0),
        four(0), five(0), six(0), seven(0),
        eight(0), nine(0), ten(0), jack(0)
        queen(0), king(0), ace(0)
    char card;
    do
    {
      cout << "How many cards are in your hand?\n"
          << "Enter a number between 2 and 5\n";
      cin >> number_of_cards;
      cout << "When entering card values, enter\n"
           << "them in the following manner:\n"
           << "For 2 thru 10 enter that number.\n"
           << "For face cards and Aces enter the first letter.\n"
           << "For instance, enter J for a jack or A for an ace.\n";
        if ((number_of_cards < 2) || (number_of_cards > 5)
        {
           cout << "Illegal number of cards!\n"; //I have a problem here that needs to be resolved
        }
        for (int count = number_of_cards; count <= 5; count++)
        {
            cout << "Enter a card value.\n"
            cin >> card;
            switch (card)
            {
               case '2':
                    deuce = (deuce + 1);
                    break;
               case '3':
                    three = (three + 3);
                    break;


I still have a feeling I am missing something really basic here...
Last edited on
This code might help you manage the value of each card but your going to have to handle the exception that an ace can be a 1 or 11.

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
#include <cstdlib>
#include <iostream>

#define MAX_CARDS  13

using namespace std;

int getCardValue( string s );

int main(int argc, char *argv[])
{
    string userInput = "";

    cin >> userInput;
    cout << getCardValue(userInput) << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}


int getCardValue( string s )
{
    int value = 0;

    string names[] = {"2","3","4","5","6","7","8",
                      "9","10","J","Q","K","A"};
    int values[] =   {2,3,4,5,6,7,8,9,10,10,10,10,1};
    
    for( int i = 0; i < MAX_CARDS; i++)
    {
         if( s == names[i])
         {
             value = values[i];
         }
    }    
    return value;
}

I got it sorted.
Thanks for the help.
I didn't use any of your code as I am in a beginner C++ class.
We haven't covered that stuff yet.
I got it working well though, on my own, and I sure did learn a lot today.
I have a major headache now, but it was worth it. (grin)
I'm still not sure I skinned the cat in the best way possible.
I'm sure that my instructor will have some comments for me.

My main problem is the fact that the pre-req for this course was waived in my case.
I don't know why they told me I didn't need it, but I am really wishing I had taken it now.

For several reasons, it's too late for that, and I am stuck with this situation.
I'll just keep bulling my way through this stuff!

I'm sure I will be back for some nudges in the right direction.
As I said before, I don't want people to write my code for me.
I just need some guidance at times.
Thats cool, there are many ways to solve one problems some maybe more elegant than others but when you start out I believe getting the program to work and understanding it would be your first concern.
Topic archived. No new replies allowed.