No matter which options you enter, it always displays the result for paper beating rock.

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

int main ()
{

	/* declaring variables */

	char first;
	char second;
	char paper = 'p';
	char paperP = 'P';
	char rock = 'r';
	char rockR = 'R';
	char scissors = 's';


	/* Instructions */

	cout << "Player one types in p for paper, r for rock, or s for scissors." << endl;
	cout << "Player two types in p for paper, r for rock, or s for scissors." << endl;
	cout << "You may use lower case letters or upper case." << endl;
	cin >> first >> second;

	/* Code to determine which player chose which option */



	switch (first)
	{
	case 'p':
	case 'P': first=paper;
		break;
	case 'r':
	case 'R': first=rock;
		break;
	case 's':
	case 'S': first=scissors;
		break;
	}
	switch (second)
	{
	case 'p':
	case 'P': second=paper;
		break;
	case 'r':
	case 'R': second=rock;
		break;
	case 's':
	case 'S': second=scissors;
		break;
	}


	/* Code to determine winnner, loser, or tie */

	if ((first=paper) && (second=rock))
	{
		cout << "Player 1 wins. Paper covers rock" << endl;
	}
	else if ((first=rock) && (second=scissors))
	{
		cout << "Player 1 wins. Rock breaks scissors." << endl;
	}
	else if ((first=scissors) && (second=paper))
	{
		cout << "Player 1 wins. Scissors cut paper." << endl;
	}
	else if ((first=paper) && (second=paper) || (first=rock) && (second=rock) || (first=scissors) && (second=scissors))
	{
		cout << "Nobody wins." << endl;
	}
	else if ((first=rock) && (second=paper))
	{
		cout << "Player 2 wins. Paper covers rock" << endl;
	}
	else if ((first=scissors) && (second=rock))
	{
		cout << "Player 2 wins. Rock breaks scissors." << endl;
	}
	else if ((first=paper) && (second=scissors))
	{
		cout << "Player 2 wins. Scissors cut paper." << endl;
	}
	else
	{
		cout << "invalid" << endl;
	}
}

New to c++ so it might be a little messy

Dani AI

Generated

Nice catch by and . After that correction, here are a few practical cleanups and a compact, reliable way to decide the winner that avoids lots of nested conditionals and duplicated variables.

The approach below normalizes input to a small integer (rock=0, paper=1, scissors=2), validates input, then uses a single modulus formula to pick the result. That makes the logic easier to read and harder to get wrong, and it also makes it simple to print the correct descriptive message for the winner.

#include <iostream>
#include <cctype>

int toChoice(char c) {
    c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    if (c == 'r') return 0; // rock
    if (c == 'p') return 1; // paper
    if (c == 's') return 2; // scissors
    return -1; // invalid
}

int main() {
    char a, b;
    if (!(std::cin >> a >> b)) return 0;
    int ca = toChoice(a), cb = toChoice(b);
    if (ca < 0 || cb < 0) { std::cout << "invalid\n"; return 0; }

    static const char* msg[] = {
        "Rock breaks scissors",
        "Paper covers rock",
        "Scissors cut paper"
    };

    int result = (ca - cb + 3) % 3; // 0: tie, 1: player1 wins, 2: player2 wins
    if (result == 0) std::cout << "Nobody wins.\n";
    else if (result == 1) std::cout << "Player 1 wins. " << msg[ca] << ".\n";
    else std::cout << "Player 2 wins. " << msg[cb] << ".\n";
}

Extra tips: compile with warnings enabled (for example g++ -std=c++17 -Wall -Wextra) to catch suspicious expressions; avoid unused variables (remove the duplicate paperP/rockR variables); use static_cast<unsigned char> before std::tolower to avoid undefined behavior on signed char; and validate input early so invalid entries are handled cleanly. These small changes make the program clearer and easier to extend (for example, to accept full words like "rock").

Recommended Answers

All 4 Replies

You need to use doubled equal sign for the equality comparison. As in:

if ((first==paper) && (second==rock))

What you have assigns paper to first (and evaluates as TRUE) and assigns rock as second (which also evaluates as TRUE), so your first test always runs.

if ((first=paper) && (second=rock))

should be if ((first==paper) && (second==rock))

ah. thank you very much.

guess i should stop falling asleep in lecture :P

ah. thank you very much.

guess i should stop falling asleep in lecture :P

That's what I keep telling my students. It never works.:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.