expression must be a modifiable lvalue. Codecademy - c++

Can someone help me fix this, I'm doing the Learn C++ class on Codecademy and it says, "expressions must be a modifiable lvalue." It says this inside every if statement that I wrote. It's the last number in all of them. My guess would be that there is a problem with the logical operators in them but I'm not sure how to fix them. Any help would be greatly appreciated.
#include <iostream>
#include <stdlib.h>
int main() {
srand (time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "=================================\n";
std::cout << "rock paper scissors lizard spock!\n";
std::cout << "=================================\n";
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
std::cout << "4) Lizard\n";
std::cout << "5) Spock\n";
std::cout << "shoot!\n";
std::cin >> user;
if(computer == 1 && user == 2 || user = 5) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You win!\n";
}
else if (computer == 1 && int user == 3 || int user == 4){
std::cout << "Computer Chose Rock!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Rock!\n";
std::cout << "Tie!\n";
}
if(computer == 2 && user == 3 || user = 4) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You win!\n";
}
else if (computer == 2 && user == 1 || user == 5){
std::cout << "Computer Chose Paper!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Paper!\n";
std::cout << "Tie!\n";
}
if(computer == 3 && user == 1 || user = 5) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You win!\n";
}
else if (computer == 3 && user == 4 || user == 2){
std::cout << "Computer Chose Scissors!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Scissors!\n";
std::cout << "Tie!\n";
}
if(computer == 4 && user == 1 || user = 3) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You win!\n";
}
else if (computer == 4 && user == 2 || user == 5){
std::cout << "Computer Chose Lizard!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Lizard!\n";
std::cout << "Tie!\n";
}
if(computer == 5 && user == 4 || user = 2) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You win!\n";
}
else if (computer == 5 && user == 3 || user == 1){
std::cout << "Computer Chose Spock!\n";
std::cout << "You lose.\n";
}
else{
std::cout << "Computer Chose Spock!\n";
std::cout << "Tie!\n";
}
}

First of all, you should be careful with == or != in if statements. Then you already specified that user is int and you can use it in if statements without its type because compiler thinks that you are trying to declare it again.You also can't use time(NULL) without including ctime library.
#include <iostream>
#include <stdlib.h>
#include <ctime>
int main() {
srand(time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "=================================\n";
std::cout << "rock paper scissors lizard spock!\n";
std::cout << "=================================\n";
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
std::cout << "4) Lizard\n";
std::cout << "5) Spock\n";
std::cout << "shoot!\n";
std::cin >> user;
if (computer == 1 && user == 2 || user == 5) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You win!\n";
}
else if (computer == 1 && user == 3 || user == 4) {
std::cout << "Computer Chose Rock!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Rock!\n";
std::cout << "Tie!\n";
}
if (computer == 2 && user == 3 || user == 4) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You win!\n";
}
else if (computer == 2 && user == 1 || user == 5) {
std::cout << "Computer Chose Paper!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Paper!\n";
std::cout << "Tie!\n";
}
if (computer == 3 && user == 1 || user == 5) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You win!\n";
}
else if (computer == 3 && user == 4 || user == 2) {
std::cout << "Computer Chose Scissors!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Scissors!\n";
std::cout << "Tie!\n";
}
if (computer == 4 && user == 1 || user == 3) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You win!\n";
}
else if (computer == 4 && user == 2 || user == 5) {
std::cout << "Computer Chose Lizard!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Lizard!\n";
std::cout << "Tie!\n";
}
if (computer == 5 && user == 4 || user == 2) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You win!\n";
}
else if (computer == 5 && user == 3 || user == 1) {
std::cout << "Computer Chose Spock!\n";
std::cout << "You lose.\n";
}
else {
std::cout << "Computer Chose Spock!\n";
std::cout << "Tie!\n";
}
}

