Error expected primary-expression before '||' token - c++

Currently trying to complete my C++ Assignment, the code isnt complete yet, but I was just trying to compile it and I ran into this error. I cant seem to figure out how to fix it, any pointers? Please excuse this super noobie question.. im still a beginner in an intro course.
Code

Your parentheses are messed up. An if statement condition in C++ should be wrapped in a parenthesis like so:
if ( animal == 'D' || animal == 'd' )...
if ( status == 'Y' || status == 'y' )...
else if ( status == 'N' || status == 'n')...

You if condition must be enclosed by a single bracket
if ((animal == 'D') || (animal == 'd'))
&
if((status == 'Y) || (status == 'y))
&
else if((status == 'N') || (status == 'n'))

Your If syntax is wrong, sweetie. You have missed the round brackets.
Should be:
if((status == 'N') || (status == 'n'))

Related

While not equal statement and or differences

while ((choice != "Wizard") && (choice != "Knight"))
This is the only one that worked
while ((choice != "Wizard") || (choice != "Knight"))
while (choice != "Wizard" || "Knight")
Honestly, I don't understand why using OR won't work and what difference it has when I separate them. Hoping someone can help explain.
Take the string "Wizard":
Does it differ from "Wizard"? No.
Does it differ rom "Knight"? Yes.
So "different from Wizard AND different from Knight" is false (it does not differ from "Wizard"). However, "different from Wizard OR different from Knight" is true (it does differ from "Knight").
As for the third form, choice != "Wizard" || "Knight" is parsed as (choice != "Wizard") || "Knight". "Knight" is a string literal and as such, it decays to a non-null pointer in most expressions. Being non-null, the pointer converts to true. So you're effectively asking: "choice differs from "Wizard" OR true." That is of course always true.
As an alternative to an ever-growing chain of (choice != "...") &&, you can instead create a container of things to test for, and see if choice is an element of that container.
static const std::unordered_set<std::string> options = {
"Wizard",
"Knight",
"Rogue",
"Basket Weaver",
};
while (options.count(choice) == 0)

Program works fine in Debug, but not in Release

I made a tic-tac-toe game which works good. Recently I decided to add a reset game feature.
Here is the part where I take input from user
void playerMove() //Gets the player move and updates the box variables
{
int boxnumber;
while(1) //Set's loop if the player enters a invalid input
{
cout << "\n Player " << player << "'s turn!(Press 0 to reset the game!): ";
cin >> boxnumber;
if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0) // Checks if the input is valid or not
{
system("CLS");//If invalid, show error and loop back to start to get new input
displayBoard();
cout << "\n Invalid Input! Try again!\n";
}else if(boxnumber == 0)
{
refreshGame();
displayBoard();
}else
{
box[boxnumber-1] = player;//If inputs are fine, set the box variable's and break out of loop!
break;
}
}
}
Now, in debug mode, when I press 0, everything runs fine and the game is reset, but in release build, when I press 0, it gives me the "Invalid Input! Try again!"
Things I have tried the didnt work:
-Rebuild the entire release and debug version.
-Making a new project and copy-pasting my code. Same thing, debug works, release doesnt.
For anyone wondering, I am using code::blocks IDE. The compiler is GNU GCC.
Thanks for the help! :)
You have undefined behavior.
In the "first" if you have box[boxnumber-1], so when you enter 0 (as you have stated in your question), you're trying to access element with index -1. That's UB, as you're reading "invalid" memory.
You should check for 0 first (and for negative numbers also).
In the if statement, put the range checks in front of the value checks. That is, change
if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)
to
if(boxnumber < 0 || box number > 9 || box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O')
That takes advantage of short-circuiting: if the input value is less than 0 or greater than 9 the value checks won't be executed. That will avoid checking things like box[10], which isn't valid.
That still leaves a problem: if the user inputs 0, this code will happily check box[-1], which is also out of range. To get rid of this, move the branch of the if statement that tests box number == 0 in front of this part:
if (box number == 0)
// whatever
else if (box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)
// whatever else

C++ setGender method reverts to default values

I've written the code bits below. I have a constructor which takes five arguments. Unfortunately, the setGender method spits out a default 'M' for all instances of a class rather than setting the gender to the specified parameter. What am I doing incorrectly? Any advice would be greatly appreciated. Thank you.
DateProfile::DateProfile(char gdr,
char searchGdr, int romanceScale, int financeScale, string theName)
bool DateProfile::setGender(char gdr)
{
if (gdr != 'M' || gdr != 'F')
return false;
gender = gdr;
return true;
}
if (gdr != 'M' || gdr != 'F') is always true, irrespective of input. If you're passing 'M', the second part of the expression is true. If you're passing anything else, the first part of the expression becomes true.
What you meant to write is if (gdr != 'M' && gdr != 'F') instead.
Increasing the warning level of your compiler may have helped you spot the error. Most compilers will warn about expressions always evaluating to a single value, or at least about the unreachable code following it.

if statement with multiple condition not working...

here is my code :
string function1( string input)
{
string output;
int i=0;
if (input.at(i)!='A' || input.at(i)!='a'|| input.at(i)!='E' || input.at(i)!='e' || input.at(i)!='I' || input.at(i)!='i' || input.at(i)!='O'||input.at(i)!='o' || input.at(i)!='U' || input.at(i)!='u')
{
char x=input[i];
input.erase(input.begin()+i);
output=input+x;
}
else
{
output=input+"yay";
}
return output;
}
but its not doing what i want it to do.. can't figure out where its going wrong...
can any1 help?
Basically the issue is that its never going in to the else statement..
if i pass in BJ it should return BJYAY right..
but its giving me JB
Thanks!
Change the || operators of the if statement to &&. The statement you have written is always true. What you want is that the first character is not a vowel, i.e. it does not match 'A' AND it does not match 'E', etc.
Changing the != to == will give you the result requested.

if condition on turbo C++

I'm having a problem regarding with if statement in C++ this statement is in do-while loop.
gotoxy(27,22);cout<<"Do you want to continue [Y]?";
sub=getche();
if(sub!='y' || sub!='Y')
{
gotoxy(27,24);cout<<"INVALID ANSWER!!";
gotoxy(27,26);cout<<"Closing Program....";
delay(3000);
exit(1);
}else
{
sub=ans;
}
}while(tolower(ans)=='y');
whenever I input y on the variable sub the code on if statement is still executing.. please someone tells me where is the error.. Thanks!
The boolean expression of (sub!='y' || sub!='Y') will always evaluate to true
This line:
if(sub!='y' || sub!='Y')
Needs to be this:
if ( (sub != 'y') && (sub != 'Y') )
Your code if(sub!='y' || sub!='Y') will be true ,no matter what you enter because eithersub!='y' or sub!='Y' will evaluate to true. Hence Use && instead of ||.