# Need help to tweak this beginner program :|

• 10-04-2011
Sephyx
Need help to tweak this beginner program :|
Code:

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{

int cards, num, count;
char value, j, k, q, t, a, J, K, Q, T, A;
int total = 0;

cout<<"How many cards do you have?"<<endl;
cin>>cards;
count=1;

for (count = 1; count <= cards; count++)
{
if (num=='k'||'K'||'q'||'Q'||'j'||'J'||'t'||'T')
{
num=10;
}
else if (num=='a'||'A')
{
num=11;
}
cout <<  "Please enter the card value for card " << count <<": "<< endl;
cin >> num;
total=total+num;
}

if (total <=21 && total >= 2)

{
cout << "Total is "<< total << endl;
}

else
cout<< "Bust"<<endl;

return 0;

}

This is just a blackjack program that counts the cards. Something is wrong with my q, k, j, a, I'm trying to give q k j = 10 and ace = 11 or 1. I got the program correctly with normal numbers, but when I start to use q, k, j and a, it starts to bug out :|

Any help would be appreciated, I'm a beginner and this is an exercise, I'm just a bit stuck at the moment :(

The output should be something like this:

Example 1:
How many cards do you have? 2
Please enter the card values for card 0: a
Please enter the card values for card 1: j

Example 2:
How many cards do you have? 3
Please enter the card values for card 0: j
Please enter the card values for card 1: k
Please enter the card values for card 2: 3
BUSTED

Edit: Took out the extra things on int, I had a lot of inputs when I was using the switch command, but since I just used a loop instead I took out the card1 card2 etc.
• 10-04-2011
bernt
Code:

for (count = 1; count <= cards; count++)
{
if (value=='k'||'K'||'q'||'Q'||'j'||'J'||'t'||'T')
{
num=10;
}
else if (value=='a'||'A')
{
num=11;
}
cout <<  "Please enter the card value for card " << count <<": "<< endl;
cin >> num;
total=total+num;
}

Consider the flow of this loop. You check the variable 'value' to handle face cards and the 10 card. But from whence do you get 'value'? What is 'value' supposed to represent? Where is it being assigned, and how?
• 10-04-2011
valaris
Also note that simply chaining || will not have the effect you are probably expecting. You will need to individually compare value to each character value. In cases where I find myself doing a lot of logically OR'd numeric comparisons like you have here I usually throw it into a switch as:

Code:

switch (value)
{
case 'k':
case 'K':
case 'q':
case 'Q':
// etc...
num = 10;
break;
case 'a':
case 'A':
num = 11;
break;
}

• 10-04-2011
Sephyx
Oh that's right, value is nothing, I edited and replaced it with num, but I still get weird values. When I type in 2 for 'cards' for the loop to repeat twice, I get 10 for k j and q, but after I type in the first card the program ends. When I type in 3 for 'cards' I get even weirder values such as q being 20 and the program suddenly ending, k/j being a bust right away.
• 10-04-2011
Sephyx
I tried to exchange this with the if / else if, but it had the same effect, I'm still getting weird values ;(

Quote:

Originally Posted by valaris
Also note that simply chaining || will not have the effect you are probably expecting. You will need to individually compare value to each character value. In cases where I find myself doing a lot of logically OR'd numeric comparisons like you have here I usually throw it into a switch as:

Code:

switch (value)
{
case 'k':
case 'K':
case 'q':
case 'Q':
// etc...
num = 10;
break;
case 'a':
case 'A':
num = 11;
break;
}

• 10-04-2011
laserlight
What exactly did you try?
• 10-04-2011
valaris
You can't just change value to num in the if statements. "num" doesn't become initialized to anything valid until it gets to the cin >> num; call. You need to get the users input before you check it as a first task. You should also probably post some updated code if you want updated help.
• 10-04-2011
Sephyx
Umm I tried Valaris' suggestion to use the switch/case and exchange it with the if/else if statement that I had to convert j,k,q into 10, but my program is still giving me off values when I try it :|

Updated code:
Code:

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{

int num, count, cards;
char j, J, q, Q, k, K, a, A;
int total = 0;

cout<<"How many cards do you have?"<<endl;
cin>>cards;

count=1;

for (count = 1; count <= cards; count++)
{

cout <<  "Please enter the card value for card " << count <<": "<< endl;
cin >> num;

switch (num) // Put after cin>>num to translate value.
{
case 'k':
case 'K':
case 'q':
case 'Q':
case 'j':
case 'J':
num = 10;

break;

case 'a':
case 'A':
num = 11;

break;
}

total=total+num;
}

if (total <=21 && total >= 2) //Less/Equal to 21 or Greater/Equal to 2

{
cout << "Total is "<< total << endl;
}

else
cout<< "Bust"<<endl;

return 0;

}

• 10-04-2011
valaris
Notice you are expecting a char value into num but you have declared it an int. num (or perhaps some better named variable) should be of type char. Then set an integer value to the numeric value of the card.
• 10-04-2011
Sephyx
Wow, yeah, by putting 'num' into char it fixed a lot of things it works for the most part, only one last thing.

Edit: When I put 'num' into char it was able to fix the k,j,q values, but now it's not taking in the numeric values, how do I put the numeric values into char to add it to the 'case'? Since num cant be both num and char at the same time.

Updated Code:
Code:

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{

int count, cards;
char j, J, q, Q, k, K, a, A, num;
int total = 0;

cout<<"How many cards do you have?"<<endl;
cin>>cards;

count=1;

for (count = 1; count <= cards; count++)
{

cout <<  "Please enter the card value for card " << count <<": "<< endl;
cin >> num;

switch (num) // Put after cin>>num to translate value.
{
case 'k':
case 'K':
case 'q':
case 'Q':
case 'j':
case 'J':
num = 10;

break;

case 'a':
case 'A':

if (total<=10 && total>=2)
num = 11;
else
num = 1;

break;
}

total=total+num;

}

if (total <=21 && total >= 2) //Less/Equal to 21 or Greater/Equal to 2

{
cout << "Total is "<< total << endl;
}

else
cout<< "Bust"<<endl;

return 0;

}

My last problem right now is the value of the Ace, I put

Code:

if (total<=10 && total>=2)
num = 11;
else
num = 1;

Under case a/A, but let's say you have 2 cards, when you type in 'q' then 'a' it is correct since the total would be 10 from the value of 'q' allowing 'a' to be 11 and giving me a 21, but when I type 'a' first it gives me a 1 since the total is 0. Finally almost have it >.< I need to get better at sorting out int/char/double variables ;\