Diego Eduardo Camacho Diego Eduardo Camacho - 1 year ago 62
C++ Question

Why does't the "Or" operator work?

Can't get the "Or" operator to work i have also tried "And" but with both i get syntax errors.

#include <iostream>
#include <string>
#include <time.h>
#include <random>
using namespace std;

int main() {
string Player1, Player2; /*Player 1 and Player 2 Name*/
int Turn, Pop; /*Turn can be either 0 for Player 1 or 1 for Player 2*/ /*Pop is how many ballons will be popped can be either 1 or 2*/
bool Continue = true;/*Checks if the game has finished or not*/
int Ballons = 10; /*Original number of Ballons*/
while (Ballons != 0) {/*Main Game Loop*/
cout << "How many ballons would you like to pop?";
cin >> Pop;/*How many Ballons does the player want to pop*/
*if (Pop > 2) || (Pop<1){/*checks if the user wants to pop more then 2 and asks to choose again*/*
cout << "You Can only pop 1 or 2 ballons at a time." << endl;

Thank you

Answer Source

It should be written

 if (Pop > 2 || Pop < 1) {

You could also write it

if ((Pop > 2) || (Pop < 1)) {

but the extra brackets inside don't change the meaning nor serve any real purpose in this situation since both sides are simple and || has the highest precedence here.

In C/C++ you need to put the condition part of an if between brackets (). After those brackets you have to provide either the code to execute if the condition is met or a { which you should remember to close with a } after the code to execute if the condition is met.