Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Switch Statement Help
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bfh




PostPosted: Sat Jul 31, 2010 9:45 pm   Post subject: Switch Statement Help

First ill outline the problem:

the problem is that the switch statements seem to print everything... not just whats needed...


Some info: using rand function to gen a random card 1-52.
then wanna check the card value in the switch statement and print corrent information about the card.


here sample code:

code:



void func_card_type_dec(int *player_card1, int *player_card2){


        switch (*player_card1){
       
                case 1:
                        printf("First Card Draws An Ace of Clubs");
                case 2:
                        printf("First Card Draws 2 of Clubs");
                case 3:
                        printf("First Card Draws 3of Clubs");
                case 4:
                        printf("First Card Draws 4  of Clubs");
                case 5:
                        printf("First Card Draws 5  of Clubs");
                case 6:
                        printf("First Card Draws 6  of Clubs");
                case 7:
                        printf("First Card Draws 7  of Clubs");
                case 8:
                        printf("First Card Draws 8  of Clubs");
                case 9:
                        printf("First Card Draws 9  of Clubs");
                case 10:
                        printf("First Card Draws 10 of Clubs");
                case 11:
                        printf("First Card Draws Jack  of Clubs");
                case 12:
                        printf("First Card Draws Queen  of Clubs");
                case 13:
                        printf("First Card Draws King of Clubs");
                               
                case 14:
                        printf("First Card Draws An Ace of Diamonds");
                case 15:
                        printf("First Card Draws 1 of Diamonds");
                case 16:
                        printf("First Card Draws 3 of Diamonds");
                case 17:
                        printf("First Card Draws 4 of Diamonds");
                case 18:
                        printf("First Card Draws 5 of Diamonds");
                case 19:
                        printf("First Card Draws 6 of Diamonds");
                case 20:
                        printf("First Card Draws 7 of Diamonds");
                case 21:
                        printf("First Card Draws 8 of Diamonds");
                case 22:
                        printf("First Card Draws 9 of Diamonds");
                case 23:
                        printf("First Card Draws 10 of Diamonds");
                case 24:
                        printf("First Card Draws Jack of Diamonds");
                case 25:
                        printf("First Card Draws Queen of Diamonds");
                case 26:
                        printf("First Card Draws King of Diamonds");
                       
                       
                case 27:
                        printf("First Card Draws An Ace of Hearts");
                case 28:
                        printf("First Card Draws 1 of Hearts");
                case 29:
                        printf("First Card Draws 3 of Hearts");
                case 30:
                        printf("First Card Draws 4 of Hearts");
                case 31:
                        printf("First Card Draws 5 of Hearts");
                case 32:
                        printf("First Card Draws 6 of Hearts");
                case 33:
                        printf("First Card Draws 7 of Hearts");
                case 34:
                        printf("First Card Draws 8 of Hearts");
                case 35:
                        printf("First Card Draws 9 of Hearts");
                case 36:
                        printf("First Card Draws 10 of Hearts");
                case 37:
                        printf("First Card Draws Jack of Hearts");
                case 38:
                        printf("First Card Draws Queen of Hearts");
                case 39:
                        printf("First Card Draws King of Hearts");
       
                case 40:
                        printf("First Card Draws An Ace of Spades");
                case 41:
                        printf("First Card Draws 1 of Spades");
                case 42:
                        printf("First Card Draws 3 of Spades");
                case 43:
                        printf("First Card Draws 4 of Spades");
                case 44:
                        printf("First Card Draws 5 of Spades");
                case 45:
                        printf("First Card Draws 6 of Spades");
                case 46:
                        printf("First Card Draws 7 of Spades");
                case 47:
                        printf("First Card Draws 8 of Spades");
                case 48:
                        printf("First Card Draws 9 of Spades");
                case 49:
                        printf("First Card Draws 10 of Spades");
                case 50:
                        printf("First Card Draws Jack of Spades");
                case 51:
                        printf("First Card Draws Queen of Spades");
                case 52:
                        printf("First Card Draws King of Spades");     
        }
        switch (*player_card2){
       
                case 1:
                        printf("Second Card Draws An Ace of Clubs");
                case 2:
                        printf("Second Card Draws 2 of Clubs");
                case 3:
                        printf("Second Card Draws 3of Clubs");
                case 4:
                        printf("Second Card Draws 4  of Clubs");
                case 5:
                        printf("Second Card Draws 5  of Clubs");
                case 6:
                        printf("Second Card Draws 6  of Clubs");
                case 7:
                        printf("Second Card Draws 7  of Clubs");
                case 8:
                        printf("Second Card Draws 8  of Clubs");
                case 9:
                        printf("Second Card Draws 9  of Clubs");
                case 10:
                        printf("Second Card Draws 10 of Clubs");
                case 11:
                        printf("Second Card Draws Jack  of Clubs");
                case 12:
                        printf("Second Card Draws Queen  of Clubs");
                case 13:
                        printf("Second Card Draws King of Clubs");
                               
                case 14:
                        printf("Second Card Draws An Ace of Diamonds");
                case 15:
                        printf("Second Card Draws 1 of Diamonds");
                case 16:
                        printf("Second Card Draws 3 of Diamonds");
                case 17:
                        printf("Second Card Draws 4 of Diamonds");
                case 18:
                        printf("Second Card Draws 5 of Diamonds");
                case 19:
                        printf("Second Card Draws 6 of Diamonds");
                case 20:
                        printf("Second Card Draws 7 of Diamonds");
                case 21:
                        printf("Second Card Draws 8 of Diamonds");
                case 22:
                        printf("Second Card Draws 9 of Diamonds");
                case 23:
                        printf("Second Card Draws 10 of Diamonds");
                case 24:
                        printf("Second Card Draws Jack of Diamonds");
                case 25:
                        printf("Second Card Draws Queen of Diamonds");
                case 26:
                        printf("Second Card Draws King of Diamonds");
                       
                       
                case 27:
                        printf("Second Card Draws An Ace of Hearts");
                case 28:
                        printf("Second Card Draws 1 of Hearts");
                case 29:
                        printf("Second Card Draws 3 of Hearts");
                case 30:
                        printf("Second Card Draws 4 of Hearts");
                case 31:
                        printf("Second Card Draws 5 of Hearts");
                case 32:
                        printf("Second Card Draws 6 of Hearts");
                case 33:
                        printf("Second Card Draws 7 of Hearts");
                case 34:
                        printf("Second Card Draws 8 of Hearts");
                case 35:
                        printf("Second Card Draws 9 of Hearts");
                case 36:
                        printf("Second Card Draws 10 of Hearts");
                case 37:
                        printf("Second Card Draws Jack of Hearts");
                case 38:
                        printf("Second Card Draws Queen of Hearts");
                case 39:
                        printf("Second Card Draws King of Hearts");
       
                case 40:
                        printf("Second Card Draws An Ace of Spades");
                case 41:
                        printf("Second Card Draws 1 of Spades");
                case 42:
                        printf("Second Card Draws 3 of Spades");
                case 43:
                        printf("Second Card Draws 4 of Spades");
                case 44:
                        printf("Second Card Draws 5 of Spades");
                case 45:
                        printf("Second Card Draws 6 of Spades");
                case 46:
                        printf("Second Card Draws 7 of Spades");
                case 47:
                        printf("Second Card Draws 8 of Spades");
                case 48:
                        printf("Second Card Draws 9 of Spades");
                case 49:
                        printf("Second Card Draws 10 of Spades");
                case 50:
                        printf("Second Card Draws Jack of Spades");
                case 51:
                        printf("Second Card Draws Queen of Spades");
                case 52:
                        printf("Second Card Draws King of Spades");     
        }
        }
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sat Jul 31, 2010 10:11 pm   Post subject: RE:Switch Statement Help

