By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Basic Programming Question

P: 3
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
Oct 2 '06 #1
Share this Question
Share on Google+
5 Replies


100+
P: 293
D_C
PI should be declared:
Expand|Select|Wrap|Line Numbers
  1. static const double PI = 3.141592;
The define keyword is used for something different. You may be getting your languages and keywords mixed up.
Oct 2 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
PI should be declared:
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. static const int BUFFER_SIZE = 100;
  2. char buffer[BUFFER_SIZE];
  3.  
This code compiles in C++ but you will get an error compiling it in C


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

Expand|Select|Wrap|Line Numbers
  1. #define BUFFER_SIZE 100
  2. char buffer[BUFFER_SIZE];
  3.  
Oct 2 '06 #3

100+
P: 293
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 :).
Oct 3 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
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).
Oct 3 '06 #5

risby
P: 30
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)
Oct 3 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.