rock paper scissors
Dec 10, 2010 at 9:52pm UTC
i have written this code for rock paper scissors but the code wont work. every time i execute it and run the program it comes out as a draw, is there something very simple that im missing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int P_C;
int P_S;
P_S = 0;
int C_C;
int C_S;
C_S = 0;
srand( time(NULL));
cout << "Welcome to Rock Paper Scissors. Please select from the following:- \n" ;
cout << "1: Rock \n2: Paper \n3: Scissors \n" ;
cin >> P_C;
C_C = (rand() % 3 +1);
cout << C_C << endl;
if (C_C = 1)
{
if (P_C = 1)
cout << "its a draw!!" << endl;
else if (P_C = 2)
{
P_S ++;
cout << "congratulations you win!!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
else if (P_C = 3)
{
C_S ++;
cout << "unluck the computer won this round!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
}
else if (C_C = 2)
{
if (P_C = 1)
{
C_S ++;
cout << "unluck the computer won this round!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
else if (P_C = 2)
cout << "Its a draw!\n" ;
else if (P_C = 3)
{
P_S ++;
cout << "congratulations you win!!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
}
else if (C_C = 3)
{
if (P_C = 1)
{
C_S ++;
cout << "unluck the computer won this round!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
else if (P_C = 2)
{
P_S ++;
cout << "congratulations you win!!\n The score is now:\n Player: " << P_S << endl << "Computer: " << C_S << endl;
}
else if (P_C = 3)
cout << "Its a draw!\n" ;
}
return 0;
}
Dec 10, 2010 at 9:55pm UTC
You are using = for comparison: You should use ==.
Dec 10, 2010 at 10:06pm UTC
Exactly as kooth said it.
Essentially instead of comparing (for example) P_C with 3, you put 3 in P_C, and then what the if gets is true because the P_C=3 operation succeeded; so this way EVERY IF SUCCEEDS.
You need == (2 equals) to compare.
Edit:
?????
Last edited on Dec 10, 2010 at 10:07pm UTC
Dec 10, 2010 at 10:12pm UTC
Actually the = operator returns (*this )
, so P_C = 3
would return 3. It's still true, but if you do P_C = 0
then you would get false.
Dec 10, 2010 at 11:23pm UTC
Meh, close enough... :P
Topic archived. No new replies allowed.