Jump to content

learning to use #define

- - - - -

  • Please log in to reply
4 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

My elementary programming skills which I acquired not long ago are very much rusty these days. I was practicing with the code below but I received those two errors about the red line in the source code. Would you please help me?

I remember when I started posting here people would tell me that my code isn't properly organized, like braces are not aligned etc. They used to use a certain term. What is it? Please tell me. Thank you for the help.


//define_pi.cpp

// learning to use #define on Pi


#include <iostream>

#include <cstdlib>

#include <cmath>


using namespace std;


int main()

{

    float r, area;

    #define pi = 3.142;


    cout << "enter radius"; cin >> r;


    [B][COLOR="#FF0000"]area = 2*pi*r;[/COLOR][/B]


    cout << "area is: " << area << endl;


    system("pause");

    return 0;

}


Errors:

17|error: expected primary-expression before '=' token|

17|error: invalid type argument of 'unary *'|


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Well the term might be indentation. White Spaces are used to make your program more clear and readable.

---------- Post added at 12:16 AM ---------- Previous post was at 12:12 AM ----------

Put the #define directive in the begining of your progam.
One thing more i want to mention out is.

    #define pi = 3.142;

This isn't the way of writing #define directives. The correct form is,

    #define pi 3.142


I think i'm able to write a code for printing "Hello, World!". Proud of that!

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Note that macros are evil though.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
In C there are numerous places where #defines are your only option, for example (ignoring variable length arrays in C99), array[LEN] where LEN is a #define and not a constant. As well case statements,
const int foo = 2;
switch(...) {
  case foo:

Will not work, however if foo were a #define it would.

In C++ however, you can use constants in these places and the #define will cause unnecessary issues (it is replaced at compile time, it is not an actual variable in run time)

Unless you have a very good reason, you should be doing this, even at the global scope:
const float pi = 3.1415;

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, AK, Dutchman, Alexander.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users