Thread: problem with program number divisible by 10? using macros

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    problem with program number divisible by 10? using macros

    Hello,
    I just recently started to learn c++ programming, i'm using Practical C++ Programming book by Steve Oualline. I came across the programming exercise-Write a macro that returns true if its parameter is divisible by 10 and false otherwise. I've written a code but can't figure out what is wrong with it. I also did a google check on the errors but still no luck.

    Here's is the code below and also errors associated with it
    Code:
    #include <iostream>
    #define MACR(x) \
    { \
            if(x%10==0) \
        std::cout<<"True"; \
        else \
        std::cout<<"False"; \
    }
    int main()
    {
        int x;// x is the number user inputs to check whether it is divisible or not
        std::cout<<"\nI/P the number: ";
        std::cin>>x;
        std::cout<<"\nis the number divisible by 10: "<<MACR(x);
        return(0);
    }
    Error log:
    14|error: expected primary-expression before '{' token|
    14|error: expected ';' before '{' token|
    ||=== Build finished: 2 errors, 0 warnings ===|

    thanks for any help.

  2. #2
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    its wrong because its generating something like this

    Code:
    std::cout<<"\nis the number divisible by 10: "<< {
                                                           if(x%10==0)
                                                       std::cout<<"True";
                                                       else \
                                                       std::cout<<"False";
                                                         }
    just put your macro on the next line after the ";" and it should work

    like this

    Code:
    std::cout<<"\nis the number divisible by 10: ";
        MACR(x);
    Last edited by Dante Wingates; 06-23-2010 at 02:44 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the book posed the question with the wording that you used, then it is misleading. Macros do not return values.

    One way to write this macro is:
    Code:
    #define IS_DIV_BY_10(x) (((x) % 10 == 0) ? true : false)
    But if you try to use it like this:
    Code:
    std::cout << "\nis the number divisible by 10: " << IS_DIV_BY_10(x);
    It should compile, but the output might not be exactly what you expect.

    That said, because macros should normally be avoided in C++, I decided to do a quick check of reviews of this book. One reviewer stated:
    Quote Originally Posted by IP Freely
    The book exhibits (and therefore disseminates) numerous, fundamental misunderstandings of C++ features, design, and programming philosophy. Why even mention macros and the pre-processor, structures, unions, and bitfields in a C++ book that neglects data encapsulation, object-oriented design, memory management idioms, virtual functions and inheritance, templates, and other basic language features and philosophy? These are not merely consequences of the publication date.
    If this is true, then you would be better off with another book such as Accelerated C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by Dante Wingates View Post
    its wrong because its generating something like this

    Code:
    std::cout<<"\nis the number divisible by 10: "<< {
                                                           if(x%10==0)
                                                       std::cout<<"True";
                                                       else \
                                                       std::cout<<"False";
                                                         }
    just put your macro on the next line after the ";" and it should work

    like this

    Code:
    std::cout<<"\nis the number divisible by 10: ";
        MACR(x);
    I started to doubt in my logic itself but thanks to you, it worked!!

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by laserlight View Post
    If the book posed the question with the wording that you used, then it is misleading. Macros do not return values.

    One way to write this macro is:
    Code:
    #define IS_DIV_BY_10(x) (((x) % 10 == 0) ? true : false)
    But if you try to use it like this:
    Code:
    std::cout << "\nis the number divisible by 10: " << IS_DIV_BY_10(x);
    It should compile, but the output might not be exactly what you expect.

    That said, because macros should normally be avoided in C++, I decided to do a quick check of reviews of this book. One reviewer stated:

    If this is true, then you would be better off with another book such as Accelerated C++.
    I really can't say the difference between a good book and a bad one 'cause I don't know what should be learnt by beginner and what not. This book was referred by this website but I was suppose to study this after reading C++ without fear. But since, I couldn't get my hand on that copy, i bought steve oualline.

    I'll check out the book you mentioned if it is available in the stores. If you have any other books on your mind which can atleast give me a good overview & idea of what I'm reading if not best knowledge, please lemme know.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Accelerated C++ is IMO one of the best beginner books of C++ available. It gives you a quick introduction into the high-level facilities of C++ that allows you to get a quick overview on the language and finally, to get started writing programs faster.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program for generating combination
    By ymadhusoodan in forum C Programming
    Replies: 13
    Last Post: 05-18-2010, 09:54 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM