1

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.

Richard Paulicelli
  • 129
  • 3
  • 6
  • 23

4 Answers4

3

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";
}
Bucket
  • 7,415
  • 9
  • 35
  • 45
  • Ahhh, haha, thanks, stupid me. Just one of those simple syntax errors. Thanks a bunch! I'll make sure to look for that kind of stuff in the future! – Richard Paulicelli Sep 26 '13 at 04:33
  • 3
    It happens to the best of us. I remember back in college my professors would recommend that we switch the order of our comparisons, like `42 == foo`, just so we wouldn't run into this same blunder. It works, but for English speakers, it's not the most intuitive. Your call. :) – Bucket Sep 26 '13 at 04:34
3

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');
user2804925
  • 247
  • 3
  • 11
1

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');
mvp
  • 111,019
  • 13
  • 122
  • 148
-1

//Syntax you're looking for is this:

char keep_going;
do {
//some statements
} while(cin.get(keep_going));
rama41222
  • 65
  • 1
  • 7