Thread: Is there any reason?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    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 spades = 3;
    
    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";
    	}
    #include <Jesus.h>
    It will save your life

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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"
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    Cool! thanks alpha! you made me so much happier

    Thanks for the rule of thumb to
    #include <Jesus.h>
    It will save your life

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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 spades = 3;

    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()?
    #include <Jesus.h>
    It will save your life

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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()
    {
    }
    #include <Jesus.h>
    It will save your life

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>void main()
    >>{
    >>}

    BADBADBADBAD! USE INT MAIN!!!
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    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?
    - Tigs

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault for who knows what reason
    By phlook in forum C Programming
    Replies: 4
    Last Post: 03-14-2009, 11:31 PM
  2. Replies: 16
    Last Post: 09-23-2008, 03:32 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. My thread stops for no reason
    By lectrolux in forum C Programming
    Replies: 7
    Last Post: 05-21-2003, 07:56 AM
  5. Another reason to not like America
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 129
    Last Post: 06-13-2002, 12:11 PM