In addition to what was already said (you should be careful not to confuse user = 5 and user == 5; you should not use the keyword int, because the compiler thinks it's a declaration), there is one another very important issue.
You have to learn about operator precedence. The logical AND has higher priority than OR and will be evaluated first:
computer == 1 && user == 2 || user == 5
is equivalent to
(computer == 1 && user == 2) || user == 5
which means if user == 5 holds true, the whole statement will return true. Even if computer != 1. And your code will output "computer chose rock", which is incorrect.
Change it to
computer == 1 && (user == 2 || user == 5)
And as a general rule, that many similar if statements is a very poor choice. I hope they cover that later in your course. Good luck!

Related

I keep getting the error "‘else’ without a previous ‘if’" and don't know why

I started c++ a few days ago and when ever I write an else if statement I always get the error ‘else’ without a previous ‘if’. Here is my code (I'm trying to make rock paper scissors between users btw. I want to eventually add lizard and spock too from the big bang show.):
/*
This program will play rock paper scissors with the user
*/
#include <iostream>
#include <stdlib.h>
int main() {
srand(time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "===========================\nrock paper scissors!\n===========================\n";
std::cout << "1) ROCK\n";
std::cout << "2) PAPER\n";
std::cout << "3) SCISSORS\n";
std::cout << "shoot! \n";
std::cin >> user;
/*
if (user == 1) {
std::cout << "You pick rock!\n";
}
else if (user == 2) {
std::cout << "You pick paper!\n";
}
else if (user == 3) {
std::cout << "You picked scissors!\n";
}
else {
std::cout << "Invalid input, please select a number between 1 and 3\n";
*/
if (computer == 1)
std::cout << "CPU picks Rock!";
else if (computer == 2)
std::cout << "CPU picks Paper!";
else
std::cout << "CPU picks Scissors!";
if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2)
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 3)
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
}
I tried adding and removing curly brackets. (I heard they were optional though) I tried switching else if to if else because I always forget which one to use. I also tried checking to make sure I didn't use any semi colons because it seems that's always a big issue. I'm still learn though and I'm very new to c++, so go easy on me.
Some problematic lines from your code:
if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2)
Now using intendation to illustrate how this is parsed:
if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2) // <-------------------------
The brackets are optional when there is only a single statement, as for example
if (foo) {
std::cout << blabla;
}
// same as
if (foo) std::cout << blabla;
But once there is more, the brackets make a difference. Or put differently, the following two are equivalent:
if (foo)
std::cout << blabla;
std::cout << whoops;
// is the same as
if (foo) {
std::cout << blabla;
}
std::cout << whoops;
It is better to always write the brackets even in cases they can be left out, because otherwise, adding or removing a single line requires to refactor a whole block of code.
Each else part belongs to the most inner if statement.
So in this code snippet
if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2)
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 3)
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
For example this else statement
else if (user == 2)
is wrong because there is a preceded if statement with the else part
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
You need to place the inner if statements in a compound statement as for example
if (user == 1)
{
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
}
else if (user == 2)
{
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
}
else if (user == 3)
{
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
}
else
{
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
}
Pay attention to that you could use switch statement the following way
switch (user)
{
case 1:
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
case 2:
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
case 3:
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
default:
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
break;
}
This makes your code more readable.
And to make the code even more readable you could introduce an enumeration the following way
enum { Rock = 1, Paper = 2, Scissors = 3 };
//...
switch (user)
{
case Rock:
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
case Paper:
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
case Scissors:
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
break;
default:
std::cout << "Invalid input, please select a number between " << Rock << " and " << Scissors << " (No Spaces please!)\n";
break;
}
Others here have provided very good answers, but here's my personal take on things. Don't exclude the brackets.
For single statements, you can always place them in-line, but make sure your code doesn't look messy.
The lacking brackets made my editor have a fit, but I think I indented and bracketed this correctly.
This is the same issue the compiler was having, too.
/*
This program will play rock paper scissors with the user
*/
#include <iostream>
#include <stdlib.h>
int main() {
srand(time(NULL));
int computer = rand() % 3 + 1;
int user = 0;
std::cout << "===========================\nrock paper scissors!\n===========================\n";
std::cout << "1) ROCK\n";
std::cout << "2) PAPER\n";
std::cout << "3) SCISSORS\n";
std::cout << "shoot! \n";
std::cin >> user;
if (computer == 1) {
std::cout << "CPU picks Rock!";
} else if (computer == 2) {
std::cout << "CPU picks Paper!";
} else {
std::cout << "CPU picks Scissors!";
}
if (user == 1) {
std::cout << "You pick rock!\n";
if (computer == 1) {
std::cout << "You both tie!\n";
} else if (computer == 2) {
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
} else {
std::cout << "You win!\n";
} else if (user == 2) {
std::cout << "You pick paper!\n";
if (computer == 2) {
std::cout << "You both tie!\n";
} else if (computer == 3) {
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
} else {
std::cout << "You win!\n";
}
} else if (user == 3) {
std::cout << "You picked scissors!\n";
if (computer == 3) {
std::cout << "You both tie!\n";
} else if (computer == 1) {
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
} else {
std::cout << "You win!\n";
}
} else {
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
}
}
}
Following on to my comment, you're doing too much work to figure out the outcome.
if (choice == cpuChoice) {
std::cout << "You tied.\n";
} else if ((choice == ROCK && cpuChoice == SCISSORS) ||
(choice == PAPER && cpuChoice == ROCK) ||
(choice == SCISSORS && cpuChoice == PAPER)) {
std::cout << "You won!\n";
} else {
std::cout << "You lost...\n";
}
Using the enum from the chosen answer, and the logic I suggested in my comment, this is how you determine the outcome of the match without repeating yourself and checking every single variation.
Talk it out. There are three outcomes: win, lose, and tie. Checking a tie is super simple. To know if you won, there are only three possible combinations. Check for one of them. Otherwise, you have to have lost. There is no need to explicitly check if you lost, it's the only possibility remaining.
My entire program was 50 lines of code, and half of it was just the enum and creating a function to print the enum values as a string. It should be longer, though. I skipped things like input validation, and I used a 'raw' enum instead of an enum class.