Case statements by default fall through, meaning that it will just continue going line by line executing statements, like so:

c++:

int num = 1;

switch(num)
{
    case 1:
        {
            printf("a"); // this will be printed
        }
    case 2:
        {
            printf("b"); // and this will too
        }
}


in order to tell the computer you want to exit the case statement, use the break keyword:

c++:

int num = 1;

switch(num)
{
    case 1:
        {
            printf("a"); // this will still be printed
        }
        break;
    case 2:
        {
            printf("b"); // and now this will not
        }
        break;
}


Hope that helps.

EDIT: Also, you are using a very brute force approach to this problem, try using integer division and remainders to figure out what the name of the card is.
bfh




PostPosted: Sun Aug 01, 2010 2:00 pm   Post subject: RE:Switch Statement Help

Thanks for help, but what type of code would i use to figure our your division code would look like.

im kinda brain dead atm, so i might figure it out later.

im planing to use arrays in a later version, gonna rewrite code at a later time, for the time being just trying to get this to work then i worry about code size.


thanks testing your code for case statements now
DemonWasp




PostPosted: Sun Aug 01, 2010 3:50 pm   Post subject: RE:Switch Statement Help

This is easier if you start at 0 and go to 51, instead of 1-52. It works out the same, but the code is simpler.

First, notice that the suit is determined by the card value divided by 13 (the number of cards in a suit), rounded down. This gives you a number in the range 0-3, which you could say is one of ["Clubs", "Diamonds", "Hearts", "Spades"]. Let this number be the suit.

Then, notice that the card value is then the value modulus 13 (the remainder after dividing by 13). This gives you a number in the range 0-12, which we could call [ "Ace", "Two", "Three", ... , "King" ]. Let this number be the value.

Then all you need to do to construct your final string is the following:
printf ( "Card drawn is the %s of %s\n", values[value], suits[suit] );

The code for this should take maybe 5-8 lines, total.
bfh




PostPosted: Sun Aug 01, 2010 4:47 pm   Post subject: RE:Switch Statement Help

Ill try playing around with that later, little trouble getting the explaination but i read your idea real quick.

thanks for positive input. ima try this code on later, but it sounds a hell of a lot better that what i wrote.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: