If/Else statement; Expected Expression Error

Oct 14, 2014 at 5:27pm
I'm trying to compile this simple program in Xcode, however, it keeps giving me an error saying expected expression. I thought I had my brackets in the correct place, but cannot find where I'm wrong

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
cout<<"Please enter if you're 'Male' or 'Female': "<<endl;
    cin>>gender;
    
    cout<<"Please enter your activity level as either 'Active' or 'Inactive': "<<endl;
    cin>>activityLevel;
    
    if (gender == "Female")
    {
        
        if (activityLevel == "Active")
            
        daiyCalories = weight * 12;
        {
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended daily calories is "<<daiyCalories<<endl;
        }
    
        else (activityLevel == "Inactive")
        
        daiyCalories = weight * 10;
        {
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended daily calories is "<<daiyCalories<<endl;
        }
    }
    
    if (gender == "Male")
    {
        if (activityLevel == "Active")
        
        daiyCalories = weight * 15;
        {
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended dailty calories is "<<daiyCalories<<endl;
        }
    
        else (activityLevel == "Inactive")
            
        daiyCalories = weight * 13;
        {
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended daily calories is "<<daiyCalories<<endl;
        }
        
    }
return 0;
}


Thanks for any help!
Oct 14, 2014 at 5:29pm
PS it's saying expected expression on each of the else statments
Oct 14, 2014 at 5:30pm
You can't put a condition after an "else" statement. Did you mean those to be "else if" statements?
Oct 14, 2014 at 5:34pm
I tried changing the "else" statements to "else/if", but it still gives the same results
Oct 14, 2014 at 5:50pm
Your if and else statements aren't connected.

For example, lines 10-12 is an if statement, and ends there. Then you have a block of regular code from 13-17 and else without an if on line 19. The pattern is repeated from line 31 downwards.

Also, as mentioned by MikeyBoy you need an else if, as shown below. Don't know if this is what you want to do or not, but formatting like this should help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (gender == "Female")
    {
        if (activityLevel == "Active")
        {
        daiyCalories = weight * 12;
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended daily calories is "<<daiyCalories<<endl;
        }
        else if (activityLevel == "Inactive")
        {
        daiyCalories = weight * 10;
        cout<<"You are "<<gender<<endl;
        cout<<"Your activity level is "<<activityLevel<<endl;
        cout<<"Your recommended daily calories is "<<daiyCalories<<endl;
        }
    }
    
Oct 14, 2014 at 5:58pm
@tipaye, that worked perfectly! thank you!
Topic archived. No new replies allowed.