+ 4

How do I? (help please) : while statements with multiple conditions

So here is my question : int a, b, x, y; while (a!=x & b>y){ if (a>=2x){ b=(y-1); cout << "statement0" << endl; cin >> a; if (a==x){ cout << "statement2" << endl; } } else{ if (a==x){ cout << "statement1" << endl; } } /*Q. when the first if statement triggers that sequence, and where a==x gets (statement2), how do I make it so that the (statement1) from the else statement also doesn't show up?*/ example of result: when initial a>=2x: "statement2" "statement1" (//which is not what I want, 2 statements appearing) but when initially a==x: "statement1" (//which is what I want, just a single statement) (this is a not the original code, it does ask for user input, and I use 2x to help visualize that when I set the conditions for twice the value of x or a value greater than x.)

23rd Feb 2018, 7:25 PM
Jung Sa (Hanen)
Jung Sa (Hanen) - avatar
3 Respostas
+ 1
anyway, I figured it out, I just have to put the if statement where a==x in front of the if statement where the condition of a>=2x is met.
23rd Feb 2018, 7:49 PM
Jung Sa (Hanen)
Jung Sa (Hanen) - avatar
+ 2
this is a simplified version of the problem i'm having, but all in all, in the original code, it does ask for user input, and 2x is just merely a statement i'm using to help visualize the problem. Also I did state that it DOES work so that must mean I did it right, the only part i'm having trouble is allowing it to set priority of these 2 statements being synonymous.
23rd Feb 2018, 7:28 PM
Jung Sa (Hanen)
Jung Sa (Hanen) - avatar
+ 1
I don't know a lot about c++, but some things in this code arent valid/good, like you dont ask for user input to check if a!=x or b>y. If you want to say "and" you should use 2 &s, so like this (&&); Also, 2x isn't valid like in math, you should use 2*x. Hope that someone will help you solve this, or you could first ask for user input something like: cout << "enter a number: " << endl; cin >> a; .... cout << "enter the last number: " << endl; cin >> y; ...
23rd Feb 2018, 7:20 PM
Ice
Ice - avatar