-2

I am trying to make a Rock-Paper-Scissor game in C++ in Visual Studio.

I am trying to make the computer generated response, but when I make the if statements, it thinks it is an error on line 9:

#include <iostream>

using namespace std;

string rockPaperScissor;

int computerChoiceInteger = 1 + (rand() % 3);

if (computerChoiceInteger == 1) {
    cout << "1";
}

if (computerChoiceInteger == 2) {
    cout << "2";
}

if (computerChoiceInteger == 3) {
    cout << "3";
}

int main() {
    cout << "Rock, paper or scissor?\n";
    cout << "R=Rock P=Paper S=Scissor\n";

    cin >> rockPaperScissor;

    if (rockPaperScissor == "r" || "R") {
        cout << "correct";
    }
    if (rockPaperScissor == "p" || "P") {
        cout << "correct";
    }
    if (rockPaperScissor == "s" || "S") {
        cout << "correct";
    }
}

I have tried to comment out the first if statement, but then it says there is an error on line 13.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    The statements in the global space should be in a function. – Eljay Feb 14 '23 at 18:00
  • 3
    Also this is wrong: `rockPaperScissor == "r" || "R"` (Doesn't do you thing it does), it should be `rockPaperScissor == "r" || rockPaperScissor == "R"` – Marek R Feb 14 '23 at 18:01
  • 1
    `if (rockPaperScissor == "r" || "R")` -- In the C++ book you're using, how are `if` statements constructed when comparing two or more items? – PaulMcKenzie Feb 14 '23 at 18:04
  • 1
    Please refer to [some good beginners book, or basic tutorial](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?r=Saves_AllUserSaves) _@Tord_ – πάντα ῥεῖ Feb 14 '23 at 18:17
  • `if (tolower(rockPaperScissor[0]) == 'r)`. You can also use `std::transform` to convert the entire string to lower or upper case. This will elimate half of your comparisons. – Thomas Matthews Feb 14 '23 at 18:39

2 Answers2

2

For your main error, you cannot have logical statements (if, while, etc.) outside of a function body. You will need to move all of your if statements into your main function body.

C++ uses the main function as the "entry point" for your program, which means once all global variables, class definitions, and more, are settled, the main function will be what your program begins to execute. All the logic for your program should always be inside of a function.

Also, as others in the comments have pointed out, you cannot choose between two values in an or statement like you are currently doing. You need to explicitly make two comparisons. I've moved your conditional logic as well as changed your or statements here, as well as added some more newlines:

#include <iostream>

using namespace std;

int computerChoiceInteger = 1 + (rand() % 3);

int main() {
    if (computerChoiceInteger == 1) {
        cout << "1\n";
    }

    if (computerChoiceInteger == 2) {
        cout << "2\n";
    }

    if (computerChoiceInteger == 3) {
        cout << "3\n";
    }

    cout << "Rock, paper or scissor?\n";
    cout << "R=Rock P=Paper S=Scissor\n";

    string rockPaperScissor;
    cin >> rockPaperScissor;

    if (rockPaperScissor == "r" || rockPaperScissor == "R") {
        cout << "correct\n";
    }

    if (rockPaperScissor == "p" || rockPaperScissor == "P") {
        cout << "correct\n";
    }

    if (rockPaperScissor == "s" || rockPaperScissor == "S") {
        cout << "correct\n";
    }

    return 0;
}

As further reading, you may be interested in Generate random number between 1 and 3 in C++ to generate a more truly random choice by the computer.

dylan
  • 68
  • 7
2

Your program is structured all wrong.

  • you are missing #include <stdlib.h> or #include <cstdlib> to declare rand(). It is possible that <iostream> is already including one of them internally for you, but you should not rely on that behavior. Be explicit in the headers you want to use.

  • global scope can only have declarations, not statements. Statements can only appear in function scope.

  • your use of the || operator is incorrect. You can't compare a variable to multiple values the way you are trying to. You need a separate comparison for each value.

Try something more like this instead:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

    int computerChoiceInteger = 1 + (rand() % 3);
    cout << computerChoiceInteger << endl;

    cout << "Rock, paper or scissor?\n";
    cout << "R=Rock P=Paper S=Scissor\n";

    string rockPaperScissor;
    cin >> rockPaperScissor;

    if (rockPaperScissor == "r" || rockPaperScissor == "R") {
        cout << "correct";
    }
    else if (rockPaperScissor == "p" || rockPaperScissor == "P") {
        cout << "correct";
    }
    else if (rockPaperScissor == "s" || rockPaperScissor == "S") {
        cout << "correct";
    }

    return 0;
}

Now, the code should at least compile, so you can carry on with finishing your game logic (since what you have shown is not a complete Rock-Paper-Scissor game yet).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    I recommend transforming the `rockPaperScissor` string to all lower or all upper case after the `cin` statement. This would reduce the comparisons by half and simplify the comparisons. – Thomas Matthews Feb 14 '23 at 18:41
  • @ThomasMatthews while a valid point, that would just be an optimization at this stage in the OP's development. – Remy Lebeau Feb 14 '23 at 18:48