I am a first year IT student cramming my last assignments in before the monday afternoon deadline. I am battling with a problem that looks like this: I have to create a program using the case statements to choose a winner from rock, paper, scissors and count the wins,losses,ties to be displayed after each play. the problem is how do I loop the switch. Also, any ideas on how I should count score using variables?
I would really appreciate any help, C++ and I aren't real friendly yet but it will be my language of choice when I graduate.
Here is my working code but I the program doesn't loop after one game. I want to play an many as I want while the game keeps track of the score until I type 'quit'.
// include libraries
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// declare variables
int computerChoice;
char userChoice;
char wins = 0;
char losses = 0;
char ties = 0;
// seed the random number generator with the current time of day
// to guarantee a different sequence of random numbers each time.
srand( (int) time(0) );
// clear the screen and display the title
system("cls");
cout << " Rock, Paper, Scissors" << endl
<< " Roshambo" << endl << endl;
// loop while userChoice is not equal to quit
while (userChoice != quit)
{
// determine the computer's choice. (0=ROCK , 1=PAPER, 2=SCISSORS)
computerChoice = rand() % 3;
// ask the user for his or her choice
cout << "Enter R for rocks or P for paper or S for scissors or press type QUIT to end game: ";
cin >> userChoice;
// create a case for each possible player choice
switch (userChoice)
{
case 'r': // user throws ROCK
case 'R':
// create a case for each possible computer choice
switch (computerChoice)
{
case 0: // Computer picks ROCK
cout << "Rock ties rock, we tie!" << endl;
break;
case 1: // Computer picks PAPER
cout << "Paper covers rock, you lose." << endl;
break;
case 2: // Computer picks SCISSORS
cout << "Rock smashes scissors, you win!" << endl;
break;
}
break;
case 'P': // user throws PAPAER
case 'p':
// create a case for each possible computer choice
switch (computerChoice)
{
case 0: // Computer picks ROCK
cout << "Paper covers rock, you win." << endl;
break;
case 1: // Computer picks PAPER
cout << "Paper ties paper, we tie!" << endl;
break;
case 2: // Computer picks SCISSORS
cout << "Scissors cut paper, you lose" << endl;
break;
}
break;
case 's': // user throws SCISSORS
case 'S':
// create a case for each possible computer choice
switch (computerChoice)
{
case 0: // Computer picks ROCK
cout << "Rock smashes scissors, you lose." << endl;
break;
case 1: // Computer picks PAPER
cout << "Scissors cust paper, you win!" << endl;
break;
case 2: // Computer picks Scissors
cout << "Scissors ties scissors, we tie!" << endl;
break;
}
break;
default: // user entered something else.
cout << "Sorry, I can't interpret the character " << userChoice;
cout << endl;
} // end case for userChoice
} // end of loop
return 0;
}