Nothing happening when I call my function?

I'm trying to make a function for a rock, paper, scissors game with two char parameters where the first one represents the user's choice of rock, paper, or scissors. The second parameter represents the result of the game, either win, loss, or tie. When I tried to call the function, however, nothing is happening. I'm lost on what exactly I need to do next. All help is greatly appreciated!
#include <iostream>
#include <cstdlib>
using namespace std;
double playRPS (char a, char b);
int main() {
char letter;
char result = 0;
cout << "Welcome to COP3014 ROCK PAPER SCISSORS!\n\n";
cout << "Please select: " << endl
<< "Rock(r), Paper(p), or Scissors(s)? " << endl
<< "Or enter q to quit --> ";
cin >> letter;
if (letter == 'r' || letter == 'R' || letter == 'p' || letter == 'P' || letter == 's' || letter == 'S') {
playRPS(letter, result);
}
else {
cout << "Please enter r, p, or s" << endl;
}
return 0;
}
double playRPS (char x, char y) {
int choice1 = 0, choice2 = 0, choice3 = 0;
int user2 = rand() % 3 + 1;
if (( x == 'r' || x == 'R') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose ROCK!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if ((x == 'r' || x == 'R') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose ROCK!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if ((x == 'r' || x == 'R') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose ROCK!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose PAPER!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else if (( x == 'p' || x == 'P') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose PAPER!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 'p' || x == 'P') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose PAPER!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '2')) {
cout << "The computer chose... PAPER!";
cout << "You chose SCISSORS!";
cout << "You WIN!";
y = choice1;
return choice1;
}
else if (( x == 's' || x == 'S') && (user2 == '1')) {
cout << "The computer chose... ROCK!";
cout << "You chose SCISSORS!";
cout << "You LOSE!";
y = choice2;
return choice2;
}
else if (( x == 's' || x == 'S') && (user2 == '3')) {
cout << "The computer chose... SCISSORS!";
cout << "You chose SCISSORS!";
cout << "You TIED!";
y = choice3;
return choice3;
}
else{
return main();
}
General remarks
using namespace std;
Avoid using namespace std.
return main();
You are not allowed to call main in your code. This will result in Undefined Behavior. Plus, what is your intention here?
rand()
rand() should be avoided. Here is an interesting video on why you should not use it, and instead use C++11 random.
y = choice2;
You are passing y by value, which means assigning it won't modify the y from the outside. You should pass y by reference when doing this (i.e. char& y in the declaration).
Why is the function not doing anything?
... Actually, it does!
user2 == '2'
Your comparisons are broken. '2' is actually not 2, but 50. The reason is that '2' is a character, so you actually are reading the associated character code.
This means all of your conditions are false in playRPS, so the only thing the function does it to call main() (in return main();).
What about shortening your code?
Your test cases are quite redundant and heavy. You could change it to drastically cut down your code size.
Let's print what the choice selected by the player...
if (x == 'r' || x == 'R')
cout << "You chose ROCK!" << endl;
else if (x == 'p' || x == 'P')
cout << "You chose PAPER!" << endl;
else if (x == 's' || x == 'S')
cout << "You chose SCISSORS!" << endl;
All good! Let's do the same with the computer's choice!
if (user2 == 1)
cout << "The computer chose... ROCK!" << endl;
else if (user2 == 2)
cout << "The computer chose... PAPER!" << endl;
else if (user2 == 3)
cout << "The computer chose... SCISSORS!" << endl;
Then you should compare what the player chose to what the computer chose, and tell who is the winner. Unfortunately, we can't compare x to user2 without doing many cases again...
What if we decided to have x's choice being saved the same way as user2? We also can use tolower to avoid checking for the caps variant of the letter.
int user1 = 0;
x = tolower(x); // we force x to lower case
if (x == 'r')
user1 = 1;
else if (x == 'p')
user1 = 2;
else if (x == 's')
user1 = 3;
Good! Now we can also improve conditions in our first if/else if block:
if (user1 == 1)
cout << "You chose ROCK!" << endl;
else if (user1 == 2)
cout << "You chose PAPER!" << endl;
else if (user1 == 3)
cout << "You chose SCISSORS!" << endl;
Which means we also can compare user1 to user2 so we know who won.
if (user1 == user2) {
cout << "It's a TIE!" << endl;
}
else if ((user1 == 1 && user2 == 2) ||
(user1 == 2 && user2 == 3) ||
(user1 == 3 && user2 == 1)) {
cout << "You LOSE!" << endl;
}
else {
cout << "You WIN!" << endl;
}
However, using 1, 2 and 3 does not make things very clear. What if you used an enum to represent these values?
enum RPSChoice
{
ROCK = 1,
PAPER = 2,
SCISSORS = 3
};
For example, the first block now looks like:
if (user1 == ROCK)
cout << "You chose ROCK!" << endl;
else if (user1 == PAPER)
cout << "You chose PAPER!" << endl;
else if (user1 == SCISSORS)
cout << "You chose SCISSORS!" << endl;
What if we wrapped our new two first blocks into a function so we avoid repeating ourselves?
void printDecision(string who, int choice) {
cout << who; // no matter what, we will tell who took a decision
if (choice == ROCK)
cout << " chose ROCK!" << endl;
else if (choice == PAPER)
cout << " chose PAPER!" << endl;
else if (choice == SCISSORS)
cout << " chose SCISSORS!" << endl;
}
This way, we can make playRPS even more clear, by replacing the two large blocks into simple, short function calls:
printDecision("You", user1);
printDecision("The computer", user2);
Let's do another simple function that decides who won:
int winner(int user1, int user2) {
if (user1 == user2) {
return 0; // tie
}
else if ((user1 == ROCK && user2 == PAPER) ||
(user1 == PAPER && user2 == SCISSORS) ||
(user1 == SCISSORS && user2 == ROCK)) {
return 2; // user2 is the winner
}
else {
return 1; // user1 is the winner
}
}
And a final one that returns the value we give according to a given character:
int characterToChoice(char c)
{
c = tolower(c);
if (c == 'r')
return ROCK;
else if (c == 's')
return SCISSORS;
else if (c == 'p')
return PAPER;
else
return 0; // Not a proper choice!
}
Done! This is the final program with all improvements in (nothing done to replace rand() in), and here is an online prompt to try it out.
Note that there are more ways you can improve the code, to simplify it even more and to make it more clear. I am most notably thinking about std::unordered_map to bind a RPSChoice value to a string, and a char to a RPSChoice. You may also prefer switch to if in some cases.
As stated by the comments to your question, you could have diagnosed this issue using a debugger. πάντα ῥεῖ's comment for reference:
The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert).

C++ Functions and strange errors

So I'm writing a small Rock, Paper, Scissors game structure in C++ and I've run into some errors I don't understand.
Solution
The function string numberToWord (int x) can't be in the function main. It has to be a separate method due to the way the compiler works. I simply moved it out then it worked fine.
Previous Question
So I'm writing a small Rock, Paper, Scissors game structure in C++ and I've run into some errors I don't understand.
The first is the code expects a ';' at the NumberToWord function but it shouldn't since it's a function.
Another error is randomly one of the else statements it doesn't seem to like.
Maybe I'm missing something, I don't know but it should be a simple fix.
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int seed = static_cast <int> (time(0)); //Sets the random seed
srand(seed);
int winCount = 0;
string numberToWord (int x) {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
}
while (winCount < 3) {
int computerChoice = rand() % 4;
int userChoice;
cout << userChoice << endl;
cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input
cin >> userChoice; //Inputs user input to variable
if (userChoice == computerChoice) {
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Draw!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
}
return 0;
}
Thanks for any and all help!
Part 2
Simply put the program doesn't like the '<<'. I use this just fine in many other programs for variables but this time when I used a string variable it throws an error. I looked up C++ string variables and it looks like I'm doing it correctly so I don't know the reason for the errors.
References:
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/variables/
void displayOutput(int comp, int user, string winner) {
string compOutputChoice = "";
string userOutputChoice = "";
/*
if (comp == 0) { compOutputChoice = "Rock"; }
else if (comp == 1) { compOutputChoice = "Paper"; }
else if (comp == 2) { compOutputChoice = "Scissors"; }
if (user == 0) { userOutputChoice = "Rock"; }
else if (user == 1) { userOutputChoice = "Paper"; }
else if (user == 2) { userOutputChoice = "Scissors"; }
*/
cout << "Compuer Choose: " << compOutputChoice << endl;
cout << "You Choose: " << userOutputChoice << endl;
//cout << winner << endl;
return;
}
Errors:
Error (active) no operator "<<" 32
Error (active) no operator "<<" 33
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 32
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 33
The function string numberToWord (int x) is nested inside the main function. That is not valid C++.
The GCC compiler does support nested functions as an extension, but it's not part of the standard and other compilers (that I know of) don't accept it. Just don't do that. Move the function out of main (or, if it makes sense, make it a lambda).
The problem is simple. numberToWord cannot be an internal function of main. Move it outside main or change it to a lambda if you are using a newer C++.
auto numberToWord = [](int x) -> string {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
};

C++ Nested Validation loop

My teacher would like me to put a "nested validation loop around the player's choice. That keeps looping until they enter valid input (1, 2, or 3)." I am having trouble getting anything to work could I get some pointers, please and thank you.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
// Keaton Graffis 12/28/2015
int main()
{
int seed = time(0);
srand(seed);
char playAgain;
int playerChoice, aiChoice, win = 0, tie = 0, lose = 0;
do
{
cout << "Lets play a game of rock paper scissors.\n";
cout << "Enter a 1 for sccisors a 2 for rock or a 3 for paper: ";
// Generates outcomes of the players choice
cin >> playerChoice;
if (playerChoice == 1)
{
cout << "You picked Rock!\n";
}
else if (playerChoice == 2)
{
cout << "You picked Paper!\n";
}
else if (playerChoice == 3)
{
cout << "You picked Scissors!\n";
}
// gentrate the computers choices
int aiChoice = rand() % 3 + 1;
if (aiChoice == 1)
{
cout << "The computer chose Rock!\n";
}
else if (aiChoice == 2)
{
cout << "The computer chose Paper!\n";
}
else if (aiChoice == 3)
{
cout << "The computer chose Scissors!\n";
}
// Determines wins, ties and loses
if (playerChoice == 1 && aiChoice == 1) {
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if (playerChoice == 1 && aiChoice == 2)
{
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if (playerChoice == 1 && aiChoice == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if (playerChoice == 2 && aiChoice == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if (playerChoice == 2 && aiChoice == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if (playerChoice == 2 && aiChoice == 3)
{
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if (playerChoice == 3 && aiChoice == 1)
{
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if (playerChoice == 3 && aiChoice == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if (playerChoice == 3 && aiChoice == 3)
{
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
// Outputs wins, ties and loses
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> playAgain;
system("CLS");
// Allow user to play again
} while (playAgain == 'Y' || playAgain == 'y');
}
You can throw a while loop over the block of code that reads in the input and break from the loop when you receive valid input.
int choice;
bool valid = false;
while(!valid) {
cout << "Enter a 1 for scissors, 2 for rock, 3 for paper" << endl;
cin >> choice;
if(choice == 1 || choice == 2 || choice == 3)
valid = true;
}

stuck on do while loop for C++ rock/paper/scissors/lizard/spock game - undeclared identifiers

I am stuck on something I know should be really simple to fix but I just can't figure it out I've been Google-ing and gone through my text (Gaddis C++ Intro, Ch. 6) and tried a few things - mostly moving the loop around as I thought I placed it wrong; I've searched through several C++ forums as well and I have found examples where you ask the userj "do you want to continue/play again" but that is not what I am to do, I am to have it automatically restart if there is a tie. It keeps coming back to the variables not being defined/declared for use within the loop -- except I don't see how they aren't. I'm using VS 2010.
Here's the code. I appreciate any help. I feel kinda stupid for not being able to fix this myself. This is my first programming class and I'm also taking visual basic at the same time. It's been interesting.
The debug errors I'm getting are both C2065 "undeclared identifier" for both cpuChoice and userChoice.
[code]
#include <iostream>
#include <ctime>
using namespace std;
void outputChoice(int c)
{
switch(c)
{
case 1:
cout << "Rock";
break;
case 2:
cout << "Paper";
break;
case 3:
cout << "Scissors";
break;
case 4:
cout << "Lizard";
break;
case 5:
cout << "Spock";
break;
}
}
//bool for determining if win or if draw -- loss will be elseif
bool isWin(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == 1 && cpuChoice == 3) ||
(userChoice == 1 && cpuChoice == 4) ||
(userChoice == 2 && cpuChoice == 1) ||
(userChoice == 2 && cpuChoice == 5) ||
(userChoice == 3 && cpuChoice == 2) ||
(userChoice == 3 && cpuChoice == 4));
return result;
}
bool isDraw(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == cpuChoice));
return result;
}
int main()
{
do{
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
{
int userChoice;
cout << "Please choose your move. Select 1-5: \n\n";
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
int cpuChoice = rand() % 5 + 1;
cout << "You chose... ";
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... ";
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
return 0;
}
[/code]
You problem is variable scope. change the first lines inside main():
int main()
{
int userChoice, cpuChoice;
do {
Then inside, instead of declaring these variables, just assign a value:
int cpuChoice = rand() % 5 + 1;
should be
cpuChoice = rand() % 5 + 1;
And get rid of the other declaration of userChoice altogether.
That should do it.
You declared these variables int the wrong scope. Move both declarations before the loop
int main()
{
int userChoice;
int cpuChoice;
do{
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
{
cout << "Please choose your move. Select 1-5: \n\n";
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
cpuChoice = rand() % 5 + 1;
cout << "You chose... ";
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... ";
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
return 0;
}