Like Tree1Likes

learning to use #define

This is a discussion on learning to use #define within the C++ Programming forums, part of the General Programming Boards category; Hi My elementary programming skills which I acquired not long ago are very much rusty these days. I was practicing ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    learning to use #define

    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 line at #17 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 all the help.

    Code:
    //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;
    
        area = 2*pi*r;
    
        cout << "area is: " << area << endl;
    
        system("pause");
        return 0;
    }
    Errors:
    Code:
    17|error: expected primary-expression before '=' token|
    17|error: invalid type argument of 'unary *'|
    Last edited by jackson6612; 09-20-2011 at 10:21 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    This:
    Code:
    #define pi = 3.142;
    should be:
    Code:
    #define pi 3.142
    Actually, it should be:
    Code:
    const double pi = 3.142;
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Light.

    Please also help me with this one. I remember when I started posting here people would tell me that my code isn't properly organized, like braces are not aligned etc.

    By the way, is the code in my first post above properly aligned? Please let me know.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Yes, in terms of indentation it is fine.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Once again, thanks.

    I replaced the line "#define pi = 3.142" with "#define pi 3.142" as you suggested. But I still get one error about the line #17. What's still wrong? Would you please give it a look? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    My guess is try changing your float variables to double.

    Also, correct me if I'm wrong but is it better coding practice to declare defines outside of main. For example:

    Code:
    #define pi 3.142
    
    using namespace std;
    
    int main()
    {
        double r, area;
     
        cout << "enter radius"; cin >> r;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by jackson6612
    I replaced the line "#define pi = 3.142" with "#define pi 3.142" as you suggested. But I still get one error about the line #17. What's still wrong? Would you please give it a look?
    What error did you get? I tested just in case I made a mistake, and I can say for certain that I made no mistake.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    No, it didn't fix it; I mean changing float to double. I still get the same error about line #17: 17|error: invalid type argument of 'unary *'|

    Someone please help me. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by jackson6612
    No, it didn't fix it; I mean changing float to double. I still get the same error about line #17: 17|error: invalid type argument of 'unary *'|
    This is the program that I tested:
    Code:
    //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;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }
    It does not produce the error for me, but according to you, it does. Try it. What error do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    m_pi(3) - Linux man page :

    #define M_PI 3.141592653589793238462643

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by laserlight View Post
    This is the program that I tested:
    Code:
    //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;
     
        area = 2*pi*r;
     
        cout << "area is: " << area << endl;
     
        system("pause");
        return 0;
    }
    It does not produce the error for me, but according to you, it does. Try it. What error do you get?
    I'm extremely sorry, Light. It works for me now. Actually this is what happened. In your first post in this thread you told me to use:
    Code:
    #define pi 3.142
    . I thought you just missed the semicolon at the end out of hurry because you have to help so many people. So, I was using semicolon at the end which gave rise to error. By the way, why don't we use semicolon at the end of it? Please tell me. Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    #define doesn't need a semicolon at the end because the preprocessor just replaces the word with an expression. Semicolons would be placed in the rest of the code.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by jackson6612
    By the way, why don't we use semicolon at the end of it?
    Because preprocessor macros are replaced using fairly dumb substitution. With the semi-colon, this:
    Code:
    area = 2*pi*r;
    effectively becomes:
    Code:
    area = 2*3.142;*r;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, whiteflags.

    So, anything which follows "#" or hash sign is called preprocessor directive? One example is use of #include with header files.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    That is correct.
    But also because the preprocessor is a dumb text replacer at many times, it is not recommended that you use #define for simple constants.
    The errors you just got could easily have been caught earlier and fixed more easily had you used a constant double/float declaration like laserlight showed you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define
    By Johnathan1707 in forum C Programming
    Replies: 3
    Last Post: 08-02-2009, 09:10 AM
  2. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  3. #define
    By tikelele in forum C Programming
    Replies: 2
    Last Post: 12-01-2007, 10:43 PM
  4. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  5. API learning reference (for those who are learning)
    By steve_i83 in forum Windows Programming
    Replies: 1
    Last Post: 11-04-2001, 12:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21