+ 1

Unexpected Output and Warning

I am just playing around with what I have learned so far, up to the naming convention of variables. So I don't understand what I am doing wrong with the if/else. It doesn't seem to evaluate what I have in prens, plus I get this warning message in Code:Blocks: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]| #include <iostream> using namespace std; int main() { string PlantName; //Pascal naming convention bool plantState; //Camel naming convention cout << "\n"; cout << "Please enter the plant name: "; cin >> PlantName; cout << "\n"; cout << "Is " << PlantName << " yellow and limp? [1=Yes:0=No]: "; cin >> plantState; if (plantState=1) { cout << "\n"; cout << "Give " << PlantName << " some water." << endl; cout << "\n"; } else { cout << "\n"; cout << "Water " << PlantName << " every 2 days." << endl; } return 0; }

2nd May 2018, 2:36 PM
Michelle
Michelle - avatar
2 Respostas
+ 7
= is assignment operator. Use == to compare. if(plantState == 1){ // ... }
2nd May 2018, 2:44 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
Oi! Thank you, Shamima! How could I miss that!
2nd May 2018, 4:38 PM
Michelle
Michelle - avatar