-2

The else if not seem to work, no matter what information I enter I only receive the first sentence in the if statement. I've tried many things but none seems to work.
I need if else statement to work because my program is like a job interview and based on what information I enter I should get if im accepted or not.

Here is my code:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    setlocale(LC_ALL,"");
    //Variabler och strings

    string kön;
    string barn;
    int stopp;
    int ålder;

    //Frågor
    cout << "Vad har du för kön? (Kvinna/Man)" << endl;
    cin>>kön;
    cout << "Hur gammal är du?" << endl;
    cin>>ålder;
    cout << "Har du barn?" << endl;
    cin>>barn;

    if(kön=="Kvinna" || "kvinna" && ålder<30 && barn=="Nej" || "nej")
    {
        cout << "Du är kvalificerad för jobbet eftersom du är under 30 och har inte barn";
    }
    else if (kön=="Man" || "man" && ålder>30 && barn=="Ja" ||"ja")
    {
         cout << "Du är inte kvalificerad eftersom du har barn och du är över 30";
    }
    else if (kön=="Kvinna" || "kvinna" && ålder>30 && barn=="Ja" || "ja")
    {
        cout << "Du är inte kvalificerad eftersom du är kvinna, har barn och du är över 30";
    }
    else if (kön=="Man" || "man" && ålder<30 && barn=="Nej" || "nej")
    {
        cout << "Du är kvalificerad för jobbet eftersom du är man, under 30 och har inte barn";
    }
    else if (kön=="Kvinna" || "kvinna" && ålder>30 && barn=="Nej" || "nej")
    {
        cout << "Du är inte kvalificerad eftersom du är kvinna och du är över 30";
    }
    else if (kön=="Man" || "man" && ålder<30 && barn=="Ja" || "ja")
    {
        cout << "Du är kvalificerad eftersom du är man och under 30";
    }
    else if (kön == "Man" || "man" && ålder>30 && barn == "Nej" || "nej")
    {
        cout << "Du är kvalificerad för jobbet eftersom du är man och du har inte barn";
    }
    
    cin>>stopp;
    return 0;
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
Ekkorojew
  • 1
  • 1
  • 1
    What do you think `if (kön=="Kvinna" || "kvinna"` does? Check the [operator precedence](https://en.cppreference.com/w/cpp/language/operator_precedence). – G.M. Nov 13 '21 at 13:25
  • 5
    `kön=="Kvinna" || "kvinna"` doesn't do what you think it does. You want `kön=="Kvinna" || kön=="kvinna"`. Also note that `&&` has higher precedence than `||`. Also, I strongly suggest naming variables in English, or at least not using any non-ASCII characters in the names. – HolyBlackCat Nov 13 '21 at 13:25
  • Does this answer your question? [How do I check if a variable is not equal to multiple things in C++?](https://stackoverflow.com/q/51631573/11683) – GSerg Nov 13 '21 at 13:33
  • As G.M and HolyBlackCat have commented `kön=="Kvinna" || "kvinna"` is 100% certain to do something different than you expect (in fact, it is *guaranteed* to test true in C++). Please explain what you believe `kön=="Kvinna" || "kvinna"` does *and* how you reached that belief. I request that, since a lot of similar questions come up in C++ forums on SO (and elsewhere), often with people adamant that their code should do something different than the C++ standards mandate. Understanding what you expect and why you expect it will help others provide sensible responses to such questions. – Peter Nov 13 '21 at 13:38

1 Answers1

-3

If your first statement is true you will get the output from 'if" only. How can you expect it will reach else block also? Your first statement is always true irrespective of the values. Because any non-zero value will be true in C/C++. In C++ (x=="A" || "B") will always return true. Because "B" is taken as true.

Prabir Ghosh
  • 430
  • 3
  • 7