szhu
szhu

Reputation: 29

Why is my if else statements not working and outputting the right index from an array?

This code is supposed to output the right color from the user's input. The user's input is stored in an array. However, the else statements are not executing/working.

string eyecolor;
string colorcin[4];

void color() {
  cout << "I am going to ask you for 4 colors." << endl;
  for (int a = 0; a < 4; a++) {
    cout << "Color " << a + 1 << ": ";
    cin >> colorcin[a];
  }
}

void eyes() {
  cout << "What color are your eyes? ";
  cin >> eyecolor;
  if (eyecolor == "blue" || "Blue" || "green" || "Green" || "hazel" ||
      "Hazel") {
    cout << "Your favorite color is " << colorcin[0] << endl;
  } else {
    cout << "Your favorite color is " << colorcin[1] << endl;
  }
}

int main() {
  color();
  eyes();
}

Upvotes: 1

Views: 78

Answers (4)

ProXicT
ProXicT

Reputation: 1933

Here, a lot of the answers explain why your approach isn't correct in C++, but nobody has yet provided a solution that solves this cumbersome problem of repeating one variable multiple times.

That being said, I won't cover the details of what is wrong in your code, instead, I will provide you with my solution that makes things a lot easier in cases where you need to use this construct often:

template <typename T, std::size_t TSize>
struct AnyOfThis { T values[TSize]; };

template <typename TFirst, typename... TOthers>
auto anyOf(TFirst&& first, TOthers&&... others) {
    return AnyOfThis<std::decay_t<TFirst>, 1 + sizeof...(others)>{std::forward<TFirst>(first), std::forward<TOthers>(others)...};
}

template <typename T, std::size_t TSize>
bool operator==(const T value, const AnyOfThis<typename std::decay<T>::type, TSize>& anyOfThis) {
    return std::find(std::begin(anyOfThis.values), std::end(anyOfThis.values), value) != std::end(anyOfThis.values);
}

Basically, it creates a static array from a variadic function. Then there is another function which serves as a comparator, which takes the value you want to compare and looks for this value in the array.

The use-case reads fairly well, too:

if (1 == anyOf(1, 2, 3)) {
    // do stuff
}

And as you can see in the demo below, with one additional code sugar, you can also put string literals into the anyOf function and it will automatically convert them to std::string so you can compare them like this:

std::string eyecolor("blue");
if (eyecolor == anyOf("blue", "green", "brown")) {
    // do stuff
}

LIVE DEMO

Upvotes: 1

Khouri Giordano
Khouri Giordano

Reputation: 1461

You have not specified your boolean expressions correctly. For instance, you wrote:

if (haircolor == "blond" || "Blond" || "Red" || "red" || "Auburn" || "auburn")

This should be:

if (haircolor == "blond" || haircolor == "Blond" || haircolor == "Red"
        || haircolor == "red" || haircolor == "Auburn" || haircolor == "auburn")

A literal character array such as "Blond" will be implicitly converted to a boolean true value.

Upvotes: 2

dbush
dbush

Reputation: 225344

This is not how you compare multiple values:

if (eyecolor == "blue" || "Blue" || "green" || "Green" || "hazel" ||
      "Hazel") {

What this actually does is first compare eyecolor with "blue". If this is false, then "Blue" is evaluated in a boolean context. Because it is a string constant, it decays to a pointer to its first element, and because that pointer cannot be NULL, it evaluates to true, so this expression will always be true.

You need to explicitly compare eyecolor with each possible value:

if (eyecolor == "blue" || eyecolor == "Blue" || eyecolor == "green" || 
    eyecolor == "Green" || eyecolor == "hazel" || eyecolor == "Hazel") {

And the same for haircolor.

Upvotes: 2

Sadaf Shafi
Sadaf Shafi

Reputation: 1458

do you mean hair(); function? its working fine see

Pick a number betweem 1-100: 1                                                                                                
Pick another number between 1-100: 2                                                                                          
I am going to ask you for 4 colors.                                                                                           
Color 1: 1                                                                                                                    
Color 2: 2                                                                                                                    
Color 3: 3                                                                                                                    
Color 4: 4                                                                                                                    
What color is your hair? blond                                                                                  
Your favorite color is 3                                                                                                      
Have a nice day. 

Upvotes: 1

Related Questions