do while loop to repeat game when it is a tie - undeclared identifier error
Sep 22, 2013 at 9:30pm UTC
Hello all. I am stuck on something I know should be really simple to fix but I just can't figure it out I've been Google-ing and gone through my text (Gaddis C++ Intro, Ch. 6) and tried a few things, but it keeps coming back to the variables not being defined/declared for use within the loop -- except I don't see how they aren't. I'm using VS 2010.
Here's the code. I appreciate any help. I feel kinda stupid for not being able to fix this myself. This is my first programming class and I'm also taking visual basic at the same time. It's been interesting.
The debug errors I'm getting are both C2065 "undeclared identifier" for both cpuChoice and userChoice.
Thank you very much for taking the time to look at this and possibly offer suggestions.
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
#include <iostream>
#include <ctime>
using namespace std;
void outputChoice(int c)
{
switch (c)
{
case 1:
cout << "Rock" ;
break ;
case 2:
cout << "Paper" ;
break ;
case 3:
cout << "Scissors" ;
break ;
case 4:
cout << "Lizard" ;
break ;
case 5:
cout << "Spock" ;
break ;
}
}
//bool for determining if win or if draw -- loss will be elseif
bool isWin(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == 1 && cpuChoice == 3) ||
(userChoice == 1 && cpuChoice == 4) ||
(userChoice == 2 && cpuChoice == 1) ||
(userChoice == 2 && cpuChoice == 5) ||
(userChoice == 3 && cpuChoice == 2) ||
(userChoice == 3 && cpuChoice == 4));
return result;
}
bool isDraw(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == cpuChoice));
return result;
}
int main()
{
do {
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard" ;
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
{
int userChoice;
cout << "Please choose your move. Select 1-5: \n\n" ;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
int cpuChoice = rand() % 5 + 1;
cout << "You chose... " ;
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... " ;
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
return 0;
}
Sep 22, 2013 at 10:01pm UTC
i would declare the variables before the loop. I also noticed that you have an extra brace on line 60
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
#include <iostream>
#include <ctime>
using namespace std;
void outputChoice(int c)
{
switch (c)
{
case 1:
cout << "Rock" ;
break ;
case 2:
cout << "Paper" ;
break ;
case 3:
cout << "Scissors" ;
break ;
case 4:
cout << "Lizard" ;
break ;
case 5:
cout << "Spock" ;
break ;
}
}
//bool for determining if win or if draw -- loss will be elseif
bool isWin(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == 1 && cpuChoice == 3) ||
(userChoice == 1 && cpuChoice == 4) ||
(userChoice == 2 && cpuChoice == 1) ||
(userChoice == 2 && cpuChoice == 5) ||
(userChoice == 3 && cpuChoice == 2) ||
(userChoice == 3 && cpuChoice == 4));
return result;
}
bool isDraw(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == cpuChoice));
return result;
}
int main()
{
int userChoice;
int cpuChoice;
do {
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard" ;
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
cout << "Please choose your move. Select 1-5: \n\n" ;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
cin.ignore();
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
cpuChoice = rand() % 5 + 1;
cout << "You chose... " ;
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... " ;
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
cin.ignore();
return 0;
}
Sep 22, 2013 at 11:50pm UTC
thank you very much! i just couldn't see it. works great now.
Topic archived. No new replies allowed.