# Thread: Is there any reason?

1. ## Is there any reason?

You see in the beginning just after the #include... where it gives all those variables the const qualifier. I don't understand why there there? i removed them and nothing happened? Maybe someone could explain there meaning to me. thanks

Code:
```// cards.cpp
// demonstrates structures using playing cards
#include <iostream.h>

const int clubs = 0;
const int diamonds = 1;
const int hearts = 2;

const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;

struct card
{
int number;   // 2 to 10, jack, queen, king, ace
int suit;     // clubs, diamonds, hearts, spades
};

void main()
{
card temp, chosen, prize;                  // define cards
int position;

card card1 = { 7, clubs };                 // initialize card1
cout << "Card 1 is the 7 of clubs\n";

card card2 = { jack, hearts };             // initialize card2
cout << "Card 2 is the jack of hearts\n";

card card3 = { ace, spades };              // initialize card3
cout << "Card 3 is the ace of spades\n";

prize = card3;               // copy constthis card, to remember it

cout << "I'm swapping card 1 and card 3\n";
temp = card3; card3 = card1; card1 = temp;

cout << "I'm swapping card 2 and card 3\n";
temp = card3; card3 = card2; card2 = temp;

cout << "I'm swapping card 1 and card 2\n";
temp = card2; card2 = card1; card1 = temp;

cout << "Now, where (1, 2, or 3) is the ace of spades? ";
cin >> position;

switch (position)
{
case 1: chosen = card1; break;
case 2: chosen = card2; break;
case 3: chosen = card3; break;
}
if(chosen.number == prize.number &&          // compare cards
chosen.suit == prize.suit)
cout << "That's right!  You win!\n";
else
cout << "Sorry. You lose.\n";
}```

2. something did in fact happen. although not noticable to you, you made the variables such that they could now be modified. They were no longer "constant"

3. yes, since you removed the const keyword, they can now be modified by the program. usually, it is undesired changes that are hard to find if something goes wrong with the program. const will give you compiler errors that constants cannot be changed, and therefore, will prevent the programmer from doing something unintended.
a good rule of thumb is if the the variable does not need to be changed and shall be assigned one value throughout the program, it is best to be const.

4. Cool! thanks alpha! you made me so much happier

Thanks for the rule of thumb to

5. hmm...ok. the variables that are never used in this program are just there for completness. But when i first came across this program i was surprised to see the

const int clubs = 0;
const int diamonds = 1;
const int hearts = 2;

const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
declared outside of main()
so i put it inside of main(). but now the compiler signals errors that king, queen, and diamonds is never used in function main(). Why would it not complain when it was outside of main()?

6. I suppose there's a couple explanations. one is that a compiler can tell if a "local" variable is not used within its scope. A global however has a larger scope than the compiler can see. other modules can be linked such that they can use the first module's globals (see extern). Therefore a compiler can't really make the determination on a global.

7. Hey fill you brain.. man! i have no idea what your talking about. Thanks for taking the time to try to explain it to me but i need it in n00b english. All iwant to know is why the compiler doesn't signal errors when variables that are never used are declared outside of

void main()
{
}

8. >>void main()
>>{
>>}

9. inside main() == local
outside main() or some other function == global

scope == where a variable can be seen and used
local == one that has scope ONLY in that function
global == one that has scope in all functions

point is... compilers work on one cpp file. a global can be seen across multiple cpp files. so a compiler can't tell if the variable isn't used because it's not looking at ALL of the cpp files

10. If you have a number which won't change throughout the program, why not just use a #define?
#define diamonds 1
What is the advantage of const int over a define?

11. tigs, you'll get a few people around here that'll jump down your throat for that one.

some people feel that const is a more clean/modern/whatever way of doing it. realistically, I still use #define quite often.

12. the only time I have used #define is with classes. with c++, const is a better method, imo. i believe #define originates with the c days, i'm not sure, i've never really programmed c.

13. of course there are other reasons for using defines. pre-process activity for instance.
Code:
```#ifdef _DEBUGLOG_
#define OutputDebugLog(s) DoSomeDebugLogging(s);
#else
#define OutputDebugLog(s)
#endif```
lets see you do that with const!

anyway, I have no problem with defines used for constant integers.