how to 'and' statements

This is a discussion on how to 'and' statements within the C++ Programming forums, part of the General Programming Boards category; how do i insert a and statement into a checking procedure? i am trying to get my fruit machine to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    Question how to 'and' statements

    how do i insert a and statement into a checking procedure? i am trying to get my fruit machine to check to see if the numbers are the same, what do i need to do to ensure this?

    e.g code -
    else if (winningline[1] == 2 & winningline[2] == 2 & winningline[3] == 2)
    {
    cout<<"Winning line - Cherry\n";

    i want it so it looks to see if the value of winningline[1] is equal with the value in winning lines 2 and 3. what do i need to do to get this?

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    it is
    else if (winningline[1] == 2 && winningline[2] == 2 && winningline[3] == 2)

    i.e. you have to use && instead of &


    ot to generalise it use

    else if (winningline[1] == winningline[2] && winningline[2] == winningline[3] )

    this would be more efficient since it will automatically check for all symbols.. not just 2.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    trouble is i am running a random number generator, which generates a number between 1 and 10. each number has a diff fruit symbol more or less, and each combination of winning line of 3 gives a diff win amount.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I don't know the details of your program but I can tell you this:
    If you want to check for some statements if they are true, you and them using operator &&.
    Although in your case you could've written it like this:
    Code:
    if (winningline[1] ==  winningline[2] == winningline[3] == 2)
    ...
    As I understood, when you have same three symbols, you have a winner, but according to that symbol the ammount is different, am I right?!
    If yes:
    for example, in the statement above, store the 2 in a variable and write a switch statement to determine the ammount won buy the player, after you make sure they are all the same .
    none...

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    84
    all i want is for the number generated to print to screen the name of a fruite, which is equal to the value of the number generated.

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I mean you can have something like this:
    Code:
    //the generated random number is stored in x
    if ( winningline[1] ==  winningline[2] == winningline[3] == x )
    {
         switch( x )
         {
              case 1:
                   //print the corresponding symbol
                   //do whatever
                   break;
              //and so on for other cases
         }
    }
    none...

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    84
    trouble is that that only accounts for 10 of the cases.

    i have the value of cherry set to 1,2,3 & 4. so what happens in that case?

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You are generating random numbers between 1 and 10, right?
    Then you only need to handle these cases( 1 - 10 ), because you are not going to have any other value, right?
    and if you want to combine cases you can do the following:
    Code:
    //the generated random number is stored in x
    if ( winningline[1] ==  winningline[2] == winningline[3] == x )
    {
         switch( x )
         {
              case 1:
              case 2:
              case 3:
              case 4:
                   //the response you write here will be applied to 1, 2, 3, and 4
                   //do whatever
                   break;
              //and so on for other cases
         }
    }
    none...

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    84
    how do i get x to store the random number? and i have 3 random numbers to compare. sorry if i'm asking stupid ques, but i just can't seem to see how this bit works.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    if (winningline[1] ==  winningline[2] == winningline[3] == 2)
    Hmm, not sure how will this works:

    (winningline[3] == 2) -> if it is equal, the value of this expression is true, or "1".

    Then, if winningline[2] is 2, then:
    winningline[2] == winningline[3] == 2
    will then be false, or "0", since (winningline[3] == 2) gives you true or "1".

    Then, if winningline[1] is 0, then:
    (winningline[2] == winningline[3] == 2) will give you 0, so the if statement gets executed, when the first line is different from the other two. Or, if winninglline[1] was 2, the if would not be executed since 2 != 0.

    So, by my reasoning, that code would not work for your purposes Unless, of course, I'm wrong, in which case ammar, feel free to fry me in a wok
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    84

    Post

    thats ok, i will just have to find a way of doing it.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, it's not hard, just a bit longer:
    Code:
    //Another header to include, needed for the time(NULL) function call
    #include <time.h>
    
    //somewhere at the beginning of your program - only do ONCE:
    srand(time(NULL));
    
    //this section with the #define's and rand() just shows the basic
    //structure of how you find a random number in a certain range.
    #define RAND_MAX 10
    #define RAND_MIN 1
    int x = (rand() % (RAND_MAX - RAND_MIN + 1)) + RAND_MIN;
    
    //This is how you test to see if the 3 rows are equal to x:
    if(winningline[1] == winningline[2] && winningline[2] == winningline[3] && winningline[3] == x)
    {
        //Here, you assign what to do if x has certain values:
        switch(x)
        {
            case 1:
                what_to_do_if_x_is_1();
                something_else_to_do();
                break;
            case 2:
                what_to_do_if_x_is_2();
                something_else();
                break;
            case SOMETHING_ELSE:
                something_to_do();
                something_else();
                break;
            default:
                what_to_do_for_anything_else();
                something_else_to_do();
                break;
        }
    }
    I hope that code is bug free, although you do need to replace the what_to_do_if_blahblahblah()'s and something_else()'s. Hope that helps
    Last edited by Hunter2; 04-29-2003 at 05:21 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

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