Function issue
Oct 5, 2016 at 8:40pm UTC
I'm having a problem with my function "determineWinner". Could someone explain why nothing is being output?
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int userChoice;
int computerChoice;
int getUserChoice()
{
int choice;
do
{
cout <<"1. Rock\n" ;
cout <<"2. Paper\n" ;
cout <<"3. Scissors\n" ;
cout <<"4. Lizard\n" ;
cout <<"5. Spock\n" ;
cout <<"Enter a choice: " ;
cin >> choice;
cin.clear();
cin.ignore();
}while (cin.fail() || (choice < 1) || (choice > 5));
return choice;
}
int getComputerChoice()
{
srand(time(0));
int computerChoice;
computerChoice = (rand() % 5) + 1;
return computerChoice;
}
void determineWinner(int computerChoiceWin, int userChoiceWin)
{
if (computerChoiceWin == '1' )
{
if (userChoiceWin == '1' )
{
cout << "You tied" ;
}
else if (userChoiceWin == '2' )
{
cout << "You win" ;
}
else if (userChoiceWin == '3' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '4' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '5' )
{
cout << "You win" ;
}
}
else if (computerChoiceWin == '2' )
{
if (userChoiceWin == '1' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '2' )
{
cout << "You tied" ;
}
else if (userChoiceWin == '3' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '4' )
{
cout << "You win" ;
}
else if (userChoiceWin == '5' )
{
cout << "You win" ;
}
}
else if (computerChoiceWin == '3' )
{
if (userChoiceWin == '1' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '2' )
{
cout << "You win" ;
}
else if (userChoiceWin == '3' )
{
cout << "You tie" ;
}
else if (userChoiceWin == '4' )
{
cout << "You win" ;
}
else if (userChoiceWin == '5' )
{
cout << "You lose" ;
}
}
else if (computerChoiceWin == '4' )
{
if (userChoiceWin == '1' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '2' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '3' )
{
cout << "You win" ;
}
else if (userChoiceWin == '4' )
{
cout << "You tied" ;
}
else if (userChoiceWin == '5' )
{
cout << "You win" ;
}
}
else if (computerChoiceWin == '5' )
{
if (userChoiceWin == '1' )
{
cout << "You win" ;
}
else if (userChoiceWin == '2' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '3' )
{
cout << "You win" ;
}
else if (userChoiceWin == '4' )
{
cout << "You lose" ;
}
else if (userChoiceWin == '5' )
{
cout << "You tied" ;
}
}
return ;
}
int displayChoice()
{
switch (userChoice)
{
case 1 : cout << "You chose Rock\n" ;
break ;
case 2 : cout << "You chose Paper\n" ;
break ;
case 3 : cout << "You chose Scissors\n" ;
break ;
case 4 : cout << "You chose Lizard\n" ;
break ;
case 5 : cout << "You chose Spock\n" ;
break ;
}
switch (computerChoice)
{
case 1 : cout << "The computer chose Rock" ;
break ;
case 2 : cout << "The computer chose Paper" ;
break ;
case 3 : cout << "The computer chose Scissors" ;
break ;
case 4 : cout << "The computer chose Lizard" ;
break ;
case 5 : cout << "The computer chose Spock" ;
break ;
}
}
int main()
{
computerChoice = getComputerChoice();
userChoice = getUserChoice();
displayChoice();
determineWinner(computerChoice, userChoice);
return 0;
}
Oct 5, 2016 at 8:54pm UTC
When you compare an int to a character constant you are comparing the character's decimal value to the int. Remember that '1' (a character constant) has a decimal value of 49 not 1.
Oct 5, 2016 at 8:56pm UTC
Because userChoice
and computerChoice
are int
s
In determineWinner
you're testing for string values. That's why you're not getting any matches--1 is not equal to '1'.
Oct 5, 2016 at 8:59pm UTC
Thank you for the help.
Topic archived. No new replies allowed.