I would appreciate if anyone could help me with my program that calculates a hand of black jack. I know the way I created this program is probably not the most effective way but the only thing I seem to have problems with is getting the program to add 10 when a user enters King, Queen, or Jack so that is what I need the most help with. I would appreciate any other tips. The only requirements for this program is to use a Switch Case statement. Here is my code. Thank you in advanced for any help.

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
/* Declare variables*/
int card1, card2, card3, hand, sum23, sum13, sum21, value1, value2, value3, 
    cardentered1;
char King, Queen, Jack;

/* Get information from user*/
cout<<"Enter the value of your first card: ";
cin>>cardentered1;
cout<<"Enter the value of your second card: ";
cin>>card2;
cout<<"Enter the value of your third card: ";
cin>>card3;	


/* Display hand total*/
if (cardentered1==King || cardentered1==Queen ||cardentered1==Jack)
   card1=10;
else 
   card1=cardentered1;
   
if (cardentered2==King || cardentered2==Queen ||cardentered2==Jack)
   card2=10;
else 
   card2=cardentered2;
   
if (cardentered3==King || cardentered3==Queen ||cardentered3==Jack)
   card3=10;
else 
   card3=cardentered1;
   
sum23=card2+card3;
sum13=card1+card3;
sum21=card2+card1;

switch (card1)
{
       case 1:
            if (sum23<=10)
               value1=11;
            else
                value1=1;
            break;
       case 2:
            value1=2;
            break;
       case 3:
            value1=3;
            break;
       case 4:
            value1=4;
            break;
       case 5:
            value1=5;
            break;
       case 6:
            value1=6;
            break;
       case 7:
            value1=7;
            break;
       case 8:
            value1=8;
            break;
       case 9:
            value1=9;
            break;
       case 10:
            value1=10;
            break;
}

switch (card2)
{
       case 1:
            if (sum13<=10)
               value2=11;
            else
                value2=1;
            break;
       case 2:
            value2=2;
            break;
       case 3:
            value2=3;
            break;
       case 4:
            value2=4;
            break;
       case 5:
            value2=5;
            break;
       case 6:
            value2=6;
            break;
       case 7:
            value2=7;
            break;
       case 8:
            value2=8;
            break;
       case 9:
            value2=9;
            break;
}

switch (card3)
{
       case 1:
            if (sum21<=10)
               value3=11;
            else
                value3=1;
            break;
       case 2:
            value3=2;
            break;
       case 3:
            value3=3;
            break;
       case 4:
            value3=4;
            break;
       case 5:
            value3=5;
            break;
       case 6:
            value3=6;
            break;
       case 7:
            value3=7;
            break;
       case 8:
            value3=8;
            break;
       case 9:
            value3=9;
            break;
}
    
hand=value1+value2+value3;
if (hand<21)
	cout<<"You only have "<<hand<<" this is not enough"<<endl;
else if (hand>21)
	cout<<"Your hand is "<<hand<<" this is to much"<<endl;
else
    cout<<"You win!!! Your hand equals 21!!!!"<<endl;
	
    system("PAUSE");
    return EXIT_SUCCESS;
}

You could try concentrating on the rather simple task of understanding how to use code tags when you post.
You know, before actually moving onto something tricky like actually writing code.

It's not like the board goes out of the way to hide this information from you; I count at least 5 different places where you have an opportunity to learn, yet you missed all of them.

Is anything we say here just going to wash over you like some pebble on the shoreline?

And put so nicely!!! Anyway I have added code tags and would really appreciate any help on this topic from someone who actually knows something about C++. (And is not quick to criticize someone they don't even know.)

Not using code tags is a major problem. After a while, when one starts to add up the extra hours one spends making sense of unformatted code without code tags, it can get really irritating. Most people eventually won't even look at it if code tags aren't used. Anyway, just use code tags in the future and everything will be fine.

As to your program, I would get rid of the King, Queen, Jack variables. You've declared them as characters, haven't initialized them, and then are using them here:

if (cardentered1==King || cardentered1==Queen ||cardentered1==Jack)
     card1=10;

You're also comparing chars to integers, so you are almost guaranteed to get a bad result. Also, in this line:

cin>>cardentered1;

since cardentered1 is declared as an int, if the user enters 'K' for King, that's going to be a problem since cin is looking for digits. When it bumps into 'K', basically your program has no hope of recovering.

So, decide exactly what you want the user to input (presumably 'K', 'Q', 'J', or a number, but what about Ace?). So if you want the user to enter a character, have cardentered1 be a character. That gives you a problem if the user enters 10, though, since that's two characters, so you may want to read the user's input into a string instead, then use atoi to convert it to an integer if it's not a King, Queen, or Jack (again, what about Ace?).

So decide EXACTLY what is legal input and what isn't. From there, you need to convert that card into an integer. i think reading it in from the user as a string is your best bet since it covers all the bases. Again, I don't see the need for King, Queen, Jack variables.

Regarding the switch statement, that's a pretty trivial switch statement, but I guess if you have to have one, maybe it'll do. It's not a really good application of a switch, though it's syntactically correct.

> and would really appreciate any help on this topic from someone who actually knows something about C++
I only said what everyone else was thinking.

Actually, what most people who know C++ on this forum were thinking was "Oh, another post without code tags, I'll go and do something else for a few hours, and see if a mod has fixed it". It doesn't matter, because all it does is delay responses to your ill-formed post.

Also, if you'd been paying any attention at all, you would have noticed on literally hundreds of posts on this forum, lines like this
Last edited by Tekmaven : 7 Hours Ago at 23:02. Reason: Code tags

Interesting that your other post, whilst it has code tags, is completely devoid of any indentation at all. Which is ironic given that your original in this post DOES have indentation.

But if you want to carry on explaining why you're so damn special that we should help you even though you ignore all the rules, then I'm sure /dev/null will be happy to listen to them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.