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
|
// Blackjack values.cpp : main project file.
#include <iostream>
using namespace std;
int main()
{
int cards, i, total=0, aceCount=0; // t, j, q, k, a variables NOT being used yet. Add in when needed
char temp;
cout << "How many cards? (Between 2 and 5):" ;
cin >> cards ;
if (cards < 2 || cards > 5)
{
cout << "Not a valid number of cards.";
}
else
{
for (i=0; i<cards; i++)
{
cout << "Enter value of card " << i+1 << ". (2-9, or 't', 'j', 'q', 'k', 'a'):";
cin >> temp;
switch (temp)
{
case '2':
total +=2;
break;
case '3':
total +=3;
break;
case '4':
total +=4;
break;
case '5':
total +=5;
break;
case '6':
total +=6;
break;
case '7':
total +=7;
break;
case '8':
total +=8;
break;
case '9':
total +=9;
break;
case 't':
case 'j':
case 'q':
case 'k':
total +=10;
break;
case 'a':
total +=11;
aceCount++;
}
}
cout << total<<endl;
if (total <= 21)
{
cout << "Your total is: " << total << endl;
}
else if (aceCount!=0 && total > 21) // Only subtract 10 if total > 21
{
do
{
total-=10;
aceCount--;
} while (aceCount >0 && total > 21); // While still over 21 and an ace that equals 11
// have the ace equal one, and subtract 10 from total
if (total <= 21)
{
cout << "Total is: " << total << endl;
}
}
}
}
|