Thread: What's wrong in this code?

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    12

    What's wrong in this code?



    Edit: The code is working now.


    Hi all,

    This is my first post here. I'm learning C++ by myself, and I'm using the free version of Visual Studio. I have the following code taken from a tutorial, but it gives me an error. I'm not sure why? It says that '*' operand must be a pointer. What does that mean? The code is

    Code:
    // Using the define directive
    
    #include<iostream>
    
    
    using namespace std;
    
    
    #define PI 3.14 // it was #define PI 3.14;
    
    
    int main()
    {
        double radius = 5.0;
        double circle;
    
    
        circle = 2 * PI * radius; //The error is here
    
    
        cout << circle;
    
    
        cin.get();
    
    
        return 0;
    }
    Any help will be appreciated.

    Thanks
    Last edited by CNewProg; 08-30-2016 at 06:56 PM. Reason: OK. I found it. It was that I put a semicolon at the end of the #define directive

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In a C++ program you really should avoid using macros (#define) whenever possible. In this case use a const qualified variable instead.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    In a C++ program you really should avoid using macros (#define) whenever possible. In this case use a const qualified variable instead.

    Jim
    Why? and how and where to define const qualified variable? Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Something like

    const double PI = 3.14159;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why?
    The biggest reason is that const qualified variables are type safe and macros are not. In C++ macros are usually considered a bad practice because there are usually other ways of implementing the macro that are safer and easier to understand. Also IMO the compiler error messages involved in malformed macros are usually harder to understand.

    and where to define const qualified variable?
    In this case you could use the const qualified variable in the same location that you used the define, in the global scope. However unlike the macro you can also use this const defined variable inside your function eliminating the use of the global variable altogether.


    Jim

  6. #6
    Registered User
    Join Date
    Aug 2016
    Posts
    12
    OK, good to know. Thanks all for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code!!!
    By agnit.thebest in forum C Programming
    Replies: 5
    Last Post: 07-07-2010, 10:18 PM
  2. What is wrong with my code?
    By dakarn in forum C Programming
    Replies: 6
    Last Post: 10-14-2008, 05:16 AM
  3. What's wrong with this code...?
    By The SharK in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2005, 10:27 AM
  4. What is wrong with this code??? Can U help??
    By kickass in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 03:34 AM
  5. What is wrong with the code??
    By xeneize in forum C++ Programming
    Replies: 13
    Last Post: 10-28-2002, 07:13 PM

Tags for this Thread