### variable char problem in switch loop

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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` `````` #include 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; }``````
I ask this out of curiosity: do you know how to nest loops and use arrays?
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
Study this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#include 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).
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.