Share
Explore BrainMass

Debugging Code

Please look through the code and debug and add anything into it. Make sure it says if the letter is invalid. I am an online learner. The book does not tell me everything. .

// ********************************************************************
//
// PaperRockScissors.cpp
//
// This program simulates the game of paper, rock, scissors. At
// each turn each player enters R, P, or S (either upper or lower
// case) to indicate their choice. The program announces the winner
// along with a message indicating the reason (such as Paper covers
// rock).
//
// ********************************************************************

#include <iostream>
using namespace std;

void printWelcome();
// Postcondition: A welcome and brief instructions are printed to the screen.
char CheckUserInput();

int main (int argc, char *argv[])
{
//
// Variable Declarations
//
char play1Choice, play2Choice;
char playAgain, charUserInput;

charUserInput = play1Choice, play2Choice;

//
// Print a welcome
//
printWelcome();

//
// Play the game as long as the users wish
//
do {

//
// Nested multiway if to determine the winner and update the score
//
cout << "Player 1 please press p for paper, s for scissor and r for rock.n";
cin >> play1Choice;

cout << "Player 2 please press p for paper, s for scissor and r for rock.n";
cin >> play2Choice;

if ((play1Choice == 'P')||(play1Choice == 'p'))
if ((play2Choice == 'P')||(play2Choice == 'p'))
{
cout << "Nobody wins.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else if ((play2Choice == 'S')||(play2Choice == 's'))
{
cout << "Player 2 wins. Scissors cut paper.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else
{
cout << "Player 1 wins. Paper covers rock.n";
cout << "Do you wan to play again?: ";
cin >> playAgain;
}
else if ((play1Choice == 'R')||(play1Choice == 'r'))
if ((play2Choice == 'R')||(play2Choice == 'r'))
{
cout << "Nobody wins.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else if ((play2Choice == 'P')||(play2Choice == 'p'))
{
cout << "Player 2 wins. Paper covers rock. n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else
{
cout << "Player 1 wins. Rock breaks scissors.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else if ((play1Choice == 'S')||(play1Choice == 's'))
if ((play2Choice == 'S')||(play2Choice == 's'))
{
cout << "Nobody wins.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else if ((play2Choice == 'R')||(play2Choice == 'r'))
{
cout << "Player 2 wins. Rock breaks scissor.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
else
{
cout << "Player 1 wins. Scissors cut paper.n";
cout << "Do you want to play again?: ";
cin >> playAgain;
}
}
while (playAgain == 'Y' || playAgain == 'y');

system("PAUSE");
return EXIT_SUCCESS;
}

void printWelcome()
{
cout << endl << endl;
cout << "Welcome to the Paper, Rock, Scissors Game!!!" << endl;
cout << "============================================" << endl;
cout << endl;
cout << "At each turn, both players enter a letter indicating";
cout << " their choice - " << endl;
cout << "P or p denotes Paper, R or r denotes Rock, S or s denotes Scissors."
<< endl << endl;
cout << "Start playing ... " << endl << endl;

}

char CheckUserInput(char charUserInput)
{
return(toupper(charUserInput)!='P'||'R'||'S')
return false;
else
return true;
}

Attachments

Solution Preview

I made some changes for validating user input.

//

// ********************************************************************
//
// PaperRockScissors.cpp
//
// This program simulates the game of paper, rock, scissors. At
// each turn each player enters R, P, or S (either upper or lower
// case) to indicate their choice. The program announces the winner
// along with a message indicating the reason (such as Paper covers
// rock).
//
// ********************************************************************

#include "stdafx.h"
#include <iostream>
using namespace std;

void printWelcome();
// Postcondition: A welcome and brief instructions are printed to the screen.
bool CheckUserInput(char inputLetter);

int main(int argc, char* argv[])
{
//
// Variable Declarations
//
char play1Choice, play2Choice;
char playAgain, charUserInput;

charUserInput = play1Choice, play2Choice;

//
// Print a welcome
//
printWelcome();

//
// Play the game as long as the users wish
//
do {

//
// Nested multiway if to determine the ...

$2.19