5 Replies - 782 Views - Last Post: 19 July 2009 - 03:57 PM Rate Topic: -----

#1 Kandayo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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:

------------------------------------------------------------------------------
Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables to store the information.) Have the program report your body mass index (BMI). To calculate the BMI, first convert your height in feet and inches to your height in inches (1 foot = 12 inches). Then, convert your height in inches to your height in meters by multiplying by 0.0254. Then, convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing your mass in kilograms by the square of your height in meters. Use symbolic constants to represent the various conversion factors.

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

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  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Kandayo  Icon User is offline

  • New D.I.C Head

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

Re: C++ Constants

Posted 19 July 2009 - 03:26 PM

View Postdeery5000, 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

Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • 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 :P

Kevin
Was This Post Helpful? 0
  • +
  • -

#6 sscheider  Icon User is offline

  • D.I.C Head

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

Re: C++ Constants

Posted 19 July 2009 - 03:57 PM

View PostKandayo, 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1