How does the "Or" operator work? (In C++) [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 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.

Related

Why isn't the OR (||) logical operator being properly computed? [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 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 );

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.

Mathematic equation solved recursively 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
I have a task to make a code which will write 100 first numbers of an equation (or a function, I don't know what this is)
A(n) = (A(n-1))^2 -n*A(n-2) where A(1) = 1 and A(2) = 1
It has to be solved recursively. I have written this code so far
#include <iostream>
using namespace std;
int rekurzija(int n){
if(n=1){
return 1;
}
if(n=2){
return 1;
}
if(n>2){
return rekurzija(n-1)*rekurzija(n-1)-n*rekurzija(n-2);
}
}
int main(){
for(int n=1;n<101;n=n+1){
cout << rekurzija(n) << endl;
}
}
The problem is that the program returns 1 hundred times instead of 1,1,-2,0,...(instead of actually solving this function). What is wrong in this code?
You are using simple assignment operator = instead of Is equals to relational operator == in your rekurzija() function for if conditions
if(n = 1) //here `n = 1`is an assignment statement
{
//something...
}
What happens if you use = instead of ==?
The if condition will always evaluate to be true if the assigned value in the assignment statement is non-zero number.
Note: An assignment to zero evaluates to be false i.e, for if(n = 0), the if block will not be entered. You don't have any such if blocks in your code.
So your first if is always evaluated to be true because you are assigning a non-zero value i.e, 1 and thus your function always returns 1. that's the reason why you get 100 1's as your answer.
So, instead try changing all the if conditions to something like:
if(n == 1)
{
//something...
}
This would check if n is equals to 1 or not. If n is equal to 1 then the if block is entered, else it would not enter the if block and the next if condition is checked.
Note: Just remember this while using the = and == operators
= is for assignment
== is for comparison
When you compare things in C++ you need to do it like:
if (a == b)
and not
if (a = b)
The latter will assign b to a and return the value of a.

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.

logic or syntax error? C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).