0

I recently had problem with making a condition in my code (which is very simple btw). And I cant figure out why this code has not the output that I wanted it to have. Basically it is a code that gets an angle and tells you in which area of circle it will be. And the problem is how can I put all of conditions in that if statement. Should I use &-&&-|-||-,- (Surely the & is not very appropriate in this position)

#include <iostream>
using namespace std;
int main()
{
    int fa, a, r;
    //false angle, angle, round
    cout << "Enter the Angle: ";
    cin >> fa;
    a = fa % 360;
    cout << a;
    if ((a == 0) | (a == 90) | (a == 180) | (a == 270) | (a == 360) | (a == -90) | (a == -180) | (a == -360))
        cout << "Border dots." << endl;
    else {
        if (0 < a < 90 | -270 < a < 360) {
            cout << "1st Area." << endl;
        }
        else if (90 < a < 180 | -180 < a < -270) {
            cout << "2nd Area." << endl;
        }
        else if (180 < a < 270 | -90 < a < -180) {
            cout << "3rd Area." << endl;
        }
        else if (270 < a < 360 | 0 < a < -90) {
            cout << "4th Area." << endl;
        }
        else
            cout << "Wrong number!" << endl;
    }
    r = fa / 360;
    cout << "It is " << r << " round(s) and " << a << " Angle" << endl;
}
//17/05/2020

And sorry if I had difficulties in saying my problem.Because I don't know much about math and code phrases

darclander
  • 1,526
  • 1
  • 13
  • 35
Smercher
  • 3
  • 2
  • 5
    `|` is bitwise-or. Instead use `||` which is logical-or. – cigien May 17 '20 at 20:15
  • 14
  • 1
    @cigien Not really a problem with logical operands. It's of course *better* to use `||`, if only for short-circuit evaluation, but it won't cause a bug to use `|` instead in this case. –  May 17 '20 at 20:18
  • 2
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/430766) Also, get in the habit of *properly indenting your code*. – bitmask May 17 '20 at 20:20
  • 1
    I’m voting to close this question because solved in comments – anatolyg May 17 '20 at 20:43

2 Answers2

4

Let's consider the lines like this

if (0 < a < 90 | -270 < a < 360) {
//    ^^^^^           ^^^^^ 
}

Whether a is less than 0 or not, the result of 0 < a is a bool value which is then compared to 90. Given that both 0 and 1 are less than 90, (0 < a) < 90 is always true. You have to split the condition.

if ((0 < a  &&  a < 90) || (-270 < a  &&  a < 360)) {
//  ...
}

Of course, you could simplify the whole thing with a little more math.

#include <iostream>

int main()
{
    int fa, a, r;

    std::cout << "Enter the Angle: ";
    std::cin >> fa;
    a = fa % 360;
    std::cout << a << '\n';

    if ( a % 90 == 0 ) { 
        // ^^^^^^^^^  If it's divisible by 90...
        std::cout << "Border dots.\n";
    }
    else {
        // Consider the positive cases only
        int b = a > 0 ? a : a + 360;
        if ( b < 90 ) {
            std::cout << "1st Area.\n";
        }
        else if ( b < 180 ) {
            std::cout << "2nd Area.\n";
        }
        else if ( b < 270 ) {
            std::cout << "3rd Area.\n";
        }
        else if ( b < 360 ) {
            std::cout << "4th Area.\n";
        }
        else
            std::cout << "Wrong number!\n";
    }

    r = fa / 360;
    std::cout << "It is " << r << " round(s) and " << a << " Angle\n";
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
0
    #include <iostream>
using namespace std;
int main()
{
    int fa, a, r;
    //false angle, angle, round
    cout << "Enter the Angle: ";
    cin >> fa;
    a = fa % 360;
    cout << a;
    if ((a == 0) || (a == 90) || (a == 180) || (a == 270) || (a == 360) || (a == -90) || (a == -180) || (a == -360))
        cout << "Border dots." << endl;
    else {
        if (0 < a && a < 90 || -270 < a && a < 360) {
            cout << "1st Area." << endl;
        }
        else if (90 < a && a < 180 || -180 < a && a  < -270) {
            cout << "2nd Area." << endl;
        }
        else if (180 < a && a < 270 || -90 < a && a < -180) {
            cout << "3rd Area." << endl;
        }
        else if (270 < a && a < 360 || 0 < a && a < -90) {
            cout << "4th Area." << endl;
        }
        else
            cout << "Wrong number!" << endl;
    }
    r = fa / 360;
    cout << "It is " << r << " round(s) and " << a << " Angle" << endl;
}

Corrected the syntax problems.

Andrei Manolache
  • 766
  • 1
  • 8
  • 17