#why does the code gets run anyway and ignores the if condition in main

18 messages · Page 1 of 1 (latest)

sterile bolt
#
{
   cout << "would you like to turn on a numbers function? (y/n): ";
   string numtrigger;
   cin >> numtrigger;

   if (numtrigger == "y" || "yes")
        {
        int n1, n2;
        cout << "determine a beginning and ending numbers: ";cin>>n1>>n2;

    while(n1 > n2 || !cin)
    {
        cout<<"invalid input or you can't really have the first number higher try again: ";cin>>n1>>n2;
    }
    numberinbetween(n1, n2);
        }
    }

so i have an if and even when the condition is false the code runs anyway, why?

runic idolBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

limpid steppe
#

numtrigger == "y" || "yes" is not doing what you think its doing

#

;compile

#include <iostream>
int main()
{
int num = 1;
if (num == 0 || 2)
    std::cout << "wtf" << std::endl;
return 0;
}
late muskBOT
#
Program Output
wtf
limpid steppe
#

the number is not 0 and not 2

#

but wtf is printed

#

thats because it isnt evaluated as num == 0 or num == 2 but as num == 0 or 2

#

2 is true and so this if statement is true too

sterile bolt
#

@limpid steppe so what do i put instead

limpid steppe
#

well what i said

#

in my example it would be

#

num == 0 || num == 2

sterile bolt
#

ohh didn't see that

#

yeah

#

!solved

runic idolBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] why does the code gets run anyway and ignores the if condition in main