wierd blackjack

ok so i have to make a blackjack game or "21" but the twist is that they get to pick how many cards they have and what the cards are (stupid i know but its what the spec says so i have to do it)
anyway i have it all set out so it works but they have the option to pick J Q K A but i dont know how to change i if they pick that to a number... can someone please help, thank you in advance :) x
You're either gonna have to have the user use 11, 12, 13 for J, Q, K (use 1 for A). have to use strings and stringstream, or get very weird code.
So:

1
2
3
4
5
int var=1;
while(var>0 && var<14){
  cout<<"Please enter 1 for A, 2 through 10, 11 for J, 12 for Q, or 13 for K:  ";
  cin>>var
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <sstream>
.....
string input;
int var;
input[1]='s';
while(input!="A" && input=="2" && /*     etc     etc   */   && input!="10" && input!="J" && input!="Q" && input!="K"      ){
  cout<<"Input? ";
  getline(sin,input);
}
if(input=="A")
  var=1;
else if(input=="J")
  var=11;
else if(input=="Q")
  var=12;
else if(input=="K")
  var=13;
else
  stringstream(input)>>var;


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int var;
char input[2]={'/n','/n'};
while(!((input[0]=='A' || input[0]=='2' || input=='3' || /* etc etc */ || input[0]==9 || input[0]=='J' || input[0]=='Q' || input[0]=='K' || 
input[0]=='1') && input[1]=='/0') && (!(input(input[0]=='1' || input[1]=='0')){
// Sooo weird. Can't Guarantee this is correct.
  cout<<"Input?  ";
  cin>>input;}
if(input[0]=='A')
  var=1;
else if(input[0]=='2')
  var=2;
//  etc
//  etc
else if(input[0]=='9')
  var=9;
else if(input[0]=='1' && input[1]=='0')
  var=10;
else if(input[0]=='J')
  var=11;
else if(input[0]=='Q')
  var=12;
else if(input[0]=='K')
  var=13;
Last edited on
thank you for your help, i think im gonna attempt option two beucase i think they want us to accept letters, the only question i have is that will string carry a number that i can add up? because otherwise the date entry that they enter might corrupt the program.
if that didnt make sense i mean if i have

int card

cin >> card

and they put A

can i still do all the if statements? or will it just error?
A string is essentially a character array (with quite a few bells and whistles, but still). Thus, it can carry letters and numbers.
BUT you can't just use a string in a numerical operation (for example you can't add or substract 2 strings, even if they only contain numbers). You need to stringstream the string into an integer (or a float or double, I suppose) if you want to do that.
I just spoke to someone who is doing the same thing but still hasnt finished his, but he is using a switch., can i use a switch to make it add up?
can i use the switch with char?

for example

switch (answer[x])

case 1:
total += 1
case 2
total += 2
case "a":
total +=11
case "k":
total += 10


I suppose it could work, but not quite like you wrote it.
The cases should be in colons even for the numbers ( like so case '1': ), and every case should and with a break command (break;).
The problem is what to do with the 10. Switch can't check two values as far as I know.

BTW, there is no case '1' if you use 'A'; it's either one or the other. And K has a value of 13, not 10.
ohhhh ok lol that makes sense now :) thank you for all your help by the way! im a bit new to this but im trying :)

king normally does equal 13 but in a game of blackjack its 10 :)

Topic archived. No new replies allowed.