Why isn't the OR (||) logical operator being properly computed? [closed] - c++

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 9 days ago.
Improve this question
When printing to the terminal, the OR operator is not being applied in C++.
MWE:
#include <iostream>
int main()
{
std::cout << false || true;
return 0;
}

Shift operators have higher priority than logical operators.
So this statement
std::cout << false || true;
is equivalent to
( std::cout << false ) || ( true );
As a result the literal false will be outputted as integer 0.
If you want to output the literal true then you should write
std::cout << ( false || true );

Related

not equal is c++ (if else statements) [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 years ago.
Improve this question
Problem is that i dont know what i am doing wrong here...
i need to get if a = 1 cout is "pasirinkai fizika..."
and if a != 1 cout is "nieko nepasirinkai..."
here is code:
cout << "Pasirinkimai: parasyk skaiciu... \n";
cout << "1 ---- Skaiciuoti fizika 9 klasiai...\n";
cin >> a;
std::getchar();
if (a = 1) {
cout << "pasirinkai fizika...";
}
else if (a != 1) {
cout << "nieko nepasirinkai...";
}
std::getchar();
When i type 2 for example it says that "pasirinkai fizika..."
and as i said before it should say "nieko nepasirinkai..."
= is an assignmenet operator, you are looking for equality operator == e.g. if (a == 1).
Many languages use this C notation, you might need to get used to it.

The array index doesnt show the character [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 6 years ago.
Improve this question
I have this code:
#include <stdio.h>
int main(){
char s1[30] = "This is a sentence";
for(int i = 0; i<sizeof(s1);i++){
if(s1[i] = ' '){
printf("+");
}
}
return 0;
}
When I try to loop the array to find all the spaces this happens:
Output: ++++++++++++++++++++++++++++ //30 pluses.
Why doesnt my program outputs 3 pluses?
EDIT: My problem was a simply typo mistake, If you didn't understand what is wrong here take a look at accepted answer.
Change = to == in your if statement.
In your conditional statement, you're assigning space to s[ i ] (operator =). You want to compare them (operator ==).
Try
if (s[ i ] == ' ')
s[ i ] = ' ' is always true because the result of an assignment is the value assigned (space). This value is implicitly converted to a bool (0 = false, anything else = true). Since a space is 32 in ASCII, it will always be true.
References - Assignment Operator, Comparison Operators, ASCII Table
Do this:
if(s1[i] == ' '){
printf("+");
}
= is an assignment operator. To compare two value you need to use == operator. You have used = that that assignment operator always return true so + is being printed out all the time.

How does the "Or" operator work? (In C++) [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 6 years ago.
Improve this question
#include "stdafx.h"
#include <iostream>
int x = 0;
int main(){
std::cin >> x;
if (x == 5 || 6) {
std::cout << "5 or 6\n";
}
else {
std::cout << "Not 5 or 6\n";
}
return 0;
}
This simple code only returns "5 or 6" to the console, no matter what number you put in it. I really don't understand why. If || is the or operator, then it should work. If x is 5 or 6 it should display "5 or 6". If it's not, display "Not 5 or 6". Could someone please explain?
if (x == 5 || 6)
should be
if (x == 5 || x == 6)
You think you're checking "if x is 5 or x is 6", but you're actually checking "if x is 5, or if 6". In C++, any non-zero number by itself in an if-statement evaluates to true, so your initial if is equivalent to:
if (x == 5 || true)
The behaviour is specified in the C++ standard as follows:
A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true.

save variable from 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 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.

C++ comparing a character of a string [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 9 years ago.
Improve this question
Ok, so input is a string. When I try to compile the following code I get
c.cpp:42:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Why?
if(input[i] != ' ')
{
char s = input[i];
if(s == "+")
{
...
}
}
Use single quotes in this statement
if(s == "+")
as here
if(s == '+')
As char s is a character so it can only be compared against another character or ascii value.
Double quotes (" ") are used for string while single quotes (' ') for characters.