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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>

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 "<<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;
}
I just threw this together real quick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
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 "<<temp; 
		cout<<" degrees and "<<p;
		cout<<" inches of precipitation today. The \nBerries in the garden ";
		cout<<"will be watered"; 
		cin.ignore();
		return 0;
	}
	else 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.ignore();
		return 0;
	}
	else  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.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";
		else if (garden == 'V')
			cout << "Vegetables";
		else if (garden == 'F')
			cout << "Flowers";

		cout <<" in the garden ";
		cout<<"will NOT be watered";
	}

	cin.ignore();
	return 0;

}
Last edited on
separating all of the not watered really helped a ton! thanks man
Topic archived. No new replies allowed.