Hi, I'm having trouble with one last part of finishing my basic level C++ class.
The program requires you to enter your 3 cards so that it can program the total of the cards.
Everything seems to be in order so far, but I do not know how to set the values of the face cards- J, Q, K to 10, and A to 1.

Once I do that, I'm not sure if my line
if (sum<=11||x==1||y==1||z==1)
will work appropriately.

edit::I'm pretty sure that it doesn't work right now

/*CSC215-03
 * Lab 3 Program 2
 *  9/25/08
 * Blackjack program
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>

int main(int argc, char *argv[])

{

int sum(0), x, y, z, J(0), Q(10), K(10), A(1);
cout << "Please enter your first card: ";
cin >>x;
cout << "Please enter your second card: ";
cin >>y;
cout << "Please enter your third card: ";
cin >>z;
sum=x+y+z;
if (sum<=11||x==1||y==1||z==1)
sum==sum+10;
cout << "Your total is "<<sum<<".";
if (sum > 21)
cout << " Bust!";

system ("pause");
return 0;

}

And also, I was wondering what C++ programs you would suggest for a student to use.

We use Codewarrior in our classroom, and the professor provided a link to DevC++, which, so far in my experience, sucks. Any other suggestions would be greatly appreciated!

Would I require something along the lines of the following? I tried only entering the part for x to try it out, and I get a "parse error before character constant" in the first line, so I can't run it.


if 'A' in x:
n = 1
elif 'K' in x:
n = 10
elif 'Q' in x:
n = 10
elif 'K' in x:
n = 10
if 'A' in y:
n = 1
elif 'K' in y:
n = 10
elif 'Q' in y:
n = 10
elif 'K' in y:
n = 10
if 'A' in z:
n = 1
elif 'K' in z:
n = 10
elif 'Q' in z:
n = 10
elif 'K' in z:
n = 10

I've always been partial to table-based solutions, especially when the list of choices gets longer than about five. Excessively long if chains and switches are irritating to work with. For example:

#include <algorithm>
#include <string>

namespace {
  std::string face[] = {
    "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
  };

  int value[] = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10
  };

  std::string get_face ( int key )
  {
    for ( int i = 0; i < sizeof value / sizeof *value; i++ ) {
      if ( value[i] == key )
        return face[i];
    }

    return "";
  }
}

Unfortunately, I think that may be a little bit beyond my class's level. Might be sketchy to have in my program, don't want him to get the impression I know so much beyond the class's lessons, haha. Any possible solution on a more basic level?

Member Avatar for iamthwee

So what you saying ? You can't have an array and loop through it?

In that case just use a switch or if else clause.

Would I require something along the lines of the following? I tried only entering the part for x to try it out, and I get a "parse error before character constant" in the first line, so I can't run it.


if 'A' in x:
n = 1
elif 'K' in x:
n = 10
elif 'Q' in x:
n = 10
elif 'K' in x:
n = 10
if 'A' in y:
n = 1
elif 'K' in y:
n = 10
elif 'Q' in y:
n = 10
elif 'K' in y:
n = 10
if 'A' in z:
n = 1
elif 'K' in z:
n = 10
elif 'Q' in z:
n = 10
elif 'K' in z:
n = 10

I assume you mean "else if", not "elif", I assume you mean == instead of "in", and you are comparing a char to an int. Like this?

if (x == 'K')
     n = 10;
else if (x == 'Q')
     n = 10;
else if (x == 'J')
     n = 10;
else if (x == 'A')
     n = 1;

I imagine that's what you are trying to do, but remember that you defined x, y, z as integers, not characters, so you should take user input into a char variable, not x, y, or z. If you know functions and don't want to use Narue's solution, write this function:

int ReturnCardValue (char card)

Stick the if statements in the function. That way you don't have to do the same code over and over for x, y, and z.

Call it like this:

char xcard;
int x;
cout << "Enter a card: ";
cin >> xcard;
x = ReturnCardValue (xcard);
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.