0

The following C++ code:

if (a != '0.5' || a != '0.6')
{
    cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}

I have also tried

if ((a != '0.5') || (a != '0.6'))
{
   cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}

and tried

if ( !(a== '0.5') || !(a==0.6)
{
     cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}

Receives a number from a user and checks if the number is 0.5 or 0.6; if it is it should execute as a false statement but if it's any other number it should execute as true. However, it keeps executing as true though it should execute as false when I enter 0.5 or 0.6. This is different when I use an else if statement in which it works fine that is:

if (a != 0.5)
{
    //what to do if true.
}
else if (a != 0.6)
{
    //What to do if this is true and the first id wrong.
}
else
{
    //What to do if none are true.
}

Why can't the != execute in the if statement?

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
urbanslug
  • 146
  • 1
  • 8

6 Answers6

3

The logic is wrong. You are currently checking whether a number is not 0.5 or not 0.6 at the same time; all numbers will pass that test. You need to replace the logical-or (||) with a logical-and (&&).

Also, you need to remove the single quotes from the numbers otherwise you are creating a multicharacter literal which has an implementation defined value.

if (a != 0.5 && a != 0.6)
{
    cout << "The answer is neither 0.5 nor 0.6 " << '\n';
}
Marlon
  • 19,924
  • 12
  • 70
  • 101
2

a is supposed to be a float or a string ? In either way, this syntax is wrong '0.5', if it's a string use double quotes. Don't compare with float/double for (in)equality, because of the internal representation this won't work as you expect, see how-to-correctly-and-standardly-compare-floats.

Community
  • 1
  • 1
PW.
  • 3,727
  • 32
  • 35
1

What's the type of a?

std::string

suppose:

std::string a;
std::cin >> a;

then the following code works:

if (a == "0.5" || a == "0.6")
{
    // Do something when it's true
}
else
{
    // Do something when it's false
}

Although "0.5" and "0.6" are const char*, they will convert to std::string, so it works well.

const char *

char a[BUFSIZE];
std::cin >> a;
if (strcmp(a, "0.5") == 0 || strcmp(a, "0.6") == 0)
{
  // Do something when it's true
}
else
{
  // Do something when it's false
}

You could use strcmp to compare c-style string

float / double

When you are comparing floats, you may meet precision problems. You could write some functions or a Float class to solve the problem. Like this:

const double EPS = 1e-8;
inline bool FloatEqual(const double lhs, const double rhs)
{ return std::fabs(rhs-lhs) <= EPS; }
int main()
{
  double a;
  std::cin >> a;
  if (FloatEqual(a, 0.5) || FloatEqual(a, 0.6))
  {
    // Do something when it's true
  }
  else
  {
    // Do something when it's false
  }
}

By the way

It's interesting to find that the following statements are equal

if (a == "0.5" || a == "0.6")
if (!(a != "0.5" && a != "0.6"))
abcdabcd987
  • 2,043
  • 2
  • 23
  • 34
0

remove the single quotes from 0.5 and 0.6 and you should be using &&(AND) not ||(OR) i.e.: if (a != 0.5 && a != 0.6)

okay_google
  • 339
  • 4
  • 17
0

It must be

if (a != 0.5 && a != 0.6)
Riz
  • 9,703
  • 8
  • 38
  • 54
0

If a is a char then odd things will happen when you do somthing like:

char a = '0.5';
cout << "A: " a << endl;

This will output (on my compiler, cant be sure about any others):

A: 5

And:

char a = '0.5';
if (a == '5')
    cout << "yey" << endl;
else
    cout << "oops" << endl;

This will output:

yey

But i think this behaviour is undefined (i don't know for sure). At any rate it won't do what you seem to think it will.

Secondly i think your logic is wrong:

You are saying:

If not 0.5 OR not 0.6

Where i think you mean

If not 0.5 AND not 0.6

if (a != '0.5' && a != '0.6')
sji
  • 1,877
  • 1
  • 13
  • 28