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 last month.
Improve this question
i am just learning c++ programming language and stuck at some point will anyone help me out.
the problem is i had google some stuff and came to know that if conditions can change the varaibles value for temporary basis.
my code is below.
#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b = a + 1;
if ((a = 3) == b)
{
cout << a;
}
else
{
cout << a + 1;
}
return 0;
}
in the above code its printing the else block why not the if block the conditions must be true ?
You are mistaken.
If you will change your code the following way
int a = 2;
int b = a + 1;
if (( a = 3 ) == b)
{
std::cout << "if " << a << '\n';
}
else
{
std::cout << "else " << a + 1 << '\n';;
}
then you will see the output
if 3
In the expression of the if statement
if (( a = 3 ) == b)
the left operand of the equality operator is evaluated. As a result a becomes equal to 3 and in turn is equal to b.
If your compiler supports the C++ 17 Standard then you could declare the variables inside the if statement like
if ( int a = 2, b = a + 1; ( a = 3 ) == b )
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 2 years ago.
Improve this question
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to make a simple enhanced if statement (that is the correct definition right?) when a == 1 OR b > 2 it prints out how to say how are you sir in Arabic.
Is it possible to compare two different variables in an if and else if statement? I got myself in a Chinese finger trap with all the ('s and )'s and the different types of logical operators.
Here is my c++ code:
#include <iostream>
using namespace std;
//If / Else statement is basically a enhanced if statement.
int main()
{
int a = 1;
int b = 2;
if ((a==1)||(b>2)){
cout << "Kevak chala komm?" << endl;
}
if (((else)) (a == 1) && (b == 2)))) {
cout << "Louis C.K. is back my brothers!" << endl;
}
else{
cout << "Jek shi mash? " << endl; // How are you in polish.
}
return 0;
}
The correct syntax for that looks like this:
if (a == 1 || b > 2 ){
std::cout << "A" << std::endl;
}
else if (a == 1 && b == 2) {
std::cout << "B" << std::endl;
}
else {
std::cout << "C" << std::endl;
}
But it doesn't make any sense in your particular case, since B will never be printed (if a == 1, the first clause will hit, never using the second).
Is it possible to compare two different variables in an if and else if statement?
Yes it is very much possible, you can compare two or more different variables in if and else if stetement and also there can be multiple else if statement as well.
The below code line is wrong
if (((else)) (a == 1) && (b == 2)))) {
write else if instead.
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 c++ program which opens an url depending in what the user inputs.
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main(){
int i = 1;
string google = "https://www.google.com/search?q=";
string input;
getline(cin, input);
string changeSpace(string input)
{
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
text[i] = '+';
}
return text;
}
input = changeSpace(input);
cout << input << endl;
string url = string(google + input);
system(string("start " + url).c_str());
cout << url << endl;
}
The error is here:
string changeSpace(string input)
{
In the bracket it says it expected a " ; "
And I don't know why ocurrs that error, it may be a simple mistake, but I don't know it.
Please help me.
Your problem is because you're trying to define a function inside another function. You cannot do that.
Since C++11, the most similar thing you can do is using lambda.
int main() {
// stuff...
auto changeSpace = [] (string text) -> string
{
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
text[i] = '+';
}
return text;
}
input = changeSpace(input);
// stuff...
}
But I bet that is not the only error in your code.
The Nesting of functions is not allowed in c++. Refer this: C++ can we have functions inside functions?
For using system(string("start " + url).c_str()); in your code you should include <cstdlib>. And also use return statement in main :return 0
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
As I understand from
Left shift Float type
one cannot use left shift operator on float values. But when I tried it, it gave the same answer as multiplying by 2n.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
It outputs a = 1.1234 b = 1150 c = 1150
In which case will the two outputs (b and c) differ?
int b = (int)(a*(1<<10));
Here, since both 1 and 10 are integers, you are performing left shift operation on integer instead of on floating-point number.
you are multiplying 1024 with value of a(1.1234) in both case.
it does not mean you are shifting float value.
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
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n);
int main()
{
double i;
while (true)
{
cout << "Enter a number that isn't 0: ";
cin >> i;
if ( i == 0)
break;
if(prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime." << endl;
}
system ("Pause");
return 0;
}
bool prime (int n)
{
int i;
double sqrt_of_n = sqrt(double (n));
for (i = 2; i <= sqrt_of_n; i++)
{
if (int(n) % 1 == 0)
return false;
}
return true;
}
Everytime I run the program, if I input 7, I get that 7 isn't prime. Can someone help me figure out where I messed up?
I have tried changing between double and int for i and n.
If I input 3, it shows prime.
The problem is that it's showing some prime numbers as not prime.
The body of your for loop doesn't use i at all.
In particular, n % 1 is always zero, for any integral n.
Presumably you want to know whether n is divisible by i, but accidentally checked if n is divisible by 1.
You could easily have discovered this mistake yourself by single-stepping in a debugger, and making the various subexpressions into "watch expressions".