Char equals char undesired output [duplicate] - c++

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).

Related

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')

Unknown behaviour of OR operator in C++ [duplicate]

This question already has answers here:
How to compare multiple strings inside an if statement?
(6 answers)
Closed 4 years ago.
I have this code running by chance and when i put anything as the answer it is showing me correct. I know we have to put ans before YES and yay, but this code was compiled too, as i mentioned if i put any word as the input the output is correct:
string ans;
cin >> ans;
if(ans == "yes" || "YES" || "yay") {
cout << "Correct";
}else {
cout << "Incorrect";
}
Ok here's the precedence (L->R) and associativity of logic wise operators:
(((ans == "yes") || "YES") || "yay")
Since C/C++ has no chaining unlike Python.
1st: ans == "yes" -> str to str comparison
2nd: bool result of 1st || "YES" -> bool and str comparison = always true for "YES" is not null
3rd: true || "YES" = always true
Thus, the condition will always be true for "YES" and "yay" are not null.
You have the precedence wrong.
if (var == A || B || C) means "if (var is equal to A) OR (B is not zero) OR (C is not zero)"
You want to do if (var == A || var == B || var == C). That means "if (var is equal to A) OR (var is equal to B) or (var is equal to C)"
You should compare ans with each value. Every comparison with value is not "0" or "fail" value is assumed as "true". So if you input the if a condition like if("yes") it always returns a true.
int main()
{
std::string ans;
std::cin >> ans;
if("yes" == ans || "YES" == ans || "yay" == ans)
{
std::cout << "Correct\n";
}
else
{
std::cout << "Incorrect";
}
}

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

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'!

Pig Latin - strings

So I am supposed to convert English words to Pig Latin using stringConvertToPigLatin(string word) function. All the answers I could find on the internet were using char[], and I am not allowed to do so.
The program is supposed to begin with adding -way if the first letter is a vowel, and adding -ay if it's a consonant. The problem is that it is always adding "-way", even if my "word" has no vowel at all. What am I doing wrong? This is my function:
string ConvertToPigLatin(string word)
{
char first = word.at(0);
cout << first << endl;
if (first == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U')
{
word.append("-way");
}
else
{
word.append("-ay");
}
return word;
}
As noted in the comments your if statement is wrong. Each comparison needs to be done individually. From the comment.
if (first == 'a' || first == 'A' || first == 'e' || ...)
However, rather than using a long if statement you should consider stuffing all of the vowels into a string and using find. Something like the code below will be easier to read and follow.
#include <iostream>
#include <string>
std::string ConvertToPigLatin(std::string word)
{
static const std::string vowels("aAeEiIoOuU");
char first = word.at(0);
std::cout << first << std::endl;
if (vowels.find(first) != std::string::npos)
{
word.append("-way");
}
else
{
word.append("-ay");
}
return word;
}
int main()
{
std::cout << ConvertToPigLatin("pig") << '\n';
std::cout << ConvertToPigLatin("alone") << '\n';
}
This outputs
p
pig-ay
a
alone-way
I'll explain why your code isn't working:
if (first == 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U')
Let's walk through that iff statement using the word "Pig"
First the program checks first == 'a'... first == 'P' so that is false.
Then the program checks to see if false || 'A' is true. Since 'A' is true, false || 'A' is also true.
Short circuit evaluation kicks in, and the code doesn't bother checking the rest of the statement, the if condition is true so -way is appended.
To do what you want, you need to compare first to each letter. I.E.,
if (first == 'a' || first == 'A' || ...
Don't worry too much, this is a pretty standard mistake.

Why does this if statement not behave as I'd expect?

I am making a program that will evaluate the value of something. I have a variable that holds the total value to be added, rcoverE. When I test "y" for the second question, it works, but when I put in "n", it adds 5 anyway. Why is this happening?
#include <iostream>
using namespace std;
int main(){
int year, yearE, rcoverE;
string rcover, func;
cout << "Enter the decade your thing was produced (eg. 20):";
cin >> year;
cout << "Does you typewriter have original thingy? (y,n):";
cin >> rcover;
rcoverE = 0;
if(rcover == "y" || "Y"){
rcoverE = rcoverE + 5;
}else{
rcoverE = rcoverE + 0;
}
cout << rcoverE;
yearE = 100 - year / 2;
}
if(rcover == "y" || "Y"){
This condition is wrong it should be:
if(rcover == "y" || rcover == "Y"){
This: if(rcover == "y" || "Y"){ is logically equivalent to if(rcover == "y" || "Y" != 0) and "Y" != 0 is always true.
if(rcover == "y" || "Y")
Does not evaluate the way you think it does. This actually evaluates as if ("rcover == 'y') or if('Y')", not "rcover == ('y' || 'Y')." In some languages the compiler would not let you do this, but in C++, simply putting in the statement "Y" returns true. You need to change the statement to:
if(rcover == "y" || rcover == "Y")