Basic IF statement problem for 'Rock Paper Scissors' game

This is a discussion on Basic IF statement problem for 'Rock Paper Scissors' game within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have to make a game of Rock Paper Scissors using the IF statement. Two players. So far ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Basic IF statement problem for 'Rock Paper Scissors' game

    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();
    When I enter 1,2,or 3 it just keeps saying 'you made an error", now isn't even compiling as it says "23 expected primary-expression before "else" ".

    Buggy.

    Thanks for any help.

    smiley.jpg

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> if (playerone == '1' + playertwo == '2')
    You don't use + to combine the expressions. Look up (or remember) how to write a logical AND.

    You also have an extra semi-colon that will cause compile errors.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    You're using incorrect operator + where it isn't supposed to be used and you didn't write that IF statement correct. Perhaps you should read up on +, && and IF statements a little.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    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" << endl;
    
       else (playerone == '1' && playertwo == '3');
           cout << "Player one wins" endl;
    
       
    
    cin.get();
    }
    Thanks !

    Just added a } at the end, and one further up.

    Fails to compile on this line:-

    Code:
     else if (playerone == '1' && playertwo == '3');
    Last edited by Swerve; 10-24-2007 at 06:09 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Look at that line very closely and you will spot the problem (that was also mentioned somewhere in this thread).

    Edit: Your edited post has more errors than the original.
    Last edited by Daved; 10-24-2007 at 06:14 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    IF-clause is still incorrect.
    Each if should have a starting
    Code:
    {
    and and ending
    Code:
    }
    (this also includes else if!) (unless you put a single line of code after the IF, in that case you don't need it.
    You should not put
    Code:
    ;
    after IFs either.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're also missing an >> here:
    Code:
    cout << "Player one wins" endl;
    [edit] Your program's output might also be easier to read if you printed it more like this:
    Code:
    Player 1, please enter
        '1' for Rock;
        '2' for Paper; or
        '3' for Scissors,
    and then press Enter
    Just a thought. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by dwks View Post
    You're also missing an >> here:
    I believe that should be <<, yes?
    Like this
    Code:
    cout << "Player one wins" << endl;

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, yes. I was just testing you.

    There are also some logical errors with the program. It reads in two numbers, but compares those numbers to '1' and '2' which are characters.

    You have to read in numbers and compare with 1, 2, etc (without quotes); or read in characters and compare with '1', '2', etc (with quotes).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic structure for 2 player game
    By pants in forum C Programming
    Replies: 2
    Last Post: 05-10-2009, 06:16 PM
  2. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  3. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 11:46 AM
  4. Rock, Paper, Scissors game help
    By MillaTime in forum Game Programming
    Replies: 4
    Last Post: 09-08-2002, 06:55 PM
  5. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM

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