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

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

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

C++ Blackjack code only going to first if statement

I'm trying to code a blackjack game and everything is going smoothly so far but for this bit. No matter what I input into hitStand it always goes to the first if statement and "hits". I would like for if "h" is inputted it "Hits" and if "s" is inputted it "Stands" and, if there is an invalid input, it will tell the user to try again.
I'm still fairly new to C++, so some help would be appreciated.
while (repeat == 0)
{
char hitStand;
cout << "Would you like to HIT or STAND [H/S]";
cin >> hitStand;
if (hitStand = "H" || "h")
{
PcardNew = rand() % 13 + 1;
cout << endl;
cout << "Your new card is: " << PcardNew << endl;
if (PcardNew > 10)
{
PcardNew = 10;
}
playerTotal = playerTotal + PcardNew;
cout << "Your new total is: " << playerTotal << endl;
}
else if (hitStand = "S" || "s")
{
break;
}
else
{
cout << "Please enter a valid imput [H/S]" << endl;
}
}
There are (at least) three errors in the single if (hitStand = "H" || "h") line!
First, the = operator is an assignment, not a comparison; to test for the equality of two operands, you need the == operator.
Second, the "H" and "h" constants are string literals - that is, multi-character, null-terminated strings of characters. Use single quotes for single characters (thus, 'H' and 'h').
Third, you can't compare one object with two others like that with a logical or (||) operator. You need to make two separate comparisons and then or the results of each:
So, use this:
if (hitStand == 'H' || hitStand == 'h')
{
//...
And similarly for your second test:
else if (hitStand == 'S' || hitStand == 's')
{
//...
That is because your condition in if statement is always true. Since "h" is in or (||).
Instead use:
if (hitStand == 'H' || hitStand == 'h')
and
else if (hitStand == 'S' || hitStand =='s')

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

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

Better methods to apply operator "and &&" "or || " C++

Question: Is there a more efficient method to using operator or "|| " than what I'm using?
I'm creating the HangMan Game in which players type a letter and the only way to get out of the loop is by entering the correct word in which this case if statement is the only way out.
I'm running into a problem with my compiler, maybe is that i need to work on a better and cleaner version of my code but here is what I'm currently have so far...
// w is the word chosen
if(w1=='d' && w2=='u' && w3=='c' && w4=='k' || answer == error)
{
if(w1=='d' && w2=='u' && w3=='c' && w4=='k')
{
cout << "The word is correct \n";
}
else if (answer == error)
{
cout << " You got 5 strike you lost \n";
}
}
I'm currently using Qt Project Compiler in Ubuntu to Compile my C++ Program.
I don't have this error/ suggestion with Gcc g++ on the Command Line
/home/cristian/Qt_Programs/Hangman_Game/main.cpp:123: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
You don't need the first if statement. The second if-else will accomplish the same with or without it. That should remove the compiler suggestion.
if (w1 == 'd' && w2 == 'u' && w3 == 'c' && w4 == 'k') {
cout << "The word is correct \n";
}
else if (answer == error) {
cout << " You got 5 strike you lost \n";
}