Multiple conditioned if statements

Jul 1, 2010 at 4:32pm
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?
Jul 1, 2010 at 4:35pm
What's the type of variable "sex"?
Jul 1, 2010 at 4:37pm
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 Jul 1, 2010 at 4:37pm
Jul 1, 2010 at 4:37pm
Also, this -> military = "yes" should probably be -> military == "yes"
Jul 1, 2010 at 4:44pm
I have sex as a string.
Jul 1, 2010 at 4:46pm
@ m4ster - This worked! Thanks so much!
Jul 1, 2010 at 4:51pm
Well, don't give all the credit to me... spaggy also pointed out this problem...
Last edited on Jul 1, 2010 at 4:51pm
Jul 1, 2010 at 4:56pm
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;
		}
}
Jul 1, 2010 at 5:03pm
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;
Jul 1, 2010 at 5:03pm
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
Jul 1, 2010 at 5:05pm
Thanks m4ster! :P
Jul 1, 2010 at 5:08pm
Thanks guys, both ways work and I understand both! Appreciate it greatly!
Jul 1, 2010 at 5:13pm
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 Jul 1, 2010 at 5:14pm
Jul 1, 2010 at 5:19pm
Don't worry, I won't bite you or anything... :)
Jul 1, 2010 at 5:38pm
That can be further simplified. Note that all conditions depend on age>=18.
Topic archived. No new replies allowed.