Like Tree1Likes

if statement with multiple switch statements.

This is a discussion on if statement with multiple switch statements. within the C Programming forums, part of the General Programming Boards category; Hello guys, I'm working on a slot machine program. As a draft in my head I planned to have multiple ...

  1. #1
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    if statement with multiple switch statements.

    Hello guys, I'm working on a slot machine program. As a draft in my head I planned to have multiple switch statements followed by an if statement. The if statement would determine if the switch cases matched and would then yield points to the player.
    This seemed great in my head, but when I started programming I had my doubts. I don't think its possible to have a single if statement checking matches for multiple switch statements. Heres a sample of my code to shed some light on what Im trying to accomplish
    Code:
    srand(time(0));
        wheel1 = 1+ rand() %6;
    
    
         switch(wheel1){
    
            case 1:
                printf("Cherry\n");
             break;
    
            case 2:
                printf("Lemon\n");
             break;
    
            case 3:
                printf("Plum\n");
            break;
    
            case 4:
                printf("7\n");
             break;
    
            case 5:
                printf("Melon\n");
             break;
    
            case 6:
                printf("Bar\n");
            break;}
    
            wheel2 = 1+ rand() %6;
    
            switch(wheel2){
    
    
            case 1:
                printf("Cherry\n");
             break;
    
            case 2:
                printf("Lemon\n");
             break;
    
            case 3:
                printf("Plum\n");
            break;
    
            case 4:
                printf("7\n");
             break;
    
            case 5:
                printf("Melon\n");
             break;
    
            case 6:
                printf("Bar\n");
            break;}
    
            wheel3 = 1+ rand() %6;
    
            switch(wheel3){
    
    
            case 1:
                printf("Cherry\n");
             break;
    
            case 2:
                printf("Lemon\n");
             break;
    
            case 3:
                printf("Plum\n");
            break;
    
            case 4:
                printf("7\n");
             break;
    
            case 5:
                printf("Melon\n");
             break;
    
            case 6:
                printf("Bar\n");
            break;}
    Now what Im wanting to do is to check the cases to see if they match, if thats possible at all. For example when the player initiates the program and gets 7,7,Bar it would yield a certain number of points. As a beginner in C, and programming in general, Im pretty confident I can complete the rest of this program entirely on my own. I just cant, for the life of me, figure out how to check for matches within the cases and act on that.
    This is my first time posting here, any sort of help would be appreciated.
    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Try this:


    Code:
    void print_cases (int val) 
    {
       switch(val) {
           case 1:
               printf("Cherry\n");
               break;
    
    
           case 2:
               printf("Lemon\n");
               break;
    
    
           case 3:
               printf("Plum\n");
               break;
    
    
           case 4:
               printf("7\n");
               break;
    
    
           case 5:
               printf("Melon\n");
               break;
    
    
           case 6:
               printf("Bar\n");
               break;
        }
    }


    And call that function every time you want to print something.

  3. #3
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    Name:  slot.jpg
Views: 304
Size:  13.0 KB
    Im not having a problem getting the cases to print.
    My main problem is finding away to see if they match, in order to yield they player points if its a winning roll

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by blu View Post
    Im not having a problem getting the cases to print.
    My main problem is finding away to see if they match, in order to yield they player points if its a winning roll
    Code:
    if( (a == b) == (b == c) ) {
       // equal
    }
    I agree that using a function for your switch case is a good idea, too keep things cleaner and avoid repeating yourself. You could even skip the printing part and just return string literals at each case, then use it with printf.

    Edit: updated.
    Last edited by Subsonics; 02-13-2012 at 09:49 PM. Reason: Eh, that wont work :)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I think Subsonics meant:
    Code:
    if( (a == b) && (b == c) ) {
       // equal
    }
    Subsonics likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Code:
    const char fruit[6][8] = { "Cherry","Lemon","Plum","7","Melon","Bar" };
    srand(time(0));
    int wheel1 = rand() % 6;
    int wheel2 = rand() % 6;
    int wheel3 = rand() % 6;
    
    printf("%s\n%s\n%s\n", fruit[wheel1], fruit[wheel2], fruit[wheel3]);
    
    if (wheel1 == wheel2  &&  wheel1 == wheel3)
        puts("We have a winner!");

  7. #7
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    Thanks Ken, looks like my idea of using switch statements wasnt a very good one.
    But im curious, what does the 6 and 8 in the brackets mean after the fruit char?
    Last edited by blu; 02-14-2012 at 08:04 AM.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    It is setting up a 2-Dimensional Array.

    In this case, an array of 6 elements, where each element has up to 8 charters.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if you have more complicated cases than all 3 being equal (two cherrys + something else) one way is to set up a 3 dimensional array where each index is one of the wheel values, then initialize it to the point values you want for each case. you will have a lot of cases (6 * 6 * 6) and most of them are 0. but this is very time efficient. another way is to have a list (array or other structure) that contains just the winning combinations (maybe a struct with the 3 values + a point value) and then search the list for an entry that matches. this is more space efficient but less time efficient. but on a desktop with less than 216 winning combinations the speed issue would not be noticeable. the big advantage of the list is that it is easier to manage and change as opposed to a 3d array.

  10. #10
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    Ok guys, Ive made some progress since my last post. Ive got the actual slot machine functionality operational. Heres my code:
    Code:
    int main()
    {
        
        int bet,total;
        printf("Enter the amount to bet:\n");
        scanf("%d", bet);
        total = total - bet;
        printf("%d", &total);
    
    
        const char fruit1[20][8] = { "C","C","C","C","C","L","L","L","L","P","P","P","P","P","P","7","M","M","="};
        const char fruit2[20][8] = {  "Cherry","Cherry","Cherry","Cherry","Cherry","Cherry","Cherry","Lemon","Plum","7","7","7","7","7","7","7","7","7","7","Melon","Bars"};
        const char fruit3[20][8] = {  "Cherry","Cherry","Cherry","Lemon","Lemon","Lemon","Lemon","Lemon","Lemon","Lemon","Lemon","Plum","Plum","Plum","Plum","7","Melon","Bars"};
        srand(time(0));
        int wheel1 = rand() % 12;
        int wheel2 = rand() % 12;
        int wheel3 = rand() % 12;
    
        printf("%s\n%s\n%s\n\n", fruit1[wheel1], fruit2[wheel2], fruit3[wheel3]);
    
    
        if ((wheel1 == wheel2 || wheel3) ||  (wheel2 == wheel3 || wheel1))
        printf("Win");
      
        
    
    };
    I intend on abbreviating the rest of the symbols in that grotesquely huge printf functions. Is there any way to abbreviate those somehow?
    Now to my big problem. I implemented my subtraction of the starting number of points. It works properly on its own. However, after the scanf function, it crashes. Each half of the program (the betting half, and the wheels run properly on their own). Any help on getting this thing running? I'd really appreciate it.
    Thanks

  11. #11
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    The second argument to scanf() should be a pointer, &bet (this could cause the crash).

    Variable total is not initialized, so you're subtracting bet from a random number.

    The second argument to printf() should not be a pointer, total (not &total).

  12. #12
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    Hey guys, back again. I've pretty much finished my slot machine program.
    Here it is.
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    srand(time(0));
    char whl1, whl2, whl3, q;
    int bet, total = 100;
    while (total <= 0 || q == 'n')
    printf ("Enter the amount you want to bet\n");
    scanf("%d", &bet);
    total = total - bet;
    printf("You have %d\n", total);
    
    
    
    {
    whl1 = 0+ rand() %21;
    
    if ( whl1 <=5)
    printf("C\n");
    whl1='C';
    
    else if ( whl1 == 6)
    printf("L\n");
    whl1='L';
    
    else if ( whl1 <= 10)
    printf("L\n");
    whl1='L';
    
    else if ( whl1 == 11)
    printf("P\n");
    whl1='P';
    
    else if ( whl1 <= 17)
    printf("P\n");
    whl1='P';
    
    else if ( whl1 == 22)
    printf("7\n");
    whl1='7';
    
    else if ( whl1 == 18)
    printf("M\n");
    whl1='M';
    
    else if ( whl1 == 29)
    printf("M\n");
    whl1='M';
    
    else if ( whl1 == 20)
    printf("=\n");
    whl1='=';
    
    else if ( whl1 == 21)
    printf("=\n");
    whl1='=';
    };
    
    {
    whl2 = 0+ rand() %21;
    
    if ( whl2 <=7 )
    printf("C\n");
    whl1='C';
    
    else if ( whl2 == 8)
    printf("L\n");
    whl1='L';
    
    else if ( whl2 == 9)
    printf("P\n");
    whl1='P';
    
    else if ( whl2 == 10)
    printf("7\n");
    whl1='7';
    
    else if ( whl2 <= 19)
    printf("7\n");
    whl1='7';
    
    else if ( whl2 == 20)
    printf("M\n");
    whl1='M';
    
    else if ( whl2 == 21)
    printf("M\n");
    whl1='M';
    
    else if ( whl2 == 22)
    printf("=\n");
    whl1='=';
    
    };
    
    
    
    {
    whl3 = 0+ rand() %21;
    
    if ( whl3 <=3 )
    printf("C\n");
    whl1='C';
    
    else if ( whl3 == 4)
    printf("L\n");
    whl1='L';
    
    else if ( whl3 <= 14)
    printf("L\n");
    whl1='L';
    
    else if ( whl3 == 15)
    printf("P\n");
    whl1='P';
    
    else if ( whl3 <= 19)
    printf("P\n");
    whl1='P';
    
    else if ( whl3 == 20)
    printf("7\n");
    whl1='7';
    
    else if ( whl3 == 21)
    printf("M\n");
    whl1='M';
    
    else if ( whl3 == 22)
    printf("=\n");
    
    };
    
    {
    if (whl1 == 'C' && whl2 != 'C' && whl3 != 'C')
    total = bet * 2;
    };
    
    
    
    
    
    return 0;
    };
    Its a pretty hefty piece of work (or whatever you want to call it). I still have one concern. After the payout if statements have ran (the bottom if statements with the arithmetic), how would I make the program repeat itself as the user desires? Any further help on this? I have no idea on how to go about this in regards to the program going again, while retaining the players total money.
    Any further feedback will be much appreciated. You guys have already helped me a ton.
    Thanks.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To get your program to loop, you need to put an opening brace after the while statement and a closing brace just before the return 0. Also, you need to change the condition to total > 0. As for the q=='n' part, I suggest getting rid of it and letting a zero bet signal that the user wants to quit. So the condition would be
    Code:
    while (total > 0)
    And you'd have to put this after your scanf:
    Code:
    if (bet <= 0)
        break;
    There are some mistakes in your code. In the repeated if/else sections, you only assign to whl1, even when you're dealing with whl2 or whl3. Note that you could get rid of the repetition by using a function.

    In computing the random numbers, you probably want it to be
    Code:
    whl1 = 1 + rand() % 21;
    if you want numbers between 1 and 21, inclusive. (But notice in your if/else blocks you're also comparing to 22, which will never occur.)

    In computing the new total, you want to add in the winnings, not just set total to the winnings. So
    Code:
    total += bet * 2;
    Your payoff condition looks a little odd, that wheel 1 is C and wheel two is NOT C and wheel three is NOT C. At any rate, you probably want more possible payoffs than just one.

    You should get rid of all the extraneous braces, and you aren't supposed to have a semicolon after a closing brace (in the case of code blocks).

    Now that I look at it again, you're actually missing a bunch of braces in the if/else blocks. Remember that an if statement can only control one statement. If you want to have two or more statements controlled by an if you need to use braces. But in this case, instead of putting in a bunch of braces, I'd suggest removing all the printf("X\n") statements in the if/else blocks so that each if only controls one statement. Then add a single print statement at the end of each if/else block that prints the current value of the respective wheel, something like:
    Code:
    printf("%c\n", whl1); // for wheel 1
    Last edited by oogabooga; 02-16-2012 at 07:13 PM.

  14. #14
    blu
    blu is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    Thanks for the input. However, when i followed the instructions for looping the program it did not loop. I put the opening bracket just after the while loop and the closing bracket just before the return statement.
    Code:
        #include <stdio.h>
    
        int main()
    
    
    
        {
    
    
    
            srand(time(0));
            char whl1, whl2, whl3, q;
            whl1 = 1+ rand() %22;
            int bet, total = 100;
            printf ("Enter the amount you want to bet\n");
            scanf("%d", &bet);
            total = total - bet;
    
            printf("You have %d\n", total);
    
    
    
           while (total <= 0 || q == 'n');
    {
    
    
    
    
    
            //wheel1
    
        whl1 = 0+ rand() %21;
    
    
        if ( whl1 <= 5)
        {printf("C\n");
        whl1='C';
        }
    
    
    
        else if ( whl1 == 6 || whl1 <= 10 )
        {printf("L\n");
        whl1='L';
        }
    
    
    
        else if ( whl1 == 11 || whl1 <= 17 )
        {printf("P\n");
        whl1='P';
        }
    
    
        else if ( whl1 == 18)
        {printf("7\n");
        whl1='7';
        }
    
    
        else if ( whl1 == 19 || whl1 == 20)
        {printf("M\n");
        whl1='M';
        }
    
    
        else if ( whl1 == 21)
        {printf("=\n");
        whl1='=';
        };
    
            //wheel2
    
        whl2 = 0+ rand() %21;
    
    
        if ( whl2 <=7 )
        {printf("C\n");
        whl2='C';
        }
    
    
        else if ( whl2 == 8)
        {printf("L\n");
        whl2='L';
        }
    
    
        else if ( whl2 == 9)
        {printf("P\n");
        whl2='P';
        }
    
    
        else if ( whl2 == 10 || whl2 <= 19)
        {printf("7\n");
        whl2='7';
        }
    
    
        else if ( whl2 == 20)
        {printf("M\n");
        whl2='M';
        }
    
    
        else if ( whl2 == 21)
        {printf("=\n");
        whl2='=';
        };
    
    
            //wheel3
    
        whl3 = 0+ rand() %22;
    
    
        if ( whl3 <=3 )
        {printf("C\n");
        whl3='C';
        }
    
    
        else if ( whl3 == 4 || whl3 <= 14)
        {printf("L\n");
        whl3='L';
        }
    
    
        else if ( whl3 == 15 || whl3 <= 19)
        {printf("P\n");
        whl3='P';
        }
    
    
        else if ( whl3 == 20)
        {printf("7\n");
        whl3='7';
        }
    
    
        else if ( whl3 == 21)
        {printf("M\n");
        whl3='M';
        }
    
    
        else if ( whl3 == 22)
        {printf("=\n");
        whl3='=';
        };
    
            //payout
        {
        if (whl1 == 'C' && whl2 != 'C' && whl3 != 'C')
        total += bet * 2;
    
        };
    
        {
        if (whl1 == 'C' && whl2 == 'C' && whl3 != 'C')
        total += bet * 3;
    
        };
    
        {
        if (whl1 == 'C' && whl2 == 'C' && whl3 == 'C')
        total += bet * 8;
    
        };
    
        {
        if (whl1 == 'L' && whl2 == 'L' && whl3 == 'L')
        total += bet * 10;
    
        };
    
        {
        if (whl1 == 'L' && whl2 == 'L' && whl3 == '=')
        total += bet * 10;
    
        };
    
        {
        if (whl1 == 'P' && whl2 == 'P' && whl3 == 'P')
        total += bet * 14;
    
        };
    
        {
        if (whl1 == 'P' && whl2 == 'P' && whl3 == '=')
        total += bet * 14;
    
        };
    
        {
        if (whl1 == 'M' && whl2 == 'M' && whl3 == 'M')
        total += bet * 100;
    
        };
    
        {
        if (whl1 == 'M' && whl2 == 'M' && whl3 == '=')
        total += bet * 100;
    
        };
    
        {
        if (whl1 == '=' && whl2 == '=' && whl3 == '=')
        total += bet * 200;
        printf("New total: %d", total);
    
        };
      return 0;
     };
    
    
    };
    I appreciate the further input on helping me improve and clean up the code, but i am not confident enough to alter it. It already works exactly the way Im wanting it to and I am afraid I wont be able to bring it back to operation order.

    -Blu
    Last edited by blu; 02-16-2012 at 08:38 PM.

  15. #15
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Code:
           while (total <= 0 || q == 'n');
    That trailing semicolon counts as the whole loop. Delete it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with multiple switch statements and arrays
    By dangalong801 in forum C Programming
    Replies: 7
    Last Post: 05-23-2011, 01:58 AM
  2. switch statements
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 02:26 AM
  3. If, Else and switch statements
    By Soul.India in forum C Programming
    Replies: 4
    Last Post: 11-10-2003, 09:01 AM
  4. Switch Statements.
    By RealityFusion in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2003, 11:55 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM

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