TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Glenn D`Abrera
from: David Nugent
date: 1995-07-19 04:52:16
subject: How do i do this?

> if an enumerated type has been declared(say over a range of 1 to 7)
 > and i wanted to input data for that type in a 2-d matrix.

 >  for example, over a range of Days of the Week.
 >   if i declared
 >       for (days=Sunday;days<=Saturday;days++)
~
 >   i believe standard C lets u do this BUT C++ does not.

You came from Pascal - right? :-)

Enumations in C++ are not based on Wirth's "sets" concept, and
therefore incrementing or decrementing them has no meaning. They are
actually handled as disrete values. In fact even in C there's little
difference between an enumation and a plain old #define - there's no
internal control over sets which aren't integral and not exactly 1 apart.
For example, if you had an enumerator such as:

enum xyz
{
    smallval    = 1,
    largerval   = 2,
    largestval  = 4
};

Then 'incrementing' a variable of type enum xyz will still increment by 1,
even though there's no meaningful definition corresponding with the value
3.


 >   how can one do this in C++??

In C++, the rules are merely more strict, and the language does not let you
handle enumerations arithmetically (and an increment, is an arithmetic
operation). This is intended to ensure that you cannot have an initialised
variable of an enumulated type set to any value except one which is valid.

You can, however, "get around" this by using type int, ie.

    for ( int days=Sunday ; days <= Saturday ; days++ )

Note that 'days' is type int (might be any integral type, provided it is
large enough to hold all possible values of the enumator), and not the
enumeration type.

  Hope this helps,
  David

---
* Origin: Unique Computing, Melbourne, Australia (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 348 998 633/371 634/384 388 635/301
SEEN-BY: 635/502 503 544 727 636/100 639/100 711/401 409 410 430 510 807 808
SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 632/103 348 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.