HELP with ELSE statement

Im using an online executor. compileonline.com , so all i could see in the output was the "cout" and im not able to insert the answers/inputs :

So thanks to the help i got here before i was able to run these codes without error, BUT the ELSE statement i use to be supposed to show up when the two IF statements was not needed or rather when the requirements are met for the questions was not working (i think)

how should i put the else statement here corresponding to both the IF statements?

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
#include <iostream>
using namespace std;

int main()
{   
    
    
    
    int hb;
    cout<<"Height of boy (in inches):\n";
    cin>>hb;
    
    
    
    int hg;
    cout<<"Height of girl (in inches):\n";
    cin>>hg;
    
    int height;
    height= (hb-hg);
    if(height <= -2 || height >= 6)
    cout<<"Somebody has to grow up."<<endl;
  
    
    
    
    int wb;
    cout<<"Weight of boy (in pounds): \n";
    cin>>wb;
    
    int wg;
    cout<<"Weight of girl (in pounds): \n";
    
    int weight;
    weight=(wb-wg);
    if (weight <=5 || weight >= 20)
    cout<<"Somebody has to go on a diet."<<endl;
  


    else
    cout<<"You are both highly compatible.Go ahead and have a date."<<endl;
    


return 0;

}
Your else statement on line 41 only applies to the if on line 36. It has no relationship to the if statement on line 21.

The easiest way to fix this is to add a boolean flag.

21
22
23
24
25
  bool compatible = true;
  if(height <= -2 || height >= 6)
  {  cout<<"Somebody has to grow up."<<endl;
      compatible = false;
  }

36
37
38
39
40
41
42
  if (weight <=5 || weight >= 20)
  {  cout<<"Somebody has to go on a diet."<<endl;
    compatible = false;
  }

  if (compatible)
    cout << "You are both highly compatible.Go ahead and have a date." << endl;









Topic archived. No new replies allowed.