I'm working on a Rock Paper Scissors game in C++, and for my exception handling I'm trying to create an if statement for when the user input is not one of three characters, 'R', 'S' or 'P', but I just don't know how to properly build it without receiving error alerts.
I went to my textbook, my professor's videos on Exception Handling, and of course here on StackOverflow, to see if my issue has already been found (of which I've found none). I'm also not sure if I'm using throw correctly.
Here is my code:
#include <iostream>
#include <cctype>
using namespace std;
int main(int argc, const char * argv[]) {
char Player1;
char Player2;
char playAgain = 'Y';
do {
try {
cout << "Welcome to Rock Paper Scissors!" << endl;
cout << "Player 1, you're up! Enter R, P, or S." << endl;
cin >> Player1;
Player1 = toupper(Player1);
cout << "Now Player 2, make your move! R, P, or S." << endl;
cin >> Player2;
Player2 = toupper(Player2);
if( Player1 != 'R' && Player2 != 'R')
throw 0;
else if(Player1 != 'S' && Player2 !='S')
throw 0;
else if(Player1 != 'P' && Player2 !='P')
throw 0;
if (Player1 == Player2)
cout << "Tie game. Play again!";
else if(Player1 == 'R' && Player2 == 'P')
cout << "Paper covers rock, Player 2 wins!";
else if(Player1 == 'R' && Player2 == 'S')
cout << "Rock breaks scissors, Player 1 wins!";
else if(Player1 == 'P' && Player2 == 'R')
cout << "Paper covers rock, Player 1 wins!";
else if(Player1 == 'P' && Player2 == 'S')
cout << "Scissors cut paper, Player 2 wins!";
else if(Player1 == 'S' && Player2 == 'R')
cout << "Rock breaks scissors, Player 2 wins!";
else if(Player1 == 'S' && Player2 == 'P')
cout << "Scissors cut paper, Player 1 wins!";
}
catch(int errID) {
cout << "Error: " << errID << endl;
}
cout << "Do you want to play again? Y/N ";
cin >> playAgain;
playAgain = toupper(playAgain);
}
while (playAgain == 'Y');
return 0;
}
Your first three if statements are wrong. If both players don't enter R, you throw an error. The statements need to be rewritten to this instead:
if (Player1 != 'R' && Player1 != 'P' && Player1 != 'S')
throw 0;
if (Player2 != 'R' && Player2 != 'P' && Player2 != 'S')
throw 0;
I would suggest wrapping the input logic in some helper functions. And also, you can clean up the rest of the if statements as well.
Try this:
#include <iostream>
#include <cctype>
using namespace std;
char getInput(const char *prompt)
{
char input;
cout << prompt << ": ";
if (!(cin >> input)) throw 1;
return toupper(input);
}
char getChoice(const char *prompt)
{
cout << prompt;
char choice = getInput(" Enter R, P, or S");
if ((choice != 'R') && (choice != 'P') && (choice != 'S')) throw 2;
return choice;
}
int main(int argc, const char * argv[]) {
char Player1;
char Player2;
cout << "Welcome to Rock Paper Scissors!" << endl;
do {
try {
Player1 = getChoice("Player 1, you're up!");
Player2 = getChoice("Now Player 2, make your move!");
if (Player1 == Player2)
cout << "Tie game!" << endl;
else if (Player1 == 'R')
{
if (Player2 == 'P')
cout << "Paper covers rock, Player 2 wins!" << endl;
else
cout << "Rock breaks scissors, Player 1 wins!" << endl;
}
else if (Player1 == 'P')
{
if (Player2 == 'R')
cout << "Paper covers rock, Player 1 wins!" << endl;
else
cout << "Scissors cut paper, Player 2 wins!" << endl;
}
else {
if (Player2 == 'R')
cout << "Rock breaks scissors, Player 2 wins!" << endl;
else
cout << "Scissors cut paper, Player 1 wins!" << endl;
}
}
catch (int errID) {
cout << "Error: " << errID << endl;
}
}
while (getInput("Do you want to play again? Y/N") == 'Y');
return 0;
}
Alternatively, consider using a switch statement instead of multiple if statements:
#include <iostream>
#include <cctype>
using namespace std;
#define MAKE_USHORT(ch1, ch2) ((static_cast<unsigned short>(ch1) << 8) | static_cast<unsigned short>(ch2))
const char ROCK = 'R';
const char PAPER = 'P';
const char SCISSOR = 'S';
const unsigned short ROCK_ROCK = MAKE_USHORT(ROCK, ROCK);
const unsigned short ROCK_PAPER = MAKE_USHORT(ROCK, PAPER);
const unsigned short ROCK_SCISSOR = MAKE_USHORT(ROCK, SCISSOR);
const unsigned short PAPER_ROCK = MAKE_USHORT(PAPER, ROCK);
const unsigned short PAPER_PAPER = MAKE_USHORT(PAPER, PAPER);
const unsigned short PAPER_SCISSOR = MAKE_USHORT(PAPER, SCISSOR);
const unsigned short SCISSOR_ROCK = MAKE_USHORT(SCISSOR, ROCK);
const unsigned short SCISSOR_PAPER = MAKE_USHORT(SCISSOR, PAPER);
const unsigned short SCISSOR_SCISSOR = MAKE_USHORT(SCISSOR, SCISSOR);
char getInput(const char *prompt)
{
char input;
cout << prompt << ": ";
if (!(cin >> input)) throw 1;
return toupper(input);
}
char getChoice(const char *prompt)
{
cout << prompt;
char choice = getInput(" Enter R, P, or S");
if ((choice != ROCK) && (choice != PAPER) && (choice != SCISSOR)) throw 2;
return choice;
}
unsigned short getChoices()
{
char Player1 = getChoice("Player 1, you're up!");
char Player2 = getChoice("Now Player 2, make your move!");
return MAKE_USHORT(Player1, Player2);
}
int main(int argc, const char * argv[]) {
cout << "Welcome to Rock Paper Scissors!" << endl;
do {
try {
switch (getChoices())
{
case ROCK_ROCK:
case PAPER_PAPER:
case SCISSOR_SCISSOR:
cout << "Tie game!" << endl;
break;
case ROCK_PAPER:
cout << "Paper covers rock, Player 2 wins!" << endl;
break;
case ROCK_SCISSOR:
cout << "Rock breaks scissors, Player 1 wins!" << endl;
case PAPER_ROCK:
cout << "Paper covers rock, Player 1 wins!" << endl;
break;
case PAPER_SCISSOR:
cout << "Scissors cut paper, Player 2 wins!" << endl;
break;
case SCISSOR_ROCK:
cout << "Rock breaks scissors, Player 2 wins!" << endl;
break;
case SCISSOR_PAPER:
cout << "Scissors cut paper, Player 1 wins!" << endl;
break;
}
}
catch (int errID) {
cout << "Error: " << errID << endl;
}
}
while (getInput("Do you want to play again? Y/N") == 'Y');
return 0;
}
Related
I'm doing a school project that creates a blackjack game, but unfortunately, I'm stumped at a few errors I must have made earlier on. The errors appear on lines 165 and 173. It states that I'm missing several elements that I cannot piece together. Could someone help inform me of what's wrong with this program that won't compile?
#include <iostream>
#include <string>
#include <ctime>
#include <limits>
using namespace std;
int getCard()
{
return rand() % 10 + 1;
}
char readChar()
{
char c;
cin >> c;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return c;
}
int main()
{
//set srand to seed system clock
//to generate random number
srand((int)time(0));
char yesno = 'y';
do
{
int dealer = getCard();
int first = getCard(), second = getCard();
int total = first + second;
if (yesno == 'y' || yesno == 'Y')
{
cout << "The dealer starts with a " << dealer << endl;
cout << "Your first cards: " << first << ", " << second << endl;
cout << "Total: " << total << endl;
do
{
cout << "hit? (y/n): ";
yesno = readChar();
if (yesno == 'y' || yesno == 'Y')
{
int newCard = getCard();
total += newCard;
cout << "Card: " << newCard << endl;
cout << "Total: " << total << endl;
if (total > 21)
{
cout << "Bust" << endl;
yesno = 'n';
break;
}
else if(total == 21)
{
cout << "Blackjack" << endl;
}
else if (yesno == 'n' || yesno == 'N')
{
//Dealer must take more cards if less than 17
while (dealer < 17)
{
cout << "Dealer has a " << dealer << "..." << endl;
char c;
do {
cout << "(c to continue) ";
c = readChar();
} while (c != 'c' && c != 'C');
int newDealerCard = getCard();
cout << "Dealer gets a " << newDealerCard << endl;
dealer += newDealerCard;
cout << "Total: " << dealer << endl;
}
//determine winner
if (dealer == 21 && dealer != total)
cout << "Lose" << endl;
else if (dealer == total)
cout << "Push" << endl;
else if (dealer > 21 && total > dealer && total < 21)
cout << "Win" << endl;
else if (dealer > total && dealer <= 21)
cout << "Lose" << endl;
break;
}
} while (yesno != 'n' && yesno != 'N');
}
cout << "Would you like to play again? (y/n) : ";
yesno = readChar();
} while (yesno != 'n' && yesno != 'N');
return 0;
}
Since I was a bit lost at the end, I wasn't sure where to add my missing elements.
There are at least two do {...} while (...) loops in there that do not have while clauses.
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;
}
}
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));