C++ Basic IF statement problem for 'Rock Paper Scissors' game

Discussion in 'OT Technology' started by Swerve, Oct 25, 2007.

  1. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    Hi there,

    I have to make a game of Rock Paper Scissors using the IF statement. Two players.

    So far this is what I have :-

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    int playerone;
    int playertwo;
    
    
    
    cout << "Player 1, please enter '1' for Rock, '2' for Paper or '3' for Scissors and then press Enter ";
    cin >> playerone;
    cin.ignore();
    cout << "Player 2, please enter '1' for Rock, '2' for Paper or '3' for Scissors and then press Enter ";
    cin >> playertwo;
    cin.ignore();
    
    
    if (playerone == '1' + playertwo == '2') 
    
       {
           cout << "Player two wins";
    
       else if (playerone == '1' + playertwo == '3');
           cout << "Player one wins";
    
       else 
           cout << "you made an error";}
    
    cin.get();
    Fails to compile on this line:-

    Code:
     else if (playerone == '1' && playertwo == '3');
    Just cannot get it right :wtc:

    I just know it's stupid, but my mind just wont work.
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The else doesn't have a matching if since you have the else surrounded by the curly braces. Also, the semicolon at the end of that line won't get you the result you're looking once you get it running.
     
  3. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    Better? Confused.

     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    if (playerone == '1' && playertwo == '2')

    { <---????

    plus, the semicolon is still there.
     
  5. otnorot

    otnorot ◑_◔ OT Supporter

    Joined:
    Apr 7, 2002
    Messages:
    12,728
    Likes Received:
    0
    Location:
    Toronto
    if-else statements should look like this:
    Code:
    if (...)  {
       blag;
    }
    else if (...)  {
       blah;
    }
    else  {
       blai;
    }
    
    or, if you only have one statement for every if:

    Code:
    if (...)
       blag;
    else if (...)
       blah;
    else
       blai;
    
     
    Last edited: Oct 25, 2007
  6. wabash9000

    wabash9000 Active Member

    Joined:
    Dec 27, 2006
    Messages:
    14,392
    Likes Received:
    0
    Location:
    Overland Park, KS
    1.remove the semicolin
    2. you are treating the ints like char's
    Code:
    int a = 0;
    if(a==0)
       cout<< "congrats";
    else 
        cout<<"Go fuck yourself";
    
     
  7. Lurker111

    Lurker111 New Member

    Joined:
    Sep 23, 2003
    Messages:
    1,837
    Likes Received:
    0
    Location:
    Back in Seattle
    Everyone told you what to do but nobody told you why:

    Code:
    If
    else if
    else
    These control statements do NOT need a semicolon after the logical test [the stuff in the paranthesis]. IF you use a semicolon after the logical test it is read by the compiler as a NULL statement just like if you were to put a semicolon on a line by itself:

    Code:
    int playerone;
    ; //This is read as a null statement
    
    if (playerone)
    ;
    else
    
    //The above if statement is equivalent to using 
    
    if (playerone);
    else
    An open brace should always have a closing brace to contain the code you want to "block together".

    Code:
    if (playerone)
    {
    // statements to execute on this condition
    }
    else if (!playerone)
    {
    // statements to execute on this condition
    }
    else
    If you don't close it off your "else if" is essentially part of the statements to execute only if the first test is true...
     

Share This Page