Rock, Paper, Scissors switch statement

So, i'm new to all this stuff and not used to c++ yet. My problem here is that when i enter 0-2 it works perfectly fine but when i put in anything greater than 2, i don't get the default message. I feel like it because of the "breaks;" but i'm not exactly sure. I've tried adding brackets and breaks in different spots. Im using a random number generator for the computers input and switch statements. Help please!

#include <cstdlib>
#include <iostream>
#include <complex>
#include <ctime>

using namespace std;

int main()
{
int choice;
rand();
int compn = rand() % 3;


cout << "scissors (0), rock (1), paper (2): ";
cin >> choice;


switch (compn)
{

case 0:

if (choice == 0)
cout << "The computer is scissors. You are scissors too. It is a draw." << endl;

else if (choice == 1)
cout << "The computer is scissors. You are rock. You win." << endl;

else if (choice == 2)
cout << "The computer is scissors. You are paper. You loose." << endl;
break;


case 1:

if (choice == 0)
cout << "The computer is rock. You are scissors. You loose." << endl;

else if (choice == 1)
cout << "The computer is rock. You are rock too. It is a draw." << endl;

else if (choice == 2)
cout << "The computer is rock. You are paper. You win." << endl;
break;

case 2:

if (choice == 0)
cout << "The computer is paper. You are scissors. You win." << endl;

else if (choice == 1)
cout << "The computer is paper. You are rock. You loose." << endl;

else if (choice == 2)
cout << "The computer is paper. You are paper. It is a draw." << endl;
break;

default:
cout << "Invalid number, try again" << endl;

}
system("pause");
return EXIT_SUCCESS;
}
Last edited on
Please use coding tags, edit your post and put ["code"] and ["/code"] tags around your code, not including the quotation marks.

You never get to the default: label because your "compn" variable will always be 0, 1, or 2 no matter what the user says as "choice".

To fix it,
I would do the opposite logic for your program, instead of switching on compn, do a switch statement for choice, and then in each branch, do
1
2
3
4
5
6
if (comp == 0)
   ...
else if (comp == 1)
   ...
else // compn is 2
   ... 


Edit: typos
Last edited on
Thanks, i switched the choice instead and it started working. i was sitting around for hours trying to figure this out and btw where do i put the coding tags i got a little confused with that.
Topic archived. No new replies allowed.