My else if statement using a string is not working [duplicate] - c++

This question already has answers here:
if statement not working right?
(5 answers)
Closed 7 years ago.
after a good amount of time trying to get my else if statement to work, it just doesn't. This program keeps returning the first one, no matter what I input. Please help.
#include <iostream>
#include <string>
using namespace std;
string arehap;
int main()
{
cout << "Are you happy?" << endl;
cin >> arehap;
if (arehap == "Yes" || "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || "N")
{
cout << "Bad." << endl;
}
return 0;
}

You should use this:
if (arehap == "Yes" || arehap == "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || arehap == "N")
{
cout << "Bad." << endl;
}
When you're using the || operator, you have to compare two boolean values. If arehap is equal to "Y", the following statement will be True: arehap == "Y". In that case your computer will "understand" this as if (True || False) { /* do smth */} and this will evaluate to True and the code you want to execute will be run.

Your problem lies in this line:
if (arehap == "Yes" || "Y")
C++ understands this as
if ((arehap == "Yes") || ("Y"))
and while the first check (arehap == "Yes") might be false, the second check -- which is just "Yes" is always true.
This happens, because the "Yes" gets understood as a char const* -- and this pointer must obviously not be NULL, but point to the character 'Y'!

Related

Error E0169. Exspected declaration. If statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I am trying to make a Rock-Paper-Scissor game in C++ in Visual Studio.
I am trying to make the computer generated response, but when I make the if statements, it thinks it is an error on line 9:
#include <iostream>
using namespace std;
string rockPaperScissor;
int computerChoiceInteger = 1 + (rand() % 3);
if (computerChoiceInteger == 1) {
cout << "1";
}
if (computerChoiceInteger == 2) {
cout << "2";
}
if (computerChoiceInteger == 3) {
cout << "3";
}
int main() {
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || "R") {
cout << "correct";
}
if (rockPaperScissor == "p" || "P") {
cout << "correct";
}
if (rockPaperScissor == "s" || "S") {
cout << "correct";
}
}
I have tried to comment out the first if statement, but then it says there is an error on line 13.
For your main error, you cannot have logical statements (if, while, etc.) outside of a function body. You will need to move all of your if statements into your main function body.
C++ uses the main function as the "entry point" for your program, which means once all global variables, class definitions, and more, are settled, the main function will be what your program begins to execute. All the logic for your program should always be inside of a function.
Also, as others in the comments have pointed out, you cannot choose between two values in an or statement like you are currently doing. You need to explicitly make two comparisons. I've moved your conditional logic as well as changed your or statements here, as well as added some more newlines:
#include <iostream>
using namespace std;
int computerChoiceInteger = 1 + (rand() % 3);
int main() {
if (computerChoiceInteger == 1) {
cout << "1\n";
}
if (computerChoiceInteger == 2) {
cout << "2\n";
}
if (computerChoiceInteger == 3) {
cout << "3\n";
}
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
string rockPaperScissor;
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || rockPaperScissor == "R") {
cout << "correct\n";
}
if (rockPaperScissor == "p" || rockPaperScissor == "P") {
cout << "correct\n";
}
if (rockPaperScissor == "s" || rockPaperScissor == "S") {
cout << "correct\n";
}
return 0;
}
As further reading, you may be interested in Generate random number between 1 and 3 in C++ to generate a more truly random choice by the computer.
Your program is structured all wrong.
you are missing #include <stdlib.h> or #include <cstdlib> to declare rand(). It is possible that <iostream> is already including one of them internally for you, but you should not rely on that behavior. Be explicit in the headers you want to use.
global scope can only have declarations, not statements. Statements can only appear in function scope.
your use of the || operator is incorrect. You can't compare a variable to multiple values the way you are trying to. You need a separate comparison for each value.
Try something more like this instead:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int computerChoiceInteger = 1 + (rand() % 3);
cout << computerChoiceInteger << endl;
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
string rockPaperScissor;
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || rockPaperScissor == "R") {
cout << "correct";
}
else if (rockPaperScissor == "p" || rockPaperScissor == "P") {
cout << "correct";
}
else if (rockPaperScissor == "s" || rockPaperScissor == "S") {
cout << "correct";
}
return 0;
}
Now, the code should at least compile, so you can carry on with finishing your game logic (since what you have shown is not a complete Rock-Paper-Scissor game yet).

question about this if/else code using || and &&

#include <iostream>
#include <string>
using namespace std;
int main()
{
string vegetarian, vegan, gluten_free;
cout << "Welcome to Restaurant Selector!" << endl;
cout << "Are you a vegetarian?"<<endl;
cin >> vegetarian;
cout << "Are you a vegan?"<<endl;
cin >> vegan;
cout << "Are you gluten-free?"<<endl;
cin >> gluten_free;
if (gluten_free == "yes" && vegan == "yes" && vegetarian == "yes" )
{
cout<<"The Chef's Kitchen"<<endl;
}
else if (gluten_free == "yes" && vegan == "yes" )
{
cout<<"Vegan's lair"<<endl;
}
else if (gluten_free == "yes")
{
cout<<"Starbuck's"<<endl;
}
else if ( gluten_free == "no" || "No" && vegan == "no" || "No" && vegetarian == "no"||"No")
cout<<"Burger King"<<endl;
return 0;
}
How can I get the first if expression to use multiple variations of yes.
I want it to include: yes, Yes, and YES. however I can only use the or operator on two strings. I would also like to do this for the subsequent lines. What should I do?
How can I get the first if expression to use multiple variations of yes. I want it to include: yes, Yes, and YES.
Convert the input string to lower case before comparison.
A trivial example:
auto tolower = [](unsigned char c){
return std::tolower(c);
};
std::transform(
vegan.begin(),
vegan.end(),
vegan.begin(),
tolower
);
Note that simple string processing such as this work only with fixed width encodings and not with unicode.

Why does my function get read, but become an infinite loop? [duplicate]

This question already has answers here:
If statement runs through whether conditions are met or not
(4 answers)
Closed 2 years ago.
Hello I'm having a problem where when I call my function after the user enters "Y" to start the game, the cout gets read but it becomes an infinite loop. It works just fine when you enter "N" or something thats not supposed to be entered. I am using a header file called functions to well, put all the functions if that has anything to do with it. I am still in the very early learning stages of programming, and run into so many speed bumps and just not quite sure where to turn. Any help is appreciated. (P.S. I have not yet started on the gameStart() function just because of this problem. That's not whats it's going to be in the end.)
#ifndef FUNCTIONS_H;
#define FUNCTIONS_H
#include <iostream>
using namespace std;
void startScreen()
{
void gameStart();
char answer;
cout << "Welcome to __________\n\n";
cout << "This is my fisrt ever actual program I made out of my own free will lol.\n";
cout << "It is a Text-Based Adventure game. In this game you will make a character,\n";
cout << "and explore the land of Spelet, battling enemies, leveling up, getting loot,\n";
cout << "and learning skills! You do not need to capitalize anything but your character\n";
cout << "name. If a question has (something like this), those are the choices for that \n";
cout << "interaction! Thank you for trying out my terrible little game! :)\n";
cout << "I really hope y'all enjoy it!\n\n";
cout << "Would you like to play?\n";
cin >> answer;
do
{
if (answer == 'Y' || answer == 'y')
{
gameStart();
}
else if (answer == 'N' || answer == 'n')
{
cout << "Program will now close...\n";
system("pause");
exit(0);
}
else
{
cout << "Enter a Y for yes or an N for no.\n";
cout << "Would you like to play?\n";
cin >> answer;
}
}
while (answer != 'N', 'n' || 'Y', 'y');
}
void gameStart()
{
cout << "\n\"BOOM-BOOM-BOOM...\"\n\n" << endl;
}
#endif
maybe you need:
while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y')
The comma operator doesn't do what you think it does. It "discards the result," as my link says.
You want the && operator (AND operator) instead:
while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y');

Char equals char undesired output [duplicate]

This question already has answers here:
if statement not working right?
(5 answers)
Can you use 2 or more OR conditions in an if statement? [duplicate]
(9 answers)
Closed 4 years ago.
I am trying to create a simple c++ program which prints out if char is Y or y, N or n or neither.
After debugging I have found out that the if(chr == 'Y' || 'y') statement is true even though char variable is 'N'. Can anybody tell me why this if statement is true and not false?
#include "pch.h"
#include <iostream>
using namespace std;
void main()
{
char chr = 'N';
if (chr == 'Y' || 'y')
{
cout << "chr is y" << endl;
}
else if (chr == 'N' || 'n')
{
cout << "chr is n" << endl;
}
else
{
cout << "chr is something else" << endl;
}
}
This is not doing what you thing:
if (chr == 'Y' || 'y')
This is basically:
if (chr == 'Y' || true)
So in the end:
if (true)
You have to say what you compare:
if (chr == 'Y' || chr == 'y')
The operator == only takes one character, not a set of possible characters.
Instead of this
if (chr == 'Y' || 'y')
You need
if ((chr == 'Y') || (chr == 'Y'))
Likewise for the 'N' and 'n'.
It is also possible do it with one comparison:
if (toupper((unsigned char)chr) == 'Y')
This way, maintainability is slighly improved as only one value has to be changed should the letter change (for a different localization, per example).

What is wrong with my if , else if and else loop? [duplicate]

This question already has answers here:
Can you use 2 or more OR conditions in an if statement? [duplicate]
(9 answers)
Closed 4 years ago.
Ok, so I was writing a simple interface for a programming I'm creating and i come across this issue, where it gives me the same response regardless.
#include <iostream>
using namespace std;
int main()
{
char v;
cout << "Binary or ASCII? "<<endl;
cin >> v;
if (v == 'B' || 'b')
{
cout << "Binary " << endl;
}
else if (v == 'A' || 'a')
{
cout << "ASCII " << endl;
}
else
{
cout << "ERROR: Invalid Option" << endl;
}
return 0;
}
The interface is supposed to output
Binary
if I type B or b
ASCII
if i type A or a
and
ERROR: Invalid Option
for everything else
Instead, I get
Binary
regardless of what I type
Where is my mistake? what am I doing wrong?
Let's take a look at what happens in your if:
if (v == 'B' || 'b')
First it checks if v == 'B'. Let's assume it doesn't for the sake of this walkthrough. Then it'll check (false || 'b'). Since 'b' always evaluates to true, this will be true!
You probably wanted:
if (v == 'B' || v == 'b')