You are using a version without Ads of this website. Please, consider donating:

### If then statements

How do i get it to read just one of the if statements, currently it will print all 3 based on the temp and rain inputs I give it.

Is if/then not the correct way to do this? or am i missing something simple

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````#include using namespace std; int main() { int temp; float p; char garden,B,V,F; const int TOOCOLD = 43; B = 'B'; V = 'V'; F = 'F'; cout<<"Garden Sprinkler System\n\n"; cout<<"What is the temperature in degrees (F) ?"; cin>>temp; cout<<"How much precipitation today (in inches)?"; cin>>p; cout<<"The Garden Divisions are F-Flowers V-Vegetable B-Berries\n"; cout<<"Which do you choose (FVB)?"; cin>>garden; if ((garden == B) && (temp > TOOCOLD) && (p < .4)) { cout<<"Given the teperature is "< TOOCOLD) && (p < .3)) { cout<<"Given the teperature is "< TOOCOLD) && (p < .115)) { cout<<"Given the teperature is "<
Problem is that even if your 'if' condition is not satisfied, the control will still go to your 'else' condition.

For ex, if garden is 'B' then the 1st if statement may or may not execute (depending on the values of temp and p) and the remaining else statements shall execute.

A better way would be to use a switch statement.
 ``1234567891011`` ``````switch (garden) { case 'B': // if / else statement for B break; case 'V': // if / else statement for V break; case 'F': // if / else statement for F break; }``````
I just threw this together real quick.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include using namespace std; int main() { int temp; float p; char garden; const int TOOCOLD = 43; cout<<"Garden Sprinkler System\n\n"; cout<<"What is the temperature in degrees (F) ?"; cin>>temp; cin.ignore(); cout<<"How much precipitation today (in inches)?"; cin>>p; cin.ignore(); cout<<"The Garden Divisions are F-Flowers V-Vegetable B-Berries\n"; cout<<"Which do you choose (FVB)?"; cin>>garden; cin.ignore(); if ((garden == 'B') && (temp > TOOCOLD) && (p < .4)) { cout<<"Given the teperature is "< TOOCOLD) && (p < .3)) { cout<<"Given the teperature is "< TOOCOLD) && (p < .115)) { cout<<"Given the teperature is "<
Last edited on
separating all of the not watered really helped a ton! thanks man
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: