Functional error, please help me
Apr 23, 2015 at 4:09am UTC
I don't know why it doesn't work. It is basically rock, scissor, paper with more option. and I think the loop is not working, the random number for computer's guess works, and the if statements works individually. However, when I use functions, it doesn't work. Please help me figure it out.
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 243
// Preprocessor statements
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Program starts here:
//functions declaration.
void printInstructions();
int getComputerGuess();
int getUserChoice();
string getComputerGuess2();
int stringChanger(string);
int main()
{
// YOUR CODE GOES HERE
string userChoices, cpuChoices, yesOrNo;
int score = 0, cscore = 0, counter = 0;
printInstructions();
cout << "\n" ;
while (getUserChoice() != 7) {
getUserChoice();
getComputerGuess();
getComputerGuess2();
if (getUserChoice() == 1) {
if (getComputerGuess() == 1) {
cout << "Tie" ;
}
else if (getComputerGuess() == 2) {
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 3) {
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 4) {
cout << "You Won" ;
score++;
}
else {
cout << "Computer Won" ;
cscore++;
}
}
else if (getUserChoice() == 2) {
if (getComputerGuess() == 1) {
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 2) {
cout << "Tie" ;
}
else if (getComputerGuess() == 3){
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 4) {
cout << "You Won" ;
score++;
}
else {
cout << "Computer Won" ;
cscore++;
}
}
else if (getUserChoice() ==3){
if (getComputerGuess() == 1) {
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 2) {
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 3){
cout << "Tie" ;
}
else if (getComputerGuess() == 4) {
cout << "Computer Won" ;
cscore++;
}
else {
cout << "You Won" ;
score++;
}
}
else if (getUserChoice() == 4) {
if (getComputerGuess() == 1) {
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 2) {
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 3){
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 4) {
cout << "Tie" ;
}
else {
cout << "You Won" ;
score++;
}
}
else if (getUserChoice() == 5) {
if (getComputerGuess() == 1) {
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 2) {
cout << "You Won" ;
score++;
}
else if (getComputerGuess() == 3){
cout << "Computer Won" ;
cscore++;
}
else if (getComputerGuess() == 4) {
cout << "Computer Won" ;
cscore++;
}
else {
cout << "Tie" ;
}
}
else if (getUserChoice() == 6) {
printInstructions();
}
if (getUserChoice() != 7){
cout << "\n" ;
cout << "Do you want to play again? Type 'YES' to play again, Type 'QUIT' to end" << endl;
getUserChoice();
}
counter++;
}
cout << "The score was " << score << "(YOU) : " << cscore << "(COM)" << endl;
cout << "Thank you for playing" << endl;
// End of program
return 0;
}
void printInstructions(){
cout << "Welcome to Advanced Rock, Scissors, and Papaer(ARSP) Game" << endl;
cout << endl;
cout << "You will play ARSP game against computer, and you can try it infinite time" << endl;
cout << "you have seven choices (Choice should be capitalized): \n 1. ROCK \n 2. SCISSORS \n 3. PAPER \n 4. LIZARD \n 5. SPOCK \n 6. INSTRUCTION(for instruction) \n 7. QUIT(to end)" << endl;
cout << endl;
}
int getComputerGuess(){
int cpuChoice, MIN_VALUE = 1, MAX_VALUE = 5;
srand(time(NULL)); //change the random number everytime it runs
cpuChoice = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE; //getComputerGuess() is random number between 1 to 5. its number determines what cpu put out
return cpuChoice;
}
string getComputerGuess2(){
int getComputerGuess();
string cpuChoices;
if (getComputerGuess() == 1) {
cpuChoices = "ROCK" ;
}
else if (getComputerGuess() == 2) {
cpuChoices = "SCISSORS" ;
}
else if (getComputerGuess() == 3){
cpuChoices = "PAPER" ;
}
else if (getComputerGuess() == 4) {
cpuChoices = "LIZARD" ;
}
else {
cpuChoices = "SPOCK" ;
}
return cpuChoices;
}
int getUserChoice() {
string userChoices;
int userChoice;
int stringChanger(string);
do {
cin >> userChoices;
userChoice = stringChanger(userChoices);
}while (userChoice<0 || userChoice>7);
return userChoice;
}
int stringChanger(string userChoices){
if (userChoices == "ROCK" ){
return 1;
}
else if (userChoices == "SCISSORS" ){
return 2;
}
else if (userChoices == "PAPER" ){
return 3;
}
else if (userChoices == "LIZARD" ){
return 4;
}
else if (userChoices == "SPOCK" ) {
return 5;
}
else if (userChoices == "INSTRUCTION" ){
return 6;
}
else if (userChoices == "QUIT" ) {
return 7;
}
else if (userChoices == "YES" ) {
return 0;
}
}
Last edited on Apr 23, 2015 at 10:29pm UTC
Apr 23, 2015 at 8:12am UTC
However, when I use functions, it doesn't work.
What doesn't work?
Apr 23, 2015 at 4:46pm UTC
So it doesn't leave the loop, and not giving me whether or not I won the rock scissor paper against computer. It works without functions
Topic archived. No new replies allowed.