Hi everybody, i cant seem to make my program to pass the first user input. I know i have to put it inside a do while loop, but everyone of my attempts results in an infinite loop. I'm also aware that i should use a bool to make it work, but i've spent a couple of hours trying to figure it out and still cant =(
If anybody could help me out, i would really appreciate it. Thanks guys!
//Will Rodriguez
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
// Function
void Winner(int choice1, int choice2);
void menu();
int main(int argc, char *argv[])
{
cout << "Welcome to the Greatest Game of all Times:\n" //Introduction to the program
<< "\tRock Papers & Scissors!\n\n"
<< " You will play against the computer\n"
<< " you gain one point for every win\n"
<< " the game goes to 10, so try your best!\n"
<< "------------------------------------------\n\n";
menu(); //Show Menu
char playerChoice;
cout<<"Enter your choice: ";
cin>>playerChoice;
switch(playerChoice)
{
case 'r': case 'R':
cout<<"Player: Rock"<<endl;
playerChoice = 0;
break;
case 'p': case 'P':
cout<<"Player: Paper"<<endl;
playerChoice = 1;
break;
case 's': case 'S':
cout<<"Player: Scissors"<<endl;
playerChoice = 2;
break;
case 'q': case 'Q':
exit(0);
default:
cout<<"Play option does not exist!"<<endl;
}
srand((unsigned)time(0));
int compChoice = rand()%3;
switch(compChoice)
{
case 0:
cout<<"Computer: Rock"<<endl;
break;
case 1:
cout<<"Computer: Paper"<<endl;
break;
case 2:
cout<<"Computer: Scissors"<<endl;
break;
default:
cout<<"Program is not functioning properly."<<endl;
}
Winner(playerChoice, compChoice);
system("PAUSE");
return 0;
}
void menu() //Menu
{
cout <<"(R)ock\n";
cout <<"(P)aper\n";
cout <<"(S)cissors\n";
cout <<"----------\n";
}
void Winner(int choice1, int choice2)
{
int playerWin = 0;
int compWin = 0;
if(choice1 == 0 && choice2 == 0 || choice1 == 1 && choice2 == 1 || choice1 == 2 && choice2 == 2)
{
cout <<"I'ts a tie!\n";
}
else if(choice1 == 0 && choice2 == 1)
{
cout <<"Paper covers rock!\n"
<<"You lose!\n";
compWin++;
}
else if(choice1 == 1 && choice2 == 0)
{
cout <<"Paper covers rock!\n"
<<"You win!\n";
playerWin++;
}
else if(choice1 == 0 && choice2 == 2)
{
cout <<"Rock crushes scissors!\n"
<<"You win!\n";
playerWin++;
}
else if(choice1 == 2 && choice2 == 0)
{
cout <<"Rock crushes scissors!\n"
<<"You lose!\n";
compWin++;
}
else if(choice1 == 1 && choice2 == 2)
{
cout <<"Scissors cut paper!\n"
<<"You lose!\n";
compWin++;
}
else if(choice1 == 2 && choice2 == 1)
{
cout <<"Scissors cut paper!\n"
<<"You win!\n";
playerWin++;
}
cout <<"\n"
<<"Score is\nPlayer:" <<playerWin <<endl
<<"Computer:" <<compWin <<endl;
}