### Blackjack game

Hi there, I need to make a simple blackjack game where you input the cards and the program outputs your total value. The problem is for some reason when I input J Q K I get some funky values. For example if I input J and 4, it ouputs 4. If I input J and J, it outputs 10. Also, when I enter yes, to run the program again, the program seems to add the previous values I inputted along with the values I just inputted. Any help would be gladly appreciated :)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` #include using namespace std; int main() { int total=0; int nAces=0; int value=0; char c, response; do { cout << "Enter cards: " << endl; getchar(); while(( c = getchar()) !='\n') { c=tolower(c); switch(c) { case'2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value= c -'0'; break; case 'j': case 'q': case 'k': value=10; break; case 'a': nAces++; break; case '1': char not=getchar(); value=10; break; } total+=value; } while (nAces>0) { if (total+11 <=21) { total +=11; } else {total++;} nAces--; } if (total > 21) { cout << "BUSTED" << endl; } else { cout << total << endl; } cout << "Enter more cards? (y/n): " << endl; cin >> response; } while (response == 'y' || response == 'Y'); return 0; }``````
While in a traditional blackjack game, you don't input your cards, but instead are dealt them, this exercise will still help calculate card values and can be modified into a full game of blackjack. It appears you set it up so that all cards are worth their face value, aside from higher cards such as J, Q, or K. With aces, you are merely counting them.

After you select to play again, it adds to the previous values because they were not re-initialized, so adding re-initialization to the beginning of your do/while loop solves that problem. With your switch statement set up as is, all values should be 2-10, although I don't see any value applied to aces there. The nAces loop only executes if an ace was one of the cards inputted, and applies 1 or 11 based on the total value (it seems logical). I'm unsure about the purpose of the -'0' on line 20, which subtracts from assigned values. There's probably a better way to set this up which would make it easier to debug. Maybe something like this?
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; int main() { int total=0, nAces=0,value=0; char c,response; do { cout<<"Enter cards: "; cin>>c;//takes input and applies to c c=tolower(c); if (c=='j' || c=='q' || c=='k') value=10; else if (c=='a') nAces++; else value=(char)c; while (nAces>0) { if (total+11<=21) total+=11; else total++; nAces--; } if (total>21) cout<<"BUSTED!\n"; else if (value==10) cout<>response; } while (response=='y' || response=='Y'); return 0; }``````

I can see why you want to use chars in place of strings, but it brings other complications along with it, such as figuring out when to use "(char)" and when to work with the variable alone. That's why this code sample doesn't have the total calculations resolved. Perhaps I need to further educate myself about character/integer conversion to get this to work right, as (char) appears to achieve what is wanted on output, but does not appear to assign values to other variables correctly.
Last edited on
Topic archived. No new replies allowed.