• 04-17-2008
DCMann2
Hi everyone,

I'm trying to write a card counting program, but I'm a bit confused about something. I'm reading card data (like ace, queen, 5, etc.) from a text file, and each number card or face card is assigned a value of either 1, 0, or -1. Two through six are given a value of +1, seven through nine are given a value of 0, and ten through ace are given a value of -1. The program is supposed to add up all the cards from the text file and print a value, but I'm confused about how I'm supposed to tell the program what the values of the cards are so it can add them all up. I was thinking about using an if...else statement, but I'm not entirely sure how to make it work. I'll post the code and the text file if necessary.
• 04-17-2008
JDGATX
Do you have any code to show currently?

Here is the general flow, in my mind:
1) fscanf() to read in a card
2) use a switch statement on the value of the card
3) Something like this pseudocode:
Code:

```switch (card_value) {     case '2': case '3': case '4': case '5': case '6':           sum += 1;           break;     // And so on... }```
• 04-17-2008
DCMann2
This is all I have so far. Right now all it does is read from the file and display what the cards are on the screen, but I don't know how to tell the program what the value of each card is so it will add/subtract them.

Code:

```int main(void) {     FILE* spCards;     char charIn;         int result = 0;                 printf("This program will count cards during a game of blackjack.\n");         printf("It reads the cards from a file and prints the final +/- count\n");         printf("for the cards dealt. Here are the values of the cards:\n");         printf("2, 3, 4, 5, 6 = +1\n");         printf("7, 8, 9 = 0\n");         printf("T, J, Q, K, A = -1\n\n");         spCards = fopen("location of my file", "r");                 if (spCards == NULL)                   {                       printf("Error opening cards.txt for reading\n");                       return (100);                   }     printf("For:\n");     while((fscanf(spCards, "%c", &charIn)) == true)         printf("%c", charIn);             printf("\n\nFinal count is: %d\n", result);             fclose(spCards);           system("PAUSE");          return 0; }```
• 04-17-2008
JDGATX
I've got a working program that does what you want, so feel free to ask any more questions you have.
• 04-17-2008
DCMann2
Quote:

Originally Posted by JDGATX
I've got a working program that does what you want, so feel free to ask any more questions you have.

All I need to know is how to tell the program what the value of each card is so it will add it properly. Do I need to declare each card's value at the beginning of the program, or can I assign values to each card after the file is opened for reading? Once that's done, all that's left is to use something like result += (variable), right?

EDIT: If you could print the part of your code where you tell the program what the values are, that would be really helpful.
• 04-17-2008
JDGATX
You're reading the values in from a text file, correct? This means that charIn will hold the value of each card once you read that in. Since it's a char data type, you just have to check to see what the value is. The switch statement that I suggested above accomplishes this check. A switch statement is very similar to a long string of if-else conditionals. Be sure to notice how, for each case, I check to see if the card is equal to '2', '3', '4', etc. Putting the values in single quotes tells the processor we're dealing with characters, not integers.

Code:
Code:

```        while((fscanf(spCards, "&#37;c%*c", &charIn)) == 1)  // Gets rid of the space separating the values in your text file         {                 printf("%c, ", charIn);                 switch (charIn)                 {                 case '2': case '3': case '4': case '5': case '6':                         result += 1;                         break;                 case '7': case '8': case '9':                         break;     // Do nothing                 case 'T': case 'J': case 'Q': case 'K': case 'A':                         result -= 1;                 }         }```
• 04-17-2008
DCMann2
I wrote a switch statement similar to what you provided a while ago, but the program is still giving me the wrong result, and I can't figure out why that's happening. It keeps giving me -1, even though I'm supposed to end up with -4 for the text file I was given.

Here's the code I have so far:

Code:

```#include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main(void) {     FILE* spCards;     char charIn;     int result = 0;                 printf("This program will count cards during a game of blackjack.\n");         printf("It reads the cards from a file and prints the final +/- count\n");         printf("for the cards dealt. Here are the values of the cards:\n");         printf("2, 3, 4, 5, 6 = +1\n");         printf("7, 8, 9 = 0\n");         printf("T, J, Q, K, A = -1\n\n");         spCards = fopen("L:\\CIS 161\\Chapter VII Card Counting\\Card Counting\\Card Counting\\cards.txt", "r");                 if (spCards == NULL)                   {                         printf("Error opening cards.txt for reading\n");                         return (100);                   }     printf("For:\n");     while((fscanf(spCards, "%c", &charIn)) == true)     printf("%c", charIn);     {                 switch (charIn)                 {                 case '2': case '3': case '4': case '5': case '6':                         result += 1;                         break;                 case '7': case '8': case '9':                         break;                    case 'T': case 'J': case 'Q': case 'K': case 'A':                         result -= 1;                 }         }         fclose(spCards);         printf("\n\nThe final count is: %d\n", result);     printf("Thank you, and have a great day.\n\n");       system("PAUSE");          return 0; }```
Here are the cards I was given, if it makes any difference (and yes, they're supposed to be vertical):
A
2
K
9
7
Q
A
A
4
T
• 04-18-2008
JDGATX
You need to eat up the newline character. Look above at my code. You'll see how I added a &#37;*c to eat up the newline.

Also, your brackets and indentation are screwy. Again, look at my code. It works.