variable char problem in switch loop
Aug 30, 2013 at 6:06pm UTC
I have to write a sort of blackjack program using a switch loop, I have most of it done but it needs to take letters for the face cards upper and lower case which all = 10, and have a condition for the ace being 1 or 11 (which ever helps the user more). The book gave a hint that the cards should be var. type char, but everything i tried doesn't work. So my question is how do I implement this? Do I state it some how in each case or at the start of my program?
Thank you for your help.
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
#include <iostream>
using namespace std;
int main( )
{
char ans, NumberOfCards;
int Sum, CardValue1, CardValue2, CardValue3, CardValue4, CardValue5;
do
{
cout << "Please input the number of cards 2, 3, 4 or 5\n" ;
cin >> NumberOfCards;
switch (NumberOfCards)
{
case '2' :
cout << "First Card" " " ;
cin >> CardValue1;
cout << "Second Card" " " ;
cin >> CardValue2;
Sum = CardValue1 + CardValue2;
cout << "Your Score is" " " << Sum << " \n" ;
break ;
case '3' :
cout << "First Card" " " ;
cin >> CardValue1;
cout << "Second Card" " " ;
cin >> CardValue2;
cout << "Third Card" " " ;
cin >> CardValue3;
Sum = CardValue1 + CardValue2 + CardValue3;
cout << "Your Score is" " " << Sum << " \n" ;
break ;
case '4' :
cout << "First Card" " " ;
cin >> CardValue1;
cout << "Second Card" " " ;
cin >> CardValue2;
cout << "Third Card" " " ;
cin >> CardValue3;
cout << "Forth Card" " " ;
cin >> CardValue4;
Sum = CardValue1 + CardValue2 + CardValue3 + CardValue4;
cout << "Your Score is" " " << Sum << " \n" ;
break ;
case '5' :
cout << "First Card" " " ;
cin >> CardValue1;
cout << "Second Card" " " ;
cin >> CardValue2;
cout << "Third Card" " " ;
cin >> CardValue3;
cout << "Forth Card" " " ;
cin >> CardValue4;
cout << "Fifth Card" " " ;
cin >> CardValue5;
Sum = CardValue1 + CardValue2 + CardValue3 + CardValue4 + CardValue5;
cout << "Your Score is" " " << Sum << " \n" ;
break ;
default :
cout << "Incorrect Input of Cards\n" ;
}
if (Sum > 21)
{
cout << "Busted\n" ;
}
else
{
cout << "Winner\n" ;
}
cout << "would you like to play another hand?\n" ;
cout << "Press y or n\n" ;
cin >> ans;
} while (ans == 'y' or ans == 'Y' );
system("Pause" );
return 0;
}
Aug 30, 2013 at 6:25pm UTC
I ask this out of curiosity: do you know how to nest loops and use arrays?
Aug 30, 2013 at 6:35pm UTC
I have covered nesting but not arrays. Im pretty sure a nested if else in each case is the answer but not sure how to make it happen.
Last edited on Aug 30, 2013 at 7:41pm UTC
Aug 30, 2013 at 7:58pm UTC
Study this:
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 88 89 90 91 92
#include <iostream>
using namespace std;
int convert(char );
int main()
{
char ans;
do
{
cout << "Enter the number of cards: " << endl;
int numberOfCards;
cin >> numberOfCards;
char * cards = new char [numberOfCards];
for (int i = 0; i < numberOfCards; i++)
{
cout << "Enter card " << i << ": " ;
cin >> cards[i];
}
int sum = 0;
for (int i = 0; i < numberOfCards; i++)
{
sum += convert(cards[i]);
}
cout << "Your total is: " << sum << endl;
if (sum > 21)
{
cout << "Busted..." << endl;
}
else
{
cout << "Winner!" << endl;
}
cout << "Would you like to play another hand?" << endl;
cout << "Type y or Y to continue: " ;
cin >> ans;
} while (ans == 'y' || ans == 'Y' );
delete [] cards;
cin.get();
return 0;
}
int convert(char c)
{
switch (c)
{
case '2' :
return 2;
case '3' :
return 3;
case '4' :
return 4;
case '5' :
return 5;
case '6' :
return 6;
case '7' :
return 7;
case '8' :
return 8;
case '9' :
return 9;
case 't' :
case 'T' :
return 10;
case 'j' :
case 'J' :
return 11;
case 'q' :
case 'Q' :
return 12;
case 'k' :
case 'K' :
return 13;
case 'a' :
case 'A' :
return 14;
default :
return 0;
}
}
It uses arrays and loops and lets you input any number of cards you want (because of this).
Aug 30, 2013 at 9:00pm UTC
Thank you for you help, I certainly will study this. It uses some things i haven't covered in my class yet but get the gist. I think the main thing i was missing was the int convert char with individual cases for each input.
Topic archived. No new replies allowed.