Thread: Conditionals

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Conditionals

    I am having a problem with the end of my program. This program is designed to pick 100 playing cards at random, and print out the end results, for example, how many jacks, queens, etc. The problem i am having is trying to print out the results color of the cards (for example red = diamonds and hearts, black = clubs, spades). What it is suppose to print out is '"You were dealt _ amount of red cards and _ amount of black cards", but it shows the sentence twice, and the numbers do not add up to 100, the number of andom picks (p.s. I tried to include the indents so it can be easier to read, but i was having a hard time trying to format this email):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    
    {
    	int cards, numberOnCard, cardRank[14] = {0}, cardSuit[5] = {0}, kindOfCard,
    		totalBlack = 0, totalRed = 0;
    	
    	
    
    
    	srand(time(NULL));
    
    	/* Picking 100 cards at random */
    
    	for (cards=1; cards<=100; cards++)
    	{
    		numberOnCard = rand() % 13 +1;
    		kindOfCard = rand() % 4 + 1;
    
    		cardRank[numberOnCard]++;
    		cardSuit[kindOfCard]++;
    	   
    		
    		/* "numberOnCard" is the number or face on the card */
    		
    		switch (numberOnCard)
    		{
    			
    			case 1:
    					printf("Card # %d = Ace ", cards);
    					break;
    			
    			case 2: case 3: case 4: case 5: case 6: case 7: case 8:
    			
    			case 9: case 10: 
    
    					printf("Card # %d = %d ", cards, numberOnCard);
    					break;
    					
    			case 11:
    					printf("Card # %d = Jack ", cards);
    					break;
    		
    			case 12:
    					printf("Card # %d = Queen ", cards);
    					break;
    		
    			case 13:
    					printf("Card # %d = King ", cards);
    					break;
    
    			
    			
    		}
    
    		
    		/* "kindOfCard" is the face/suit of the card */
    
    		switch (kindOfCard)
    		{
    			case 1:
    					printf("of Spades\n");
    					break;
    					
    			case 2:
    					printf("of Clubs\n");
    					break;
    
    			case 3:
    					printf("of Hearts\n");
    					break;
    
    			case 4:
    					printf("of Diamonds\n");
    					break;
    			
    		}
    	/* end for random pick */
    	}
    
    
    	
    	/* Printing out how many of each rank was chosen */
    
    	for (numberOnCard=1; numberOnCard<=14; numberOnCard++)
    
    	{	
    
    		switch (numberOnCard)
    		{
    			
    
    			case 1:
    					printf("\nYou were dealt an Ace card %d times.\n", 
    							cardRank[numberOnCard]);
    					break;
    			
    			case 2: case 3: case 4: case 5: case 6: case 7: case 8:
    			
    			case 9: case 10: 
    
    					printf("You were dealt a %d card %d times.\n", 
    							numberOnCard, cardRank[numberOnCard]);
    					break;
    					
    			case 11:
    					printf("You were dealt a Jack %d times.\n", 
    							cardRank[numberOnCard]);
    					break;
    		
    			case 12:
    					printf("You were dealt a Queen %d times.\n", 
    							cardRank[numberOnCard]);
    					break;
    		
    			
    			case 13:
    					printf("You were dealt a King %d times.\n", 
    							cardRank[numberOnCard]);
    					break;
    
    
    			
    
    			
    					
    		}
    	
    	
    	
    	}
    
    	for (kindOfCard=1; kindOfCard<=4; kindOfCard++)
    
    	{
    		
    		if (kindOfCard == 1 || kindOfCard == 2)
    		{
    				totalBlack = cardSuit[kindOfCard] + cardSuit[kindOfCard];
    				printf("You were dealt %d black cards ", totalBlack);
    				
    		}
    		else if (kindOfCard == 3 || kindOfCard == 4)
    		{
    				totalRed = cardSuit[kindOfCard] + cardSuit[kindOfCard];
    				printf("and %d red cards.\n ", totalRed);
    						
    		}
    	
    	
    	
    	}
    
    
    
    
    	return 0;
    
    }
    Last edited by uniqueniq; 02-12-2003 at 04:31 PM.

  2. #2
    xmdvp
    Guest
    I am sorry, I tried to read your code, but it's been very difficult. so I remade a cleaner working version of your program

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "time.h"
    
    enum TYPE {
    	DIAMOND = 0,
    	HEARTS,
    	SPADES,
    	CLUBS
    };
    
    const int MAX_PICKS = 100;
    
    typedef struct {
    	int type;
    	int number;
    } Card;
    
    int main(int argc, char* argv[])
    {
    	/* first, allocate a 52 set of cards
    	 * and allocate the whole deck
    	 */
    
    	int pick[MAX_PICKS];	// will contain the index of the card in the deck
    	Card deck[52];
    	int index = 0;
    
    	int red_cards = 0;
    	int black_cards = 0;
    
    	for ( int type = 0; type < 4; type++ ) {
    		for ( int n = 0; n < 14; n++ ) {
    			deck[index].number = n;
    			deck[index].type = type;
    			index++;
    		}
    	}
    
    	/*
    	 * now, pick randomly hundred cards
    	 */
    
    	/* in order to get different pick everytime we run the program */
    	srand( (unsigned)time( NULL ) );
    
    	for ( int i = 0; i < MAX_PICKS; i++ ) {
    		// get random number between 0 and 51
    		pick[i] = rand() * 52 / RAND_MAX;
    		if ( pick[i] >= 52 ) pick[i] = 51;	// might happen
    
    		// at the same time, count red/black cards
    		if ( deck[pick[i]].type > HEARTS ) {
    			black_cards++;
    		} else {
    			red_cards++;
    		}
    	}
    
    	/* 
    	 * ok, we're done picking the 100 cards
    	 * now, lets print all the results
    	 */
    
    	printf("You've been delt %d red cards amd %d black cards\n", red_cards, black_cards);
    	
    	return 0;
    }
    Hope that's what you're looking for

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Uniqueniq, please read this thread.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    because everyone knows there are only 52 playin card, D'UH lol just kiddin

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    ha ha very funny, but really, it is 52 cards, but you are picking these cards 100 times at random. And the code that someone printed out before did not work on my compiler (i use microsoft visual c++ 6.0) and it was very complicated (i'm not that much of a c expert). But thank you for taking the time to rewrite it. Its the for loop at the end, which prints out the total black and red cards, is what i am REALLY having a problem with - everything else works.

  6. #6
    xmdvp
    Guest
    actually the code I posted before does work under VC++ 6.0 because that's what I used.

    under Visual C++ 6.0, go to File->New ->Project ->Win 32 Console Application -> Empty project

    create a file, cut-paste my code, compile and say yes to include the file in your project and execute it.

    It works.

    BUT I ran your code and looked at it: you make a mistake in your last loop:

    Code:
    for (kindOfCard=1; kindOfCard<=4; kindOfCard++) {
    	if (kindOfCard == 1 || kindOfCard == 2) {
    		totalBlack = cardSuit[kindOfCard] + cardSuit[kindOfCard];
    		printf("You were dealt %d black cards ", totalBlack);
    	}
    	else if (kindOfCard == 3 || kindOfCard == 4) {
    		totalRed = cardSuit[kindOfCard] + cardSuit[kindOfCard];
    		printf("and %d red cards.\n ", totalRed);
    	}
    }
    because kindOfCard is going from 1 to 4. So it's going to enter TWO times in the first 'if' statement and TWO times in the second.

    this is what you should have instead of the loop:

    Code:
    printf("You were dealt %d black cards\n", cardSuit[1] + cardSuit[2]);
    printf("You were dealt %d red cards\n", cardSuit[3] + cardSuit[4]);
    why do complicated when you can do simple ?

    xmdvp

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    OH MY GOD!!!!! Thank you soooo much - that was so simple, and it worked!!!!!!! I have one more question - how would i be able to find and print the card that was picked the most times?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditionals
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 07:29 AM
  2. Type definitions within conditionals?
    By mushu in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:16 PM
  3. How to eliminate irrelevant conditionals?
    By cdave in forum C Programming
    Replies: 9
    Last Post: 12-10-2005, 04:39 PM
  4. circle and conditionals
    By a1pro in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2005, 02:05 AM
  5. Raycasting question
    By VirtualAce in forum Game Programming
    Replies: 10
    Last Post: 12-10-2001, 05:08 PM