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.
Related
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 );
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
Could you guys please walk and help me with this practice question?
I'm unable to figure out how the answer is 5.
int arr[12] = { 1,3,5,0,7,2,0,4,4,0,8,8 };
int count = 0;
for (int i = 0; i<11; i++) {
if (arr[i] = arr[i + 1])
count++;
else
count--;
}
cout << count << endl;
In your example you have :
if (arr[i] = arr[i + 1])
which is the =, not ==. It is assigning not checking for equality. So in the example:
if (a = 3) {
You will assign a to 3 and check if 3 is true, which it is. This leads to an easy look at why the answer is 5:
arr=> { 1,3,5,0,7,2,0,4,4,0,8,8 };
count=> 1,2,1,2,3,2,3,4,3,4,5
And if you are interested, look at the array after you have completed. It will look like this:
{3,5,0,7,2,0,4,4,0,8,8,8} // Everything has been moved down 1 (except for the final member)
See a live example of this here.
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.
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.
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 8 years ago.
Improve this question
I need to display a string with values like 36 Deg Celsius.
string sFinish = NULL;
string sValue = "36";
sFinish.append(sValue);
sFinish.append(" Deg Celsuis");
cout<<"Degree = "<<sFinish;
I am not able to figure out how to display degree (o symbol) instead of writing "Deg Celsius".
If you just copy paste "°" string into code - it shows extra character - like this "°".
Try:
std::cout << "Temperature: " << sValue << "\370";
You might find the following link helpful for the full ascii table.
Here is a solution I found here on SO: Including decimal equivalent of a char in a character array
But to summarize, this would do fine
char * val = "37";
string temp(val);
temp.append("\xB0");
cout << temp;
Just in-case if anyone wants to try this:
sFinish.append("\u2103");
this will display Deg celsius :)