#include<iostream>
usingnamespace std;
int main()
{
int temp;
float p;
char garden,B,V,F;
constint 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 "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nBerries in the garden ";
cout<<"will be watered";
cin.get();
cin.get();
return 0;
}
else
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nBerries in the garden ";
cout<<"will NOT be watered";
}
if ((garden == V) && (temp > TOOCOLD) && (p < .3))
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nVegetables in the garden ";
cout<<"will be watered";
cin.get();
cin.get();
return 0;
}
else
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nVegetables in the garden ";
cout<<"will NOT be watered";
}
if ((garden == F) && (temp > TOOCOLD) && (p < .115))
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nFlowers in the garden ";
cout<<"will be watered";
cin.get();
cin.get();
return 0;
}
else
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nFlowers in the garden ";
cout<<"will NOT be watered";
}
cin.get();
cin.get();
return 0;
}
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.
1 2 3 4 5 6 7 8 9 10 11
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;
}
#include<iostream>
usingnamespace std;
int main()
{
int temp;
float p;
char garden;
constint 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 "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nBerries in the garden ";
cout<<"will be watered";
cin.ignore();
return 0;
}
elseif ((garden == 'V') && (temp > TOOCOLD) && (p < .3))
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nVegetables in the garden ";
cout<<"will be watered";
cin.ignore();
return 0;
}
elseif ((garden == 'F') && (temp > TOOCOLD) && (p < .115))
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \nFlowers in the garden ";
cout<<"will be watered";
cin.ignore();
return 0;
}
else
{
cout<<"Given the teperature is "<<temp;
cout<<" degrees and "<<p;
cout<<" inches of precipitation today. The \n";
if (garden == 'B')
cout << "nBerries";
elseif (garden == 'V')
cout << "Vegetables";
elseif (garden == 'F')
cout << "Flowers";
cout <<" in the garden ";
cout<<"will NOT be watered";
}
cin.ignore();
return 0;
}