C++ Constants

Page 1 of 1

5 Replies - 1066 Views - Last Post: 19 July 2009 - 03:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=115699&amp;s=f82cf6b289191074bf4f6a14da8e4b40&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Kandayo

Reputation: 1
• Posts: 48
• Joined: 17-July 09

C++ Constants

Posted 19 July 2009 - 02:22 PM

I think I need help when declaring constants.

For example:

-----------------------------------------------------------------------------

Write a program that asks the user to enter a latitude in degrees, minutes, and seconds and that then displays the latitude in decimal format. There are 60 seconds of arc to a minute and 60 minutes of arc to a degree; represent these values with symbolic constants. You should use a separate variable for each input value. A sample run should look like this:

Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

------------------------------------------------------------------------------

What would you use for the constant names and values and why? I had a feeling that I don't know how to come up with constants effectively.

Here is another example:

------------------------------------------------------------------------------

---------------------------------------------------------------------------------

I used:

```const int FEET = 12;			  // CONVERSION FACTORS
const double KILO = .4536;		// CONVERSION FACTORS
const double METER = .0254;	   // CONVERSION FACTORS

```

I want to know if you guys would try something different?

Heeeeelp!!

Is This A Good Question/Topic? 0

Replies To: C++ Constants

#2 deery5000

• D.I.C Lover

Reputation: 87
• Posts: 1,095
• Joined: 09-May 09

Re: C++ Constants

Posted 19 July 2009 - 03:16 PM

the way you are doing it is fine. I have also seen the keyword define used like so

```
#define MAX 10;

int main()
{
char name[MAX];
return 0;
}

```

hope this helps
Kevin

This post has been edited by deery5000: 19 July 2009 - 03:16 PM

#3 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

Re: C++ Constants

Posted 19 July 2009 - 03:24 PM

#define is a preprocessor instruction which works in a manner similar to copy-and-paste; i.e. #define statements are parsed and replaced before the compiler runs - the net result is that there is no type safety whatsoever with #define'd constants, so its almost always better to use const variables, which are type checked just like other ordinary objects.

#4 Kandayo

Reputation: 1
• Posts: 48
• Joined: 17-July 09

Re: C++ Constants

Posted 19 July 2009 - 03:26 PM

deery5000, on 19 Jul, 2009 - 02:16 PM, said:

the way you are doing it is fine. I have also seen the keyword define used like so

```
#define MAX 10;

int main()
{
char name[MAX];
return 0;
}

```

hope this helps
Kevin

yeah, but "#define" is more of a C method. Using "const" is like the same but more improved. #define also replaces the variable before the program is compiled (notice the preprocessor directive). You can state the variable-type.

This post has been edited by Kandayo: 19 July 2009 - 03:28 PM

#5 deery5000

• D.I.C Lover

Reputation: 87
• Posts: 1,095
• Joined: 09-May 09

Re: C++ Constants

Posted 19 July 2009 - 03:50 PM

cool didnt know that, i always use const just cause i never bother about define lol

Learned something new there

Kevin

#6 sscheider

Reputation: 9
• Posts: 55
• Joined: 14-May 09

Re: C++ Constants

Posted 19 July 2009 - 03:57 PM

Kandayo, on 19 Jul, 2009 - 01:22 PM, said:

I think I need help when declaring constants.
...
Use symbolic constants to represent the various conversion factors.

```const int FEET = 12;			  // CONVERSION FACTORS
const double KILO = .4536;		// CONVERSION FACTORS
const double METER = .0254;	   // CONVERSION FACTORS

```

I want to know if you guys would try something different?

Heeeeelp!!

The syntax is fine, and this will compile and work. My suggestion is with the name of the constant. When you are converting, the equation in C++ would be something like:
```conv1 = htInches / FEET;
```

which makes someone reviewing the code need to search for the constant, FEET. If the name was INCH_PER_FOOT, then the same code becomes:
```conv1 = htInches / INCH_PER_FOOT;
```

which is a bit more descriptive. This equation also shows another potential issue. If htInches is defined as an int, then that equation will be done as integer division. Defining the constant as a double forces the evaluation as a double:
```const double INCH_PER_FOOT = 12;
conv1 = htInches / INCH_PER_FOOT;
```

The type of conv1 in this example will force the cast of the evaluation to whatever numeric type conv1 actually is. I didn't state it in these examples, so it could be anything.
A picky point is to always begin decimals between 1 and -1 with a zero. Though .4536 is fine, it's hard to see the decimal point when someone is scanning code. Typically, when troubleshooting or altering code, I don't really read it - I scan it.
Another picky point is lining up the equals signs. This really bothers some folks when they scan code. One last suggestion is that the comment "CONVERSION FACTOR" really doesn't help. When it's applied to multiple lines that are apparently different, it's not useful at all. Instead, comment the beginning of the block of conversion factors.
Summarizing, I'd suggest changing your constants to:
```	// conversion factors
const double INCH_PER_FOOT	= 12.0;
const double LB_PER_KILO	  = 0.4536;
const double METER_PER_INCH   = 0.0254;

```

As a note, using compiler directives, like #define, for constants is kind of frowned upon with ANSI C++. Many folks still do it, so it's something to watch for.

I hope this helps!
/sscheider