2

I know there are different ways to write code for the "Rock, Paper, Scissors" game. But I have tried to write the following code based on my elementary knowledge of C++.

using std::cin;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
    enum object {rock, paper, scissors}
    object player1, player2;
    cout <<"Enter two objetcs (objects include rock, paper or scissors):";
    cin >>player1 >> player2;
    if (player1==player2) cout <<"objects are equal";
    else if (player1==rock && player2=paper cout << "player 2 is the winner";
    else if (player1==rock && player2=scissors cout<<"player 1 is the winner";
    else if (player1==paper && player2=rock) cout << "player 1 is the winner";
    else if (player1==paper && player2=scissors) cout <<"Palyer 2 is the winnder";
    else if (player1==scissors && player2=paper) cout << "Player 1 is winner";
    else cout <<"Player 2 is the winner";
}   

The compiler (Dev-C++) finds error with cin >>player1 >> player2; line, explaining that "In Function 'int main(int, char**): [error] expected initializer before 'player1'". I do not get the meaning of this warning. How I can I improve this code without too much change in its structure?

developer
  • 283
  • 2
  • 8
  • 3
    You will find a full explanation of everything [in your C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is the most complicated general purpose programming language in use today. There are multiple fundamental problems with the shown code, beyond the one compilation error you're asking about. Trying to slap together something that might look like valid C++ grammar, and expecting it to work, has very little chances of a successful result. The only practical way to learn C++ is with a good book. – Sam Varshavchik Nov 06 '19 at 17:33
  • 1
    `enum object {rock, paper, scissors}` seems to be missing a `;`. Not 100% sure `enum`s need `;`s after it, but could be it. –  Nov 06 '19 at 17:34
  • You're also missing a closing parenthesis in the second and third `if`. – Some programmer dude Nov 06 '19 at 17:35
  • @Algirdas. It is the enum just below the main. – EvilTeach Nov 06 '19 at 17:35
  • 3
    And unrelated to your current problem, but remember the difference between comparison for equality with `==` and assignment with `=`. – Some programmer dude Nov 06 '19 at 17:35
  • 1
    This is a duplicate with https://stackoverflow.com/questions/10371681/enum-type-can-not-accept-cin-command – Nick Skywalker Nov 06 '19 at 17:36
  • @EvilTeach Thank you. Somehow, I missed that :/ – Algirdas Preidžius Nov 06 '19 at 17:36
  • 1
    Does this answer your question? [enum type can not accept cin command](https://stackoverflow.com/questions/10371681/enum-type-can-not-accept-cin-command) – Bob__ Nov 06 '19 at 17:37

2 Answers2

3
enum object {rock, paper, scissors}

is missing a semicolon (;)

  • 5
    That may be true, but that has absolutely nothing to do, whatsoever, with the compilation error that's being asked about. The question is full of typos, and has major fundamental problems beyond this one. This one is probably due to a typo or a transcription error, and is the least of the problems here. – Sam Varshavchik Nov 06 '19 at 17:37
  • 1
    Apart from that there is also the missing fact that the `cin` operator `>>` is not defined for custom defined type `enum object`. – Nick Skywalker Nov 06 '19 at 17:37
  • 1
    @SamVarshavchik There are lots of errors in the code the OP posted, but the one the OP is asking about is most certainly caused by this. Putting it in a compiler and applying this fix will fix *this* error. Though the OP is about to get blasted with a few more errors. –  Nov 06 '19 at 17:42
  • @hipster Thanks for replying. – developer Nov 06 '19 at 18:31
0

Since there is no mapping between your inputs and the enum types, only a direct equality can actually be determined, otherwise the last else statement triggers. Below I've modified your code with a solution that accepts the char values that cin gives, and then sets the objects with their enum values. You also had several typos missing parentheses, semicolons, and equal signs, so please read through and note the differences. This will not only fix the compiler error, but will make your code run as expected.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
int main(int argc, char** argv) {
    enum object {rock, paper, scissors};
    object player1, player2;
    cout << "Enter two objetcs (objects include rock, paper or scissors):";
    char p1, p2;
    cin >> p1 >> p2;
    if (p1 == 'r') {
        player1 = rock;
    }
    else if (p1 == 'p') {
        player1 = paper;
    }
    else if (p1 == 's') {
        player1 = scissors;
    }
    else {
        cout << "invalid input, play again" << endl;
        exit(1);
    }

    if (p2 == 'r') {
        player2 = rock;
    }
    else if (p2 == 'p') {
        player2 = paper;
    }
    else if (p2 == 's') {
        player2 = scissors;
    }
    else {
        cout << "invalid input, play again" << endl;
        exit(1);
    }

    if (player1==player2) cout <<"objects are equal";
    else if (player1==rock && player2==paper) cout << "player 2 is the winner";
    else if (player1==rock && player2==scissors) cout<<"player 1 is the winner";
    else if (player1==paper && player2==rock) cout << "player 1 is the winner";
    else if (player1==paper && player2==scissors) cout <<"Palyer 2 is the winnder";
    else if (player1==scissors && player2==paper) cout << "Player 1 is winner";
    else cout <<"Player 2 is the winner";
    cout << endl;
}

It is very important that you understand types and how there is no way in your original code to determine what inputs correspond to what enum your object is.

ufoxDan
  • 609
  • 4
  • 13
  • 1
    Thank you so much for your detailed response! I had not noticed the error in the previous answer. I think cout << "Enter two objetcs (objects include rock, paper or scissors) should also be changed to something like cout << "Enter "r" for rock, "p" for paper or "s" for scissors). – developer Nov 11 '19 at 16:08
  • But how can we further improve the above code to enter the entire word instead of its initial? – developer Nov 12 '19 at 07:58
  • That would be better asked in a new question – ufoxDan Nov 12 '19 at 13:49