I'm not sure how far I have left to go with my coding, but after I enter my play then I want the computer to play, it just repeats its play over and over until I control c to get out.

#include <iostream>
using namespace std;

// Leave the following line it. It's needed to make the compiler happy.
char cPlayRPS ();

// Add comment here re overall purpose of the program.
int main () {
// Declare variables to store the user's and the computer's play.
char userPlay;
int wins;
int losses;
int draws;		
char cPlay;
	
// Also, declare and initialize variables to save the number of wins, losses, and draws.
// Prompt the user to make a play, and input it.	

cout << "You Play (P for Paper, R for Rocks, S for Scissors or Q for Quit): ";
cin >> userPlay;
cout << "Computer Play: ";
cout << endl;

// Set up a loop to play a round, checking that user entered a valid choice.
while(userPlay != 'Q' && userPlay != 'q')
{
	cout << "Computer Play: ";
	cPlay = cPlayRPS();
	cout << cPlay << endl;
	
	if(userPlay== 'R' && cPlay=='P')
	{	 
		cout << "You win:  Paper covers Rock";
		wins=wins+1;
	}	 
	else if(userPlay=='R' && cPlay=='S')
	{
		cout << "You win: Rock crushes Scissors";
		wins=wins+1;
	}	 
	else if(userPlay=='R' && cPlay=='R')
	{
		cout << "Draw";
		draws=draws +1;
	}	 
	else if(userPlay =='P' && cPlay =='R')
	{
		cout << "You win: Paper covers Rock";
		wins=wins+1;
	}	 
	else if(userPlay =='P' && cPlay=='S')
	{
		cout << "You lose: Scissors cuts Paper";
		losses=losses+1;
	}	 
	else if(userPlay=='P' && cPlay=='P')
	{
		cout << "Draw";
		draws=draws+1;
	}	 
	else if(userPlay=='S' && cPlay=='P')
	{
		cout << "You win: Scissors cuts Paper";
		wins=wins+1;
	}	 
	else if(userPlay=='S' && cPlay=='R')
	{
		cout << "You lose: Rock crushes Scissors";
		losses=losses+1;
	}	 
	else if(userPlay=='S' && cPlay=='S')
	{
		cout << "Draw";
		draws=draws+1;
	}	 
}

	cout << "Results: " << wins << losses << draws << endl;
	cout << "All done. Thanks for playing.";

	return 0;
}

char cPlayRPS () {
	srand( time( NULL));
	switch ( rand() % 3 ) {
		case 0: return 'R';
		case 1: return 'P';
		case 2: return 'S';
	}
}

Recommended Answers

All 5 Replies

The program should allow the user to enter his play in upper or lower case letters. Should I go through my while and use the tolower() function?

Yes. Or just deal with the upper and lower cases like you did with everything else.

i.e.

if( (userPlay== 'R' || userPlay == 'r') &&  
            (cPlay == 'P' || cPlay=='p'))

Is there a way to do it without going through each one of my conditions?
P.S. I figured out the problem with my loop so thats good now

You could have nested "switch" statements. i.e.

switch(userPlay)
{
   case 'r':
       switch(cPlay)
      {
         case 'r':
               cout << "Draw";
	       draws=draws +1;
               break;
         ...
         ...
         ...
      }
   ...
   ...
   ...
}

Use the toupper() function from ctype.h:

else if (toupper(userPlay) == 'P' && toupper(cPlay) == 'R')

Also, you should only call srand() once, so move it to the beginning of main()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.