Rock-Paper-Scissors-Lizard-Spock C++ Pogram – Code Review Stack Exchange

Before anything, I would recommend fixing your spacing and tabbing. Four spaces for each level of nesting is recommended. It makes your code a lot more readable.

There is a pretty obvious bug in your code I noticed on my first pass. It’s how you’re checking the computers number versus the users number. For instance, take a look at this line:

if (user == 0 || 1)

To a novice, it looks like if user == 0 OR user == 1, but that is not happening here. Instead, this is only checking if user is equal to 0, OR if 1 is true. And 1 is always true. This can result in some unexpected behavior. Take a look:

if (user == 0 || 1) { 
    std::cout << "Greetings!" << std::endl; 
} else {
    std::cout << "Hello!" << std::endl;
}

Now, if I only enter 0, I will get the expected output of just “Greetings!”. However, if I enter literally any other number, I will instead only get “Greetings!”, instead of getting “Hello!”.

You have tons of repeated code. You can simplify this by first checking for if the computers input is the same as the users at the start of the program. Then you won’t have to keep checking on the goat if/else chunk.

if (computer == user) {
    std::cout << "You tied! :/" << std::endl;
    return 0;
}

Then, you can just assume the user loses at the start.

std::string message = "Computer wins! :(";

Now, you only have to check for win conditions, and if any are met, you change the message variable.

if (
    (user == 1 && (computer == 3 || computer == 4)) ||
    (user == 2 && (computer == 1 || computer == 5)) ||
    (user == 3 && (computer == 2 || computer == 4)) ||
    (user == 4 && (computer == 2 || computer == 5)) ||
    (user == 5 && (computer == 1 || computer == 3))
) message = "You win! :)";

Then, you just output the message variable.

std::cout << message << std::endl;

All in all, with these changes made your code now looks like this:

#include <iostream>

int main() {

    srand(time(NULL));
    int computer = rand() % 5 + 1;
    int user = 0;

    std::cout << "=============================================n";
    std::cout << "Rock    Paper    Scissors    Spock    Lizard!n";
    std::cout << "=============================================n";
    std::cout << "1) Rockn";
    std::cout << "2) Papern";
    std::cout << "3) Scissorsn";
    std::cout << "4) Lizardn";
    std::cout << "5) Spockn";
    std::cout << "n";
    std::cout << "Shoot!n";

    std::cin >> user;
    std::cout << "Computer chose: " << computer << "n";

    if (computer == user) {
        std::cout << "You tied! :/" << std::endl;
        return 0;
    }

    std::string message = "Computer wins! :(";

    if (
        (user == 1 && (computer == 3 || computer == 4)) ||
        (user == 2 && (computer == 1 || computer == 5)) ||
        (user == 3 && (computer == 2 || computer == 4)) ||
        (user == 4 && (computer == 2 || computer == 5)) ||
        (user == 5 && (computer == 1 || computer == 3))
    ) message = "You win! :)";

    std::cout << message << std::endl;

    return 0;
}