Multiple conditions in if statement

I produced this code

if ((16 <= age && race == Vulcan ) ||
(17 <= age && race != Romulan ))

and this runs but the result is not what I want.

The requirement I'm trying to get it to say is:
Minimum age is 16 for Vulcans, 17 for anyone else.
Race must not be Romulan

When I take out the race part, my nested if and else statements work. The race part is the issue but I cannot figure it out.

I changed it to many variants with no avail like
if ((16 <= age && Vulcan ) ||
(17 <= age && !Romulan ))

if ((16 <= age && Vulcan ) ||
(17 <= age && !(race =Romulan) ))

and adding more parentheses.
Hello heeyehan,

Questions:

I am fairly certain that "age" is defined as an "int", but what is "race" defines as (what type)? Then is Vulcan a variable or a constant string? If it is a constant string it should be in quotes ("").

I think the first example is on the right track, but (Vulcan) and (Romulan) should be in quotes.

My thoughts for now.

Hope that helps,

Andy
Hi Andy,

I had Vulcan and Romulan as a int so I changed it to a string and added the quotes but it came out to an error.
The error said "ISO C++ forbids comparison between pointer and integer.
I also have the user inputting the race.

cout << "Race (Human, Vulcan, Klingon, Romulan): ";
cin >> race;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// the requirement is met for anyone if
//     the race is not Romulan and the age is 17 or more
bool requirements_met = (race!=Romulan) && (age>16) ;

// if age is 16, the requirement is also met provided the race is Vulcan
if( age==16 ) requirements_met = (race==Vulcan) ;

if( requirements_met )
{
    // the requirement is met
    // ...
}
else
{
    // it not met
    // ...
}
JLBorges:

I tried that and I kept getting that Romulan is meeting the requirement.
I appreciate the help btw.
I have "dumbed" down my if statement to

if ( race != Romulan)

and yet it keeps on getting accepted. I don't understand what I'm doing 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
#include <iostream>
#include <string>
using namespace std;


bool isValidAge( string race, int age )
{
   if ( race == "Romulan" ) return false;
   int minAge = ( race == "Vulcan" ? 16 : 17 );
   return age >= minAge;
}


int main()
{
   string race = "Vulcan";
   for ( int age = 15; age <= 20; age++ )
   {
      cout << race << '\t' << age << '\t' << boolalpha << isValidAge( race, age ) << '\n';
   }

   race = "Viking";
   for ( int age = 15; age <= 20; age++ )
   {
      cout << race << '\t' << age << '\t' << boolalpha << isValidAge( race, age ) << '\n';
   }

   race = "Romulan";
   for ( int age = 15; age <= 20; age++ )
   {
      cout << race << '\t' << age << '\t' << boolalpha << isValidAge( race, age ) << '\n';
   }
}


Vulcan	15	false
Vulcan	16	true
Vulcan	17	true
Vulcan	18	true
Vulcan	19	true
Vulcan	20	true
Viking	15	false
Viking	16	false
Viking	17	true
Viking	18	true
Viking	19	true
Viking	20	true
Romulan	15	false
Romulan	16	false
Romulan	17	false
Romulan	18	false
Romulan	19	false
Romulan	20	false


If you want a single-line version it would be the horrendously unreadable
return ( ( race != "Romulan" ) && ( age >= 17 || ( race == "Vulcan" && age == 16 ) ) );


TBH, I'd be inclined to make your race an enum rather than a string, though.

Last edited on
Thank you to everyone that helped.

I was able to fix my problem with bits and pieces of everyone's aid!
The problem was some strings and having my if statements unnecessarily difficult.
Hello heeyehan,

Sorry for the delay, I could not stay up any later last night.

I believe you misunderstood my question. Without the rest of the program I could only guess at what the variable "race" was defined as and how "Vulcan" was being used. I can see now that putting "Vulcan" in quotes would give an error.

BTW with the lines:
1
2
cout << "Race (Human, Vulcan, Klingon, Romulan): ";
cin >> race; 

I would be inclined to type in the whole name, but with the "cin" expecting "race" to be input as a number "cin" would fail and no longer be usable in the rest of the program. If you want the user to enter a number than give that hint like "Human = 1". Just a thought.

I think the examples presented should give you an idea what needs to be done.

Andy
Topic archived. No new replies allowed.