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).
Related
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!
The if else at the bottom will not work; it says that pets do not really exist.
void checkIn ()
{
int roomNum = 0;
int party = 0;
char pets;
char smoke;
cout << "----------------------------" << endl;
cout << "Welcome to Shrek's Swamp Inn" << endl << endl;
cout << "How many people are In your party? " << endl;
cin >> party;
cout << "Do you have pets? (Y/N)" << endl;
cin >> pets;
switch (pets)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
cout << "Do you want a smoke room? (Y/N) " << endl;
cin >> smoke;
switch (smoke)
{
case 'Y' : case 'y':
cout << "GTLO" << endl;
break;
case 'N' : case 'n':
cout << "cool cool" << endl;
break;
default :
cout << "User error" << endl;
}
if((pets == 'Y' || 'y') && (smoke == 'Y' || 'y')){
roomNum = rand() % 4 + 1;
cout << "Your room is " << roomNum << "S" << endl;
}
else if((pets == 'Y' || 'y') && (smoke == 'n' || 'N')){
roomNum = rand() % 8 + 5;
cout << "Your room is " << roomNum << "P" << endl;
}
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
roomNum = rand() % 15 + 9;
cout << "Your room is " << roomNum << "R" << endl;
}
}
You should change the if condition
(pets == 'Y' || 'y')
to
(pets == 'Y' || pets == 'y')
Otherwise, 'y' (as a non-zero value) will always be evaluated as true for if condition, then (pets == 'Y' || 'y') will be always true too.
And it's the same for all other if conditions.
Adding to songyuanyao's answer, you have another syntax error
else if((pets == 'n' || 'N') && (smoke == 'n' || 'N'));{
Note the semicolon ; towards the end of the statement, you need to remove this
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;
}
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've just started learning C++. I am currently using Bloodshed Dev C++. dI'm creating a very basic and simple Rock Paper and Scissors Game. Everything in the program is working correctly except for the exit loop. Here is my code:
/* FILE INFO
File Name: Chapter 3 - Project 1.cpp
Author: Richard P.
P#: ---------
Assignment: Chapter 3 Project 1
*/
#include <iostream>
using namespace std;
int main()
{
char Player1_choice;
char Player2_choice;
char keep_going;
cout << "Welcome to Rock, Paper, Scissors! The rules of the game are simple:\n\n"
<< "Enter either the letter P (for Paper), S (for Scissors), or R (for Rock)\n\n"
<< "Paper covers rock, Rock breaks scissors, Scissors cut paper\n\n"
<< "If both players pick the same choice then it is a draw!\n"
<< "-----------------------------------------------------------------------------\n\n";
do
{
cout << "Okay, player 1, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
cin >> Player1_choice;
switch (Player1_choice) //I COULD DO A NESTED SWITCH STATMENT BUT FOR VARIETY I AM USING SWITCH AND IF STATMENTS.
{
case 'R':
case 'r':
cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
cin >> Player2_choice;
if (Player2_choice == 'R' || Player2_choice == 'r')
cout << "It's a draw!\n";
else if (Player2_choice == 'P' || Player2_choice == 'p')
cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
else if (Player2_choice == 'S' || Player2_choice == 's')
cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
else
cout << "That is not a valid entry! Please read the rules and play again :)\n";
break;
case 'P':
case 'p':
cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
cin >> Player2_choice;
if (Player2_choice == 'R' || Player2_choice == 'r')
cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
else if (Player2_choice == 'P' || Player2_choice == 'p')
cout << "It's a draw!\n";
else if (Player2_choice == 'S' || Player2_choice == 's')
cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
else
cout << "That is not a valid entry! Please read the rules and play again :)\n";
break;
case 'S':
case 's':
cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
cin >> Player2_choice;
if (Player2_choice == 'R' || Player2_choice == 'r')
cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
else if (Player2_choice == 'P' || Player2_choice == 'p')
cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
else if (Player2_choice == 'S' || Player2_choice == 's')
cout << "It's a draw!\n";
else
cout << "That is not a valid entry! Please read the rules and play again :)\n";
break;
default:
cout << "That is not a possible entry.\n";
}
cout << "\n\nKeep playing?\n";
cin >> keep_going;
} while (keep_going = 'y');
cout << "You have chosen not to keep playing. Press Enter to exit the game";
cin.get();
cin.get();
return 0;
}
The cin.get(); is simply there to keep the program from exiting immedietly once it has finished running.
If i bust down everything else and only leave the do while and the code that affect's it, here is what i have.
char keep_going;
do
{
cout << "\n\nKeep playing?\n";
cin >> keep_going;
} while (keep_going = 'y');
It should only continue and start the loop again if i specifically enter the letter 'y' but no matter what i enter it just doesn't seem to work properly. Thanks in advance for any and all help.
You should use == (comparison) instead of = (assignment):
do
{
cout << "\n\nKeep playing?\n";
cin >> keep_going;
} while (keep_going == 'y');
Reason being, is that when you assign a variable, a value that evaluates to true is returned. For example:
if(foo = 42) { //equivalent to if(true) {...}
cout << "This is evaluating variable assignment";
} else {
cout << "This line will never be reached";
}
Use == instead of = when comparing things. = makes the left value equal the right, while == is used to compare two objects.
Correct Code:
do
{
cout << "\n\nKeep playing?\n";
cin >> keep_going;
} while (keep_going == 'y');
In addition to using ==, you should use cin.get() so user does not need to press Enter:
char keep_going;
do {
cout << "\n\nKeep playing?\n";
keep_going = cin.get();
} while (keep_going == 'y');
//Syntax you're looking for is this:
char keep_going;
do {
//some statements
} while(cin.get(keep_going));