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.
CodePudding user response:
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.
CodePudding user response:
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;
}
CodePudding user response:
You are missing curly braces.
You need to enclose if-else block in curly braces if the block contains more than one statement.
You need to enclose in curly braces the if block on line 46, else if block on line 54 and the final else if block on line 62.
My personal opinion : Even if a particular block contains only one line, still enclose it in curly braces. This improves readability, enforces discipline and makes your code consistent.
/*
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";
}
CodePudding user response:
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";
}
}
}
CodePudding user response:
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
.