0

this is outputting int Q even when the if statement evaluates to false. why?

#include <iostream> using namespace std; int main() { string a ="i am learning c++!"; cout << "Hello world!" << endl; cout << a << endl; string b ="it is tedious."; cout << b << endl; string c="22x=220\n what does x equal?\n"; cout<< c; string Q="correct"; string W="false"; int qwerty; cin>> qwerty; if (qwerty=10){cout<<Q; } else {cout<<W; } return 0; }

13th Oct 2018, 1:59 AM
Benjamin Burks
Benjamin Burks - avatar
2 Réponses
+ 4
You are assigning 10 to qwerty as you only have one =. 10 is true so you always get Q.
13th Oct 2018, 2:53 AM
John Wells
John Wells - avatar
0
You assigned 10 to qwerty, the returned value is always true: if (qwerty = 10) You compare qwerty with 10. This returns true if qwerty equals 10, and false otherwise: if (qwerty == 10)
13th Oct 2018, 2:53 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar