Question about assigning values

This is a discussion on Question about assigning values within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to write a card counting program, but I'm a bit confused about something. I'm reading card ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Question Question about assigning values

    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.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    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...
    }
    Last edited by JDGATX; 04-17-2008 at 03:27 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    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;
    }

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I've got a working program that does what you want, so feel free to ask any more questions you have.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by JDGATX View Post
    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.

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    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, "%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;
    		}
    	}
    Read the FAQ for more info. on switch statements.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    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

  8. #8
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Replies: 1
    Last Post: 05-15-2006, 04:55 PM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 02:52 PM
  4. Assigning pictures to hex values
    By firemaker in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2005, 10:43 AM
  5. Replies: 1
    Last Post: 02-03-2005, 02:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21