Hello everyone, I'm having trouble with the string input for a rock, paper, scissors game and was wondering if someone could help me out? Whenever I run the program and input a choice such as rock, paper, or scissor, nothing happens. Instead, the program just prints out the beginning prompt.
Here is the assignment:
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows.
1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.)
2. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer.)
3. The computer's choice is displayed.
4. A winner is selected and displayed according to the following rules:
If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.)
If one player chooses scissors and the other player chooses paper, then scissors wins. (The scissors cuts paper.)
If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)
If both players make the same choice, the game must be played again to determine the winner
Be sure to divide the program into functions that perform each major task.
Make sure to allow the user to play the game over and over if he wishes.
Here is the code that I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
srand((unsigned)time(0));
string userChoice;
int user;
int compchoice = (rand()%2)+1;
cout << "Welcome to Rock, Paper, Scissors I assume you know how to play,\n";
cout << " so lets begin. You are playing against the computer.\n";
cout<<" Please input either rock, paper, or scissors.\n\n";
cin>>userChoice;
if (userChoice == "rock")
{user=0;}
else if (userChoice=="paper")
{user=1;}
else if (userChoice=="scissors")
{user=2;}
if (user==0)
{
if (compchoice==0)
{cout<<endl<<"Computer chose rock"<<endl;
cout << "It's a tie!\n\n\n\n";
}
else if (compchoice == 1)
{ cout<<endl<<"Computer chose paper"<<endl;
cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
}
else if (compchoice == 2)
{
cout<<endl<<"Computer chose scissors" <<endl;
cout << "Rock beats scissors! You win!\n\n\n\n";
}
}
if (user == 1)
{
if (compchoice=0)
{cout<<endl<<"Computer chose paper"<<endl;
cout << "Paper beats rock! You win!\n\n\n\n";
}
else if (compchoice == 1)
{
cout<<endl<<"Computer chose paper"<<endl;
cout << "It's a tie!\n\n\n\n";
}
else if (compchoice == 2)
{
cout<<endl<<"Computer chose scissors"<<endl;
cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
}
}
if (user == 2)
{
if (compchoice==0)
{cout<<endl<<"Computer chose rock"<<endl;
cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
}
else if (compchoice == 2)
{cout<<endl<<"Computer chose scissors"<<endl;
cout << "It's a tie!\n\n\n\n";
}
else if (compchoice == 1)
{cout<<endl<<"Computer chose paper"<<endl;
cout << "Scissors beat paper! You win!\n\n\n\n";
}
}
return main();
}
|
I've also tried using strcmp but nothing so far has worked. Can someone please give me suggestions as to how this problem can be resolved? Thanks.