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

1. ## 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. 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);```

3. 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:
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++.

4. Originally Posted by Dante Wingates
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. Originally Posted by laserlight
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. 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.