Not sure what this is....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Not sure what this is....

  1. #1
    J Guest

    Not sure what this is....


    I'm writing a program for my class to create an amortization table. OK so
    far, except that I'm
    supposed to create an "enumerated data type" for the month number.

    What is an "enumerated data type"?

    Any help is GREATLY appreciated!!

  2. #2
    Jim Guest

    Re: Not sure what this is....


    "J" <jaholker@hotmail.com> wrote:
    >
    >I'm writing a program for my class to create an amortization table. OK

    so
    >far, except that I'm
    >supposed to create an "enumerated data type" for the month number.
    >
    >What is an "enumerated data type"?
    >
    >Any help is GREATLY appreciated!!


    I had to do the same kind of program for the class I just finished too.
    I don't have my notes or text book with me so I'm going off of memory. It
    sounds to me that they want you to declare the data type and specify the
    values that are possible for it. For instance,
    int month = (1,2,3,4,5,6,7,8,9,10,11,12);
    This is done so that the data type can only be set to these values, otherwise
    an "out of range" error will occur.
    Good luck!

  3. #3
    Danny Kalev Guest

    Re: Not sure what this is....



    Jim wrote:
    >
    > "J" <jaholker@hotmail.com> wrote:
    > >
    > >I'm writing a program for my class to create an amortization table. OK

    > so
    > >far, except that I'm
    > >supposed to create an "enumerated data type" for the month number.
    > >
    > >What is an "enumerated data type"?
    > >
    > >Any help is GREATLY appreciated!!

    >
    > I had to do the same kind of program for the class I just finished too.
    > I don't have my notes or text book with me so I'm going off of memory. It
    > sounds to me that they want you to declare the data type and specify the
    > values that are possible for it. For instance,
    > int month = (1,2,3,4,5,6,7,8,9,10,11,12);


    Well, this doesn't solve the range problem because you can easily do
    something like month[159] = 1 and encounter a runtime crash.

    Enumerated types are user-defined types that define a closed set of
    integral values for named constants. Months are excellent candidates for
    an enumerated type because they are a closed set (i.e., you have only 12
    months a year) and they are usually referred to by their names rather
    than by number.
    Here's an enum type of months:

    enum months
    {
    Jan = 1,
    Feb,
    Mar,
    Apr,
    May,
    June,
    July,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec
    };

    int main()
    {
    months m;
    m = Jan;
    int n = m; // OK, n equals 1
    m = 5; // error, can't use plain int
    m = 80; // error
    }

    Danny Kalev

    "The ANSI/ISO C++ Professional Programmer's Handbook"
    http://www.amazon.com/exec/obidos/ASIN/0789720221

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center