0

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.

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

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' ;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.