Doesn't seem to work for no reason

When I run it, it says "expected primary-expression before '='". What is it talking about? Everything seems to check out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <math.h>
#define PI = 3.1415

int main()
{
    double fResult,fAngle;
    std::cout << "Enter angle" << std::endl;
    std::cin >> fAngle;
    fResult = cos( fAngle * PI /180.0 );
    std::cout << "The cos is" << fResult;

    return 0;
}
On line 3: Either remove the = or better change to:

const double PI = 3.1415;
You have defined PI as = 3.1415. That means all occurrences of PI in the code will be substituted by = 3.1415 without caring much about the rules of C++.

That means this line ...

 
fResult = cos( fAngle * PI /180.0 );

... will become ...

 
fResult = cos( fAngle * = 3.1415 /180.0 );

... which obviously is not correct.


You can easily fix the problem by removing the = symbol when defining PI but I think a better solution is to not use #define at all. It's better to define constants using the const keyword because it will not give you any surprises like this.

 
const double PI = 3.1415;
Last edited on
Oh my god thank you this language is horrifying for me.
Topic archived. No new replies allowed.