My teacher would like me to put a "nested validation loop around the player's choice. That keeps looping until they enter valid input (1, 2, or 3)." I am having trouble getting anything to work could I get some pointers, please and thank you.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
// Keaton Graffis 12/28/2015
int main()
{
int seed = time(0);
srand(seed);
char playAgain;
int playerChoice, aiChoice, win = 0, tie = 0, lose = 0;
do
{
cout << "Lets play a game of rock paper scissors.\n";
cout << "Enter a 1 for sccisors a 2 for rock or a 3 for paper: ";
// Generates outcomes of the players choice
cin >> playerChoice;
if (playerChoice == 1)
{
cout << "You picked Rock!\n";
}
else if (playerChoice == 2)
{
cout << "You picked Paper!\n";
}
else if (playerChoice == 3)
{
cout << "You picked Scissors!\n";
}
// gentrate the computers choices
int aiChoice = rand() % 3 + 1;
if (aiChoice == 1)
{
cout << "The computer chose Rock!\n";
}
else if (aiChoice == 2)
{
cout << "The computer chose Paper!\n";
}
else if (aiChoice == 3)
{
cout << "The computer chose Scissors!\n";
}
// Determines wins, ties and loses
if (playerChoice == 1 && aiChoice == 1) {
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if (playerChoice == 1 && aiChoice == 2)
{
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if (playerChoice == 1 && aiChoice == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if (playerChoice == 2 && aiChoice == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if (playerChoice == 2 && aiChoice == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if (playerChoice == 2 && aiChoice == 3)
{
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if (playerChoice == 3 && aiChoice == 1)
{
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if (playerChoice == 3 && aiChoice == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if (playerChoice == 3 && aiChoice == 3)
{
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
// Outputs wins, ties and loses
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> playAgain;
system("CLS");
// Allow user to play again
} while (playAgain == 'Y' || playAgain == 'y');
}
You can throw a while loop over the block of code that reads in the input and break from the loop when you receive valid input.
int choice;
bool valid = false;
while(!valid) {
cout << "Enter a 1 for scissors, 2 for rock, 3 for paper" << endl;
cin >> choice;
if(choice == 1 || choice == 2 || choice == 3)
valid = true;
}
Related
I have written a code that displays a rock-paper-scissors game against the computer. I would like to add a feature where I can create a text file in order to store the person's score and the computer score and keep track of the score but I don't know how to do it. Thank you in advance!
Here is my code.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void rock_paper_scissors()
{
static int userscore = 0;
static int computerscore = 0;
string playername;
int userchoice;
int computerchoice;
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
cout << "Please enter your choice\n";
cin >> userchoice;
cout << endl;
while (!(userchoice > 0 && userchoice <= 3))
{
cout << "invalid choice. please enter a number between 1 and 3\n";
cin >> userchoice;
}
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
}
else
{
cout << "computer wins!" << endl;
}
cout << "thank you for playing!\n";
string restart;
cout << "Would you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
if (restart == "y")
{
rock_paper_scissors();
}
}
int main()
{
cout << "MAIN\n";
rock_paper_scissors();
return 0;
}
Why do you need to write the data into in file? Updating a simple txt file after each round could be cumbersome, as you always result in a single score at the end of the program. I would suggest changing the type of the rock_paper_scissors into int indicating the score achieved by the player in a single lot. The intermediate results are irrelevant. Just place the game loop into your main function and do not use a recursive function call and static function variables here. Otherwise, the player is forced to enter his name for every single lot.
Moreover, I tested your code and you have to change your error handling. I typed in "rock" and the program stuck in an infinity loop "invalid choice. please enter a number between 1 and 3\n" Whereby it is not possible to make another entry. As I entered a string instead of an integer, you have to reset the console. Beware of the dumbest possible user.
Moreover, you should seed your program to avoid identical computer choices in each game. This could be done with srand(time(NULL)).
Eventually, I write the tracked score into a score file at the end of the main function using the fstream standard library.
#include <iostream>
#include <string>
#include <algorithm> // min max
#include <fstream> // read/write from/to files
#include <time.h> // time
using namespace std;
int rock_paper_scissors(const std::string& playername);
int main()
{
srand(time(NULL));
cout << "MAIN\n";
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
int userscore = 0;
int computerscore = 0;
std::string playername;
std::string restart;
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
do
{
int result = rock_paper_scissors(playername);
cout << "thank you for playing!\n";
userscore += result;
computerscore += std::max(0, 3 - 2 * result);
cout << playername << "'s score: " << userscore;
cout << "\ncomputer's score: " << computerscore;
cout << "\nWould you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
} while (restart == "y");
std::ofstream ofile;
ofile.open("scorefile.txt");
ofile << "Scores:\n" << playername << ": " << userscore;
ofile << "\nComputer: " << computerscore;
ofile.close();
return 0;
}
int rock_paper_scissors(const std::string& playername)
{
int userchoice;
int computerchoice;
cout << endl << endl;
do {
cout << "Please enter your choice\n";
if (std::cin >> userchoice)
{
if (userchoice > 0 && userchoice <= 3)
{
break;
}
else
{
cout << "invalid choice. please enter a number between 1 and 3\n";
continue;
}
}
else if (!cin.bad() && !cin.eof())
{
// a non integer value entered
cerr << "invalid choice. please enter a number between 1 and 3\n";
// Reset error state
cin.clear();
// remove error input
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
} while (true);
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
return 1;
}
else
{
cout << "computer wins!" << endl;
return 0;
}
}
Can someone help me fix this, I'm doing the Learn C++ class on Codecademy and it says, "expressions must be a modifiable lvalue." It says this inside every if statement that I wrote. It's the last number in all of them. My guess would be that there is a problem with the logical operators in them but I'm not sure how to fix them. Any help would be greatly appreciated.
#include <iostream>
#include <stdlib.h>
int main() {
srand (time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "=================================\n";
std::cout << "rock paper scissors lizard spock!\n";
std::cout << "=================================\n";
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
std::cout << "4) Lizard\n";
std::cout << "5) Spock\n";
std::cout << "shoot!\n";
std::cin >> user;
if(computer == 1 && user == 2 || user = 5) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You win!\n";
}
else if (computer == 1 && int user == 3 || int user == 4){
std::cout << "Computer Chose Rock!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Rock!\n";
std::cout << "Tie!\n";
}
if(computer == 2 && user == 3 || user = 4) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You win!\n";
}
else if (computer == 2 && user == 1 || user == 5){
std::cout << "Computer Chose Paper!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Paper!\n";
std::cout << "Tie!\n";
}
if(computer == 3 && user == 1 || user = 5) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You win!\n";
}
else if (computer == 3 && user == 4 || user == 2){
std::cout << "Computer Chose Scissors!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Scissors!\n";
std::cout << "Tie!\n";
}
if(computer == 4 && user == 1 || user = 3) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You win!\n";
}
else if (computer == 4 && user == 2 || user == 5){
std::cout << "Computer Chose Lizard!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Lizard!\n";
std::cout << "Tie!\n";
}
if(computer == 5 && user == 4 || user = 2) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You win!\n";
}
else if (computer == 5 && user == 3 || user == 1){
std::cout << "Computer Chose Spock!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Spock!\n";
std::cout << "Tie!\n";
}
}
First of all, you should be careful with == or != in if statements. Then you already specified that user is int and you can use it in if statements without its type because compiler thinks that you are trying to declare it again.You also can't use time(NULL) without including ctime library.
#include <iostream>
#include <stdlib.h>
#include <ctime>
int main() {
srand(time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "=================================\n";
std::cout << "rock paper scissors lizard spock!\n";
std::cout << "=================================\n";
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
std::cout << "4) Lizard\n";
std::cout << "5) Spock\n";
std::cout << "shoot!\n";
std::cin >> user;
if (computer == 1 && user == 2 || user == 5) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You win!\n";
}
else if (computer == 1 && user == 3 || user == 4) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Rock!\n";
std::cout << "Tie!\n";
}
if (computer == 2 && user == 3 || user == 4) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You win!\n";
}
else if (computer == 2 && user == 1 || user == 5) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Paper!\n";
std::cout << "Tie!\n";
}
if (computer == 3 && user == 1 || user == 5) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You win!\n";
}
else if (computer == 3 && user == 4 || user == 2) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Scissors!\n";
std::cout << "Tie!\n";
}
if (computer == 4 && user == 1 || user == 3) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You win!\n";
}
else if (computer == 4 && user == 2 || user == 5) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Lizard!\n";
std::cout << "Tie!\n";
}
if (computer == 5 && user == 4 || user == 2) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You win!\n";
}
else if (computer == 5 && user == 3 || user == 1) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Spock!\n";
std::cout << "Tie!\n";
}
}
In addition to what was already said (you should be careful not to confuse user = 5 and user == 5; you should not use the keyword int, because the compiler thinks it's a declaration), there is one another very important issue.
You have to learn about operator precedence. The logical AND has higher priority than OR and will be evaluated first:
computer == 1 && user == 2 || user == 5
is equivalent to
(computer == 1 && user == 2) || user == 5
which means if user == 5 holds true, the whole statement will return true. Even if computer != 1. And your code will output "computer chose rock", which is incorrect.
Change it to
computer == 1 && (user == 2 || user == 5)
And as a general rule, that many similar if statements is a very poor choice. I hope they cover that later in your course. Good luck!
I'm trying to make a function for a rock, paper, scissors game with two char parameters where the first one represents the user's choice of rock, paper, or scissors. The second parameter represents the result of the game, either win, loss, or tie. When I tried to call the function, however, nothing is happening. I'm lost on what exactly I need to do next. All help is greatly appreciated!
#include <iostream>
#include <cstdlib>
using namespace std;
double playRPS (char a, char b);
int main() {
char letter;
char result = 0;
cout << "Welcome to COP3014 ROCK PAPER SCISSORS!\n\n";
cout << "Please select: " << endl
<< "Rock(r), Paper(p), or Scissors(s)? " << endl
<< "Or enter q to quit --> ";
cin >> letter;
if (letter == 'r' || letter == 'R' || letter == 'p' || letter == 'P' || letter == 's' || letter == 'S') {
playRPS(letter, result);
}
else {
cout << "Please enter r, p, or s" << endl;
}
return 0;
}
double playRPS (char x, char y) {
int choice1 = 0, choice2 = 0, choice3 = 0;
int user2 = rand() % 3 + 1;
if (( x == 'r' || x == 'R') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose ROCK!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if ((x == 'r' || x == 'R') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose ROCK!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if ((x == 'r' || x == 'R') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose ROCK!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose PAPER!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if (( x == 'p' || x == 'P') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose PAPER!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose PAPER!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose SCISSORS!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 's' || x == 'S') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose SCISSORS!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose SCISSORS!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else{
return main();
}
General remarks
using namespace std;
Avoid using namespace std.
return main();
You are not allowed to call main in your code. This will result in Undefined Behavior. Plus, what is your intention here?
rand()
rand() should be avoided. Here is an interesting video on why you should not use it, and instead use C++11 random.
y = choice2;
You are passing y by value, which means assigning it won't modify the y from the outside. You should pass y by reference when doing this (i.e. char& y in the declaration).
Why is the function not doing anything?
... Actually, it does!
user2 == '2'
Your comparisons are broken. '2' is actually not 2, but 50. The reason is that '2' is a character, so you actually are reading the associated character code.
This means all of your conditions are false in playRPS, so the only thing the function does it to call main() (in return main();).
What about shortening your code?
Your test cases are quite redundant and heavy. You could change it to drastically cut down your code size.
Let's print what the choice selected by the player...
if (x == 'r' || x == 'R')
cout << "You chose ROCK!" << endl;
else if (x == 'p' || x == 'P')
cout << "You chose PAPER!" << endl;
else if (x == 's' || x == 'S')
cout << "You chose SCISSORS!" << endl;
All good! Let's do the same with the computer's choice!
if (user2 == 1)
cout << "The computer chose... ROCK!" << endl;
else if (user2 == 2)
cout << "The computer chose... PAPER!" << endl;
else if (user2 == 3)
cout << "The computer chose... SCISSORS!" << endl;
Then you should compare what the player chose to what the computer chose, and tell who is the winner. Unfortunately, we can't compare x to user2 without doing many cases again...
What if we decided to have x's choice being saved the same way as user2? We also can use tolower to avoid checking for the caps variant of the letter.
int user1 = 0;
x = tolower(x); // we force x to lower case
if (x == 'r')
user1 = 1;
else if (x == 'p')
user1 = 2;
else if (x == 's')
user1 = 3;
Good! Now we can also improve conditions in our first if/else if block:
if (user1 == 1)
cout << "You chose ROCK!" << endl;
else if (user1 == 2)
cout << "You chose PAPER!" << endl;
else if (user1 == 3)
cout << "You chose SCISSORS!" << endl;
Which means we also can compare user1 to user2 so we know who won.
if (user1 == user2) {
cout << "It's a TIE!" << endl;
}
else if ((user1 == 1 && user2 == 2) ||
(user1 == 2 && user2 == 3) ||
(user1 == 3 && user2 == 1)) {
cout << "You LOSE!" << endl;
}
else {
cout << "You WIN!" << endl;
}
However, using 1, 2 and 3 does not make things very clear. What if you used an enum to represent these values?
enum RPSChoice
{
ROCK = 1,
PAPER = 2,
SCISSORS = 3
};
For example, the first block now looks like:
if (user1 == ROCK)
cout << "You chose ROCK!" << endl;
else if (user1 == PAPER)
cout << "You chose PAPER!" << endl;
else if (user1 == SCISSORS)
cout << "You chose SCISSORS!" << endl;
What if we wrapped our new two first blocks into a function so we avoid repeating ourselves?
void printDecision(string who, int choice) {
cout << who; // no matter what, we will tell who took a decision
if (choice == ROCK)
cout << " chose ROCK!" << endl;
else if (choice == PAPER)
cout << " chose PAPER!" << endl;
else if (choice == SCISSORS)
cout << " chose SCISSORS!" << endl;
}
This way, we can make playRPS even more clear, by replacing the two large blocks into simple, short function calls:
printDecision("You", user1);
printDecision("The computer", user2);
Let's do another simple function that decides who won:
int winner(int user1, int user2) {
if (user1 == user2) {
return 0; // tie
}
else if ((user1 == ROCK && user2 == PAPER) ||
(user1 == PAPER && user2 == SCISSORS) ||
(user1 == SCISSORS && user2 == ROCK)) {
return 2; // user2 is the winner
}
else {
return 1; // user1 is the winner
}
}
And a final one that returns the value we give according to a given character:
int characterToChoice(char c)
{
c = tolower(c);
if (c == 'r')
return ROCK;
else if (c == 's')
return SCISSORS;
else if (c == 'p')
return PAPER;
else
return 0; // Not a proper choice!
}
Done! This is the final program with all improvements in (nothing done to replace rand() in), and here is an online prompt to try it out.
Note that there are more ways you can improve the code, to simplify it even more and to make it more clear. I am most notably thinking about std::unordered_map to bind a RPSChoice value to a string, and a char to a RPSChoice. You may also prefer switch to if in some cases.
As stated by the comments to your question, you could have diagnosed this issue using a debugger. πάντα ῥεῖ's comment for reference:
The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert).
I am working on this assignment that requires me to create a game of rock, paper scissors for my programming class. I have ran into a couple issues that I am not fully educated about as I am still learning the basics of this language. My professor wants me to take in the users choice and the computers choice and then change it from an int to a string and print it out as "You chose: Rock" instead of "You chose: 1" which is what it is doing now. This part would be in the getComputerChoice() and getPlayerChoice() functions. Another issue I am having trouble with is my professor wants us to check if it was a tie or if the player won and I am trying to put these functions in an If else statement but I am not exactly sure what the proper way to declare the function in the else statement is. (This is commented out in the else part of the if statement in main all the way at the bottom)
My code is as follows:
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
int getComputerChoice();
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int);
int getComputerChoice()
{
int comp;
string cpChoice;
comp = rand() % 3 + 1;
if (comp == 1)
{
cpChoice = "Rock";
}
else if (comp == 2)
{
cpChoice = "Paper";
}
else if (comp == 3)
{
cpChoice = "Scissors";
}
return comp;
}
int getPlayerChoice()
{
int userChoice;
string strChoice;
cout << "Rock, Paper, or Scissors?\n";
cout << "1) Rock\n";
cout << "2) Paper\n";
cout << "3) Scissors\n";
cout << "Please enter your choice : \n";
cin >> userChoice;
cout << '\n';
while(userChoice < 1 || userChoice > 3)
{
cout << "Invalid Selection\n";
cout << "Re-enter a number between 1 and 3\n";
cin >> userChoice;
}
if (userChoice == 1)
{
strChoice = "Rock";
}
else if (userChoice == 2)
{
strChoice = "Paper";
}
else if (userChoice == 3)
{
strChoice = "Scissors";
}
return userChoice;
}
bool isTie(string userChoice, string comp)
{
if (userChoice != comp)
return false;
else
return true;
}
bool isPlayerWinner(int userChoice, int comp)
{
if ((comp == 1 && userChoice == 2) || (comp == 3 && userChoice == 1) || (comp == 2 && userChoice == 3))
return true;
else
return false;
}
int main()
{
char selection;
int computerChoice;
int userChoice1;
string Rock;
string Paper;
string Scissors;
srand ((unsigned int)time(NULL));
do
{
cout << '\n';
cout << "ROCK PAPER SCISSORS MENU\n";
cout << "-------------------------\n";
cout << "p) Play Game\n";
cout << "q) Quit\n";
cout << "Please enter your choice : \n";
cin >> selection;
cout << '\n';
cout << '\n';
// cin >> selection;
if (selection == 'p' || selection == 'P')
{
computerChoice = getComputerChoice();
//string computerChoice = to_string(comp);
userChoice1 = getPlayerChoice();
//string userChoice1 = to_string(userChoice);
cout << "You chose: " << userChoice1 << '\n';
cout << "The computer chose: " << computerChoice << '\n';
if (isTie(computerChoice, userChoice1)== true)
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "It's a TIE!";
}
else //(isPlayerWinner(computerChoice, userChoice1));
{
cout << "You choose: " << userChoice1;
cout << "The computer chose: " << computerChoice;
cout << "You WIN!";
}
}
//else if (selection != 'p' || selection != 'q')
//{
// cout << "Invalid Selection. Try Again.\n";
// cout << '\n';
// cin >> selection;
//}
else if (selection == 'q' || selection == 'Q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
}
else if (selection != 'p' || selection != 'q')
{
cout << "Invalid Selection. Try Again.\n";
cout << '\n';
}
}while (selection != 'q');
}
ANOTHER NOTE: my professor doesn't want any void functions and doesn't want any global variables.
She told me that my isTie function was fine but didn't mention anything about the isPlayerWinner function. I believe it is fine and has no issues, I am just not sure how to declare it in the main if else statement. Any help would be appreciated and if you guys have any questions or need more info please let me know. Thanks in advance.
You pretty much have everything right.
Your getPlayerChoice() and getComputerChoice() functions right now are both returning an int that stand for the players choice. You calculate the name for that choice in those functions, but dont do anything with the actual string representing the choice. You either need to return the choice string, or make a function that takes in an int and returns name associated with that choice:
string getChoiceName(int choice)
{
string strChoice;
if (choice== 1)
{
strChoice = "Rock";
}
else if (choice== 2)
{
strChoice = "Paper";
}
else if (choice== 3)
{
strChoice = "Scissors";
}
return strChoice;
}
I prefer the method, as it make it easier to calculate the result of the match if you have the ints. There are a lot of other routes you could take, like making an enum representing choices - even the function I gave you here isn't great, but it should get you to a working state.
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 - mostly moving the loop around as I thought I placed it wrong; I've searched through several C++ forums as well and I have found examples where you ask the userj "do you want to continue/play again" but that is not what I am to do, I am to have it automatically restart if there is a tie. 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.
[code]
#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;
}
[/code]
You problem is variable scope. change the first lines inside main():
int main()
{
int userChoice, cpuChoice;
do {
Then inside, instead of declaring these variables, just assign a value:
int cpuChoice = rand() % 5 + 1;
should be
cpuChoice = rand() % 5 + 1;
And get rid of the other declaration of userChoice altogether.
That should do it.
You declared these variables int the wrong scope. Move both declarations before the loop
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;
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);
return 0;
}