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