Need help with issue
Jun 12, 2014 at 6:21pm UTC
kk
Last edited on Jun 12, 2014 at 7:44pm UTC
Jun 12, 2014 at 6:38pm UTC
lines 201 through 241 run regardless of what the result was, that's why all three messages come up. Seems like you might want to tie those in to the if/else structure you have on lines 194-199.
Jun 12, 2014 at 6:39pm UTC
You can use a switch statement like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
switch (WhoWon)
{
case 'p' :
msgWIN=rand()%5;
if (msgWIN==0)
cout <<"Congradulations you won against the computer! " <<endl;
else if (msgWIN==1)
cout <<"OMG you just won, hip hip hurrayyyyyyyyyyy! " <<endl;
else if (msgWIN==2)
cout <<" Winner Winner Winner!" <<endl;
else if (msgWIN==3)
cout << "You win!" <<endl;
else if (msgWIN==4)
cout <<"You just won against Sheldon Cooper!" <<endl;
break ;
}
also lines 194-199 are unnecessary
Last edited on Jun 12, 2014 at 6:44pm UTC
Jun 12, 2014 at 6:57pm UTC
thanks for all of you feedback!!!! it was very helpful
however I have another question, when the user enters help when prompted to do so, the help instructions show up but immediately right after an error/abort message pops up!
Jun 12, 2014 at 7:24pm UTC
added a loop in the beginning. As far as I noticed there's no other issues
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
char again = 'y' ;
while (again == 'y' )
{
string userpick;
int CompChoice,usernumber = -1,msgLOSE, msgWIN, msgTIE;
char WhoWon;
srand(unsigned (time(NULL)));
CompChoice = rand() % 5 ;
cout<<"Let's play a game! " <<endl<<endl;
while (usernumber == -1)
{
cout<<"\nChoose rock, paper, scissors, lizard, spock or help: " ;
cin >>userpick;
if (userpick == "rock" )
usernumber = 0;
else if (userpick == "paper" )
usernumber = 1;
else if (userpick == "scissors" )
usernumber = 2;
else if (userpick == "lizard" )
usernumber = 3;
else if (userpick == "spock" )
usernumber = 4;
else if (userpick == "help" )
{
cout <<"Scissors cuts paper" <<endl;
cout<<"Scissors decapitate lizard" <<endl;
cout<<"Paper covers rock" <<endl;
cout<<"Paper disproves Spock" <<endl;
cout<<"Rock crushes scissors" <<endl;
cout<<"Rock crushes lizard" <<endl;
cout<<"Spock vaporizes rock" <<endl;
cout<<"Spock smashes scissors" <<endl;
cout<<"Lizard poisons Spock" <<endl;
cout<<"Lizard eats paper" <<endl;
}
}
if (usernumber==0)
{
if (CompChoice == 0)
{
WhoWon = 't' ;
cout <<"Computer chooses rock." <<endl<<endl;
}
else if (CompChoice == 1)
{
WhoWon = 'c' ;
cout <<"Computer chooses paper." <<endl<<endl;
}
else if (CompChoice == 2)
{
WhoWon = 'p' ;
cout <<"Computer chooses scissors." <<endl<<endl;
}
else if (CompChoice == 3)
{
WhoWon = 'p' ;
cout <<"Computer chooses lizard." <<endl<<endl;
}
else if (CompChoice == 4)
{
WhoWon = 'c' ;
cout <<"Computer chooses Spock." <<endl<<endl;
}
}
if (usernumber==1)
{
if (CompChoice ==0)
{
WhoWon = 'p' ;
cout <<"Computer chooses rock." <<endl<<endl;
}
else if (CompChoice == 1)
{
WhoWon = 't' ;
cout <<"Computer chooses paper." <<endl<<endl;
}
else if (CompChoice == 2)
{
WhoWon = 'c' ;
cout <<"Computer chooses scissors." <<endl<<endl;
}
else if (CompChoice == 3)
{
WhoWon = 'c' ;
cout <<"Computer chooses lizard." <<endl<<endl;
}
else if (CompChoice == 4)
{
WhoWon = 'p' ;
cout <<"Computer chooses Spock." <<endl<<endl;
}
}
if (usernumber==2)
{
if (CompChoice ==0)
{
WhoWon = 'c' ;
cout <<"Computer chooses rock." <<endl<<endl;
}
else if (CompChoice == 1)
{
WhoWon = 'p' ;
cout <<"Computer chooses paper." <<endl<<endl;
}
else if (CompChoice == 2)
{
WhoWon = 't' ;
cout <<"Computer chooses scissors." <<endl<<endl;
}
else if (CompChoice == 3)
{
WhoWon = 'p' ;
cout <<"Computer chooses lizard." <<endl<<endl;
}
else if (CompChoice == 4)
{
WhoWon = 'c' ;
cout <<"Computer chooses Spock." <<endl<<endl;
}
}
if (usernumber==3)
{
if (CompChoice ==0)
{
WhoWon = 'c' ;
cout <<"Computer chooses rock." <<endl<<endl;
}
else if (CompChoice == 1)
{
WhoWon = 'p' ;
cout <<"Computer chooses paper." <<endl<<endl;
}
else if (CompChoice == 2)
{
WhoWon = 'c' ;
cout <<"Computer chooses scissors." <<endl<<endl;
}
else if (CompChoice == 3)
{
WhoWon = 't' ;
cout <<"Computer chooses lizard." <<endl<<endl;
}
else if (CompChoice == 4)
WhoWon = 'p' ;
cout <<"Computer chooses Spock." <<endl<<endl;
}
if (usernumber==4)
{
if (CompChoice ==0)
{
WhoWon = 'p' ;
cout <<"Computer chooses rock." <<endl<<endl;
}
else if (CompChoice == 1)
{
WhoWon = 'c' ;
cout <<"Computer chooses paper." <<endl<<endl;
}
else if (CompChoice == 2)
{
WhoWon = 'p' ;
cout <<"Computer chooses scissors." <<endl<<endl;
}
else if (CompChoice == 3)
{
WhoWon = 'c' ;
cout <<"Computer chooses lizard." <<endl<<endl;
}
else if (CompChoice == 4)
{
WhoWon = 't' ;
cout <<"Computer chooses Spock." <<endl<<endl;
}
}
switch (WhoWon)
{
case 'p' :
msgWIN=rand()%5;
if (msgWIN==0)
cout <<"Congradulations you won against the computer! " <<endl;
else if (msgWIN==1)
cout <<"OMG you just won, hip hip hurrayyyyyyyyyyy! " <<endl;
else if (msgWIN==2)
cout <<" Winner Winner Winner!" <<endl;
else if (msgWIN==3)
cout << "You win!" <<endl;
else if (msgWIN==4)
cout <<"You just won against Sheldon Cooper!" <<endl;
break ;
case 'c' :
msgLOSE=rand()%5;
if (msgLOSE==0)
cout <<"You lost to the computer! " <<endl<<endl;
else if (msgLOSE==1)
cout <<"You were pwned! " <<endl<<endl;
else if (msgLOSE==2)
cout <<"You lose! " <<endl<<endl;
else if (msgLOSE==3)
cout <<"Sucka you just lost!" <<endl<<endl;
else if (msgLOSE==4)
cout <<"You just got destroyed by the computer!" <<endl<<endl;
break ;
case 't' :
msgTIE=rand()%3;
if (msgTIE==0)
cout <<"It's a tie" <<endl<<endl;
else if (msgTIE==1)
cout <<"You tied the computer!" <<endl<<endl;
else if (msgTIE==2)
cout <<"O no, look like it's a draw!" <<endl<<endl;
break ;
}
cout << "Do you wish to play again (y/n)? " <<endl;
cin >> again;
}
}
Jun 12, 2014 at 7:33pm UTC
okk, I extremely appreciate your help!
Jun 12, 2014 at 11:34pm UTC
@twensa Are you brazillian?
Topic archived. No new replies allowed.