# Thread: I NEED #define HELP!!!!

1. ## I NEED #define HELP!!!!

Ok first let me just let you know I'm just starting to learn C++ so if this seems like a stupid question I'm sorry. But I guess we can't all be masters our first day. Ok so here is the prob....
The book I'm learning from (C++ Programming 101 by Greg Perry) wants me to:
Write a program that defines the 10 digits, 0 through 9, as literals ZERO through NINE. Add these 10 defined digits and store the sum in a variable named 'total' and print to screen the results.

I did another program sort of like this and it ran perfect it's first time compiled. But I have no clue what to do here. Here is the source code I've typed so far so you can see if I'm even on the right track. Oh and this code is just to get the literals to print I havn't done the part to add them yet cause I can't even get this to work. Well any suggestions or solutions would be great thanks.
---Source Code---
Code:
```//Review Exercises Pg 97 #3
#include <iostream.h>
#define 0 "ZERO"
#define 1 "ONE"
#define 2 "TWO"
#define 3 "THREE"
#define 4 "FOUR"
#define 5 "FIVE"
#define 6 "SIX"
#define 7 "SEVEN"
#define 8 "EIGHT"
#define 9 "NINE"
int main()
{
cout << 0"\n";
cout << 1"\n";
cout << 2"\n";
cout << 3"\n";
cout << 4"\n";
cout << 5"\n";
cout << 6"\n";
cout << 7"\n";
cout << 8"\n";
cout << 9"\n";

system ("Pause");
return 0;
}```

2. From the description I would say this is a more likely answer
Code:
```#include <iostream>
#define ONE 1
#define TWO 2
#define THREE 3

int main()
{
int sum = ONE + TWO + THREE;
cout << ONE << endl;
cout << TWO << endl;
cout << THREE << endl;
cout << "Sum: " << sum << endl;
}```
Of course you'll need to take it all the way

3. Hey thanks Thantos. I re-wrote it and it compiled and added fine. I figured thats how it should of been too when I started it but I guess the books wording threw me off. Thanks alot.
-T

4. Why not just use const instead, as i have heard defines are evil .
Code:
```#include <iostream>
using std::cout;
const int ONE = 1;
const int TWO = 2;

int main()
{
int sum = ONE + TW0;
cout<<sum;
return 0;
}```

5. Well thats what my book says to that not many programmers use it because 'const' is more managable but it said that #define does have it's advantages and well I'm just starting so I'm just tryin to learn everything I can. But it's cool cause the program compiled right but thanks for reply. I appreciate it.
-T

6. Not neccessarily evil. Like most of C/C++, preprocessor directives like #define are powerful. It can be a very useful tool. It can also show it's various pitfalls when you need them least

7. I ran this code:
#include <iostream>
using std::cout;
const int ONE = 1;
const int TWO = 2;

int main()
{
int sum = ONE + TW0;
cout<<sum;
return 0;
}
But this makes makes me this error, I can't figure out.
numeros.cpp
c:\numeros.cpp(8) : error C2065: 'TW0' : undeclared identifier
Error executing cl.exe.

numeros.exe - 1 error(s), 0 warning(s)

8. A classic zero-O replacement on the TW0 in this line:
int sum = ONE + TW0;

I hate bugs like that.

9. Code:
```
const int TWO = 2;

int main()
{
int sum = ONE + TW0;
```
Now do you see the problem? Or should I add color?
Code:
```
const int TWO = 2;

int main()
{
int sum = ONE + TW0;
```
Quzah.

10. >>int sum = ONE + TW0;
The letter O can be easily confused for the number 0. The define is TWO yet you used TW0. Feel free to change your viewing font if the two still look identical.

11. abyss:
i do believe u used a zero in two when declaring sum.
Code:
```int sum = ONE + TW0;
//should be
int sum = ONE + TWO;```

12. And if you're still not sure, I'm sure four more people will point it out also in the next three minutes...

Quzah.

13. Thnx ha ha ha Ok!!! I got it ha ha ha ha

14. Originally Posted by prog-bman
Why not just use const instead, as i have heard defines are evil .
Code:
```#include <iostream>
using std::cout;
const int ONE = 1;
const int TWO = 2;

int main()
{
int sum = ONE + TW0;
cout<<sum;
return 0;
}```
while you're staying away from deines, you should also stay away from globally-declared variables...

by the way:
Code:
```const int TWO = 2;

int main()
{
int sum = ONE + TW0;```
</sarcasm>

15. Originally Posted by major_small
by the way:
Code:
```const int TWO = 2;

int main()
{
int sum = ONE + TW0;```
</sarcasm>
So you're trying to say that one of them is the LETTER O, and the other is the number ZERO?

Quzah.