Multiple conditioned if statements

I can't seem to get this to run without errors.

1
2
if (sex == "m") && ((age >= 18) && (age <= 35)) && (military = "yes") || (pushups >= 50))
			cout << "Yes, " << name << ", you may apply." << endl;


This is my final trial and I get an error with the ||'s
1
2
if (((sex == "m") && ((age >= 18) && (age <= 35)) && ((military = "yes") || (pushups >= 50))))
			cout << "Yes, " << name << ", you may apply." << endl;


It's supposed to read it as:
If sex is male and age is between 18 and 35 and previous military experience OR you can do more than 50 pushups.

Any ideas?
What's the type of variable "sex"?
With the first attempt the issue is that you need one more bracket pair to enclose the whole if clause.

With the last attempt change (military = "yes") to (military == "yes")
Last edited on
Also, this -> military = "yes" should probably be -> military == "yes"
I have sex as a string.
@ m4ster - This worked! Thanks so much!
Well, don't give all the credit to me... spaggy also pointed out this problem...
Last edited on
Ok I just have one more problem, which is my output. It's giving me the answer to each if statement. Is there a way I apply the else statement to all of them in order to only produce one output?

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
#include <iostream>
#include <string>

using namespace std;

string name, military, sex;
	int age, pushups;

int main()
{
		cout << "What is your full name? ";
		getline(cin, name);
		
		cout << "How old are you? ";
		cin >> age;

		cout << "Were you ever in the military (yes/no)? ";
		cin >> military;

		cout << "How many pushups can you do in a row? ";
		cin >> pushups;

		cout << "Are you <m>ale or <f>emale? ";
		cin >> sex;

		if ((sex == "m") && ((age >= 18) && (age <= 30)))
			cout << "Yes, " << name << ", you may apply." << endl;

		else 
			cout << "Sorry, " << name << ", you are not eligible." << endl;


		if ((sex == "f") && ((age >= 18) && (age <= 32)))
			cout << "Yes, " << name << ", you may apply." << endl;
		else 
		{
			cout << "Sorry, " << name << ", you are not eligible." << endl;
		}

		if (((sex == "m") && ((age >= 18) && (age <= 35)) && ((military == "yes") || (pushups >= 50))))
			cout << "Yes, " << name << ", you may apply." << endl;
		else 
		{
			cout << "Sorry, " << name << ", you are not eligible." << endl;
		}

		if (((sex == "f") && ((age >= 18) && (age <= 40)) && ((military == "yes") || (pushups >= 30))))
			cout << "Yes, " << name << ", you may apply." << endl;


		else 
		{
			cout << "Sorry, " << name << ", you are not eligible." << endl;
		}
}
1
2
3
4
5
6
7
8
9
10
bool apply;

if ((sex == "m") && ((age >= 18) && (age <= 30))) apply=true;
else if ((sex == "f") && ((age >= 18) && (age <= 32))) apply=true;
else if (((sex == "m") && ((age >= 18) && (age <= 35)) && ((military == "yes") || (pushups >= 50)))) apply=true;
else if (((sex == "f") && ((age >= 18) && (age <= 40)) && ((military == "yes") || (pushups >= 30)))) apply=true;
else apply=false;

if (apply) cout << "Yes, " << name << ", you may apply." << endl;
else cout << "Sorry, " << name << ", you are not eligible." << endl;
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
#include <iostream>
#include <string>

using namespace std;

string name, military, sex;
	int age, pushups;

int main()
{
		cout << "What is your full name? ";
		getline(cin, name);
		
		cout << "How old are you? ";
		cin >> age;

		cout << "Were you ever in the military (yes/no)? ";
		cin >> military;

		cout << "How many pushups can you do in a row? ";
		cin >> pushups;

		cout << "Are you <m>ale or <f>emale? ";
		cin >> sex;

		if ((sex == "m") && ((age >= 18) && (age <= 30)))
			cout << "Yes, " << name << ", you may apply." << endl;

		else if ((sex == "f") && ((age >= 18) && (age <= 32)))
			cout << "Yes, " << name << ", you may apply." << endl;

		else if (((sex == "m") && ((age >= 18) && (age <= 35)) && ((military == "yes") || (pushups >= 50))))
			cout << "Yes, " << name << ", you may apply." << endl;

		else if (((sex == "f") && ((age >= 18) && (age <= 40)) && ((military == "yes") || (pushups >= 30))))
			cout << "Yes, " << name << ", you may apply." << endl;

		else 
		{
			cout << "Sorry, " << name << ", you are not eligible." << endl;
		}
}


Else if should solve that
Thanks m4ster! :P
Thanks guys, both ways work and I understand both! Appreciate it greatly!
More duplicate code elimination:
1
2
3
4
5
6
   if (((sex == "m") && ((age >= 18) && (age <= 30))) ||
       ((sex == "f") && ((age >= 18) && (age <= 32))) ||
       (((sex == "m") && ((age >= 18) && (age <= 35)) && ((military == "yes") || (pushups >= 50)))) ||
       (((sex == "f") && ((age >= 18) && (age <= 40)) && ((military == "yes") || (pushups >= 30)))))
        cout << "Yes, " << name << ", you may apply." << endl;
   else cout << "Sorry, " << name << ", you are not eligible." << endl;


Edit: ah, I just saw that this is more or less what m4ster r0shi posted.
Last edited on
Don't worry, I won't bite you or anything... :)
That can be further simplified. Note that all conditions depend on age>=18.
Topic archived. No new replies allowed.