Basic Programming Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeedHelp666
    New Member
    • Oct 2006
    • 3

    Basic Programming Question

    Hello fellow programmers, I am having a problem with a program I need to write for school. I have it written as follows:
    #include <iostream>
    #include<cmath>
    #define PI 3.141592;
    using namespace std;
    int main()
    {
    double a;
    double b;
    double c;
    double n;
    double m;
    double p;
    double q;

    cout<<"enter value of a\n";
    cin>>a;
    cout<<"enter value of b\n";
    cin>>b;

    c = sqrt ((a*a) + (b*b));
    n = acos (a) * 180/PI;
    m = acos (b) * 180/PI;
    p = acos (a) * PI/180; //This is where an error occurs.
    q = acos (b) * PI/180; // This is where second error occurs.

    Then it gives values. The problem is that when I try to compile it, it says
    there is a syntax error on the indicated lines above before the ; token.
    Im new at this and the answer is probably really simple, so I'm appologizing
    in advance for taking away your precious time when you could be
    creating new levels for CS. Any help would be greatly appreciated. Thanx
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    PI should be declared:
    Code:
    static const double PI = 3.141592;
    The define keyword is used for something different. You may be getting your languages and keywords mixed up.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by D_C
      PI should be declared:
      Code:
      static const double PI = 3.141592;
      The define keyword is used for something different. You may be getting your languages and keywords mixed up.
      I think that's a little harsh

      The actual error is that you have used

      #define PI 3.141592;

      you have put a ; at the end of the #define and since the preprocessor uses #defines as text subsitutions you end up with a ; in the middle of your equation.

      You could simply use

      #define PI 3.141592

      notice I have removed the ;



      D_C raises a good point (it's just I think in slightly the wrong manor). From the headers in your code you would appear to be writing a C++ program. In C++ it is considered best pratice to define actually as const variables as this provides type checking (among other advantages) rather than using #defines.

      The main objection to using const variables in C is that a lot of the time they are used for defining array sizes and you have to use a constant iteral in that situation in C, a constant variable doesn't work e.g.

      Code:
      static const int BUFFER_SIZE = 100;
      char buffer[BUFFER_SIZE];
      This code compiles in C++ but you will get an error compiling it in C


      This works in both languages (C and C++)

      Code:
      #define BUFFER_SIZE 100
      char buffer[BUFFER_SIZE];

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Harsh may not be the right word, perhaps ignorant. I knew that #define wasn't necessary, and I assumed that it was used for something different. Of course, I didn't say what different meant because I didn't know. I thought #define might be for header files (#ifndef HEADER #define HEADER).

        He was close, probably closer than I. No attack meant at all :).

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by D_C
          Harsh may not be the right word, perhaps ignorant. I knew that #define wasn't necessary, and I assumed that it was used for something different. Of course, I didn't say what different meant because I didn't know. I thought #define might be for header files (#ifndef HEADER #define HEADER).

          He was close, probably closer than I. No attack meant at all :).
          No "attack" taken and there was none in return, just trying to make sure that the C/C++ language isn't misrepresented :D I used the word "harsh" because I thought you were expressing an opinion.


          As far as I am aware #define can be used for 4 things

          defining constants, as above

          include protection on header files (as you have just mentioned)

          alteration of functionality within the code, altering function declarations (between DEBUG and RELEASE for instance) and having build time compile options for program functionality (ASSERT macro is a simple example)

          defining function like macros

          #define MAX(a,b) (((a)>(b))?(a): (b))

          Although these can (and the one I've given does) have side effects (either a or b is evaluated twice so MAX(i++, 6) may not do what you think it does).

          Comment

          • risby
            New Member
            • Sep 2006
            • 30

            #6
            Originally posted by Banfa
            I think that's a little harsh

            D_C raises a good point (it's just I think in slightly the wrong manor).
            I don't want to seem harsh but I think you mean 'manner' (he he)

            Comment

            Working...