I delcared an enum type in my program:

typedef enum {freshman, sophmore, junior, senior
}level;

then i declared a variable

level x=freshman;

how can i increase the enum type to go from freshman to sophemore etc.

Recommended Answers

All 3 Replies

>>how can i increase the enum type to go from freshman to sophemore etc.
with the assignment operator: x = sophmore;

if the enums have consecutive integral values (as in this case), we can increment by adding one to the value.

#include <iostream>
int main()
{
  enum level { FRESHMAN=0, SOPHMORE=1, JUNIOR=2, SENIOR=3 } ;
  const char* const literal_level[] = 
                            { "FRESHMAN", "SOPHMORE", "JUNIOR", "SENIOR" } ;
  for( level lev = FRESHMAN ; lev <= SENIOR ; lev = level(lev+1) )
      std::cout << literal_level[ lev ] << '\n' ;
}

>>lev = level(lev+1)
Ah yes -- but lev++ will not compile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.