Question:
C++ enum type simple question?
MrJames
2011-09-02 19:35:55 UTC
Is there a way to advance book to the next position without adding 1 in the cout I have ?


#include
using namespace std;

int main(){

enum bookType {MATH, CSC, ENGLISH, HISTORY, PHYSICS, PHILOSOPHY}; // part a

bookType book; // part b

book = MATH; // part c

// how to just advance book to the next position without adding 1 in cout???

cout << "Value of book is: " << book + 1 << endl; // d and e




I really appreciate the help guys. Thank you.


system("pause");
return 0;

}
Three answers:
samofcalifornia
2011-09-02 20:13:18 UTC
Incrementing the instance of an enum will work but with two or more exceptions. The big exception is that enums are not always sequential; it is totally possible to specify a corresponding value for each enumerator and therefore have values that are relatively random. Another exception is that you will need to know what the maximum enumerator (PHILOSOPHY in your example) is.
2011-09-02 19:37:55 UTC
Uh... what? Enums are just integers that mean something else. You can use the increment operator (++) but to get to the next enum you HAVE to add 1 to it.
pridgin
2016-11-30 01:13:27 UTC
it somewhat is ToLower no longer tolower edit: you may attempt what Sapan reported or in case you quite desire to apply enums Months m = (Months)Enum.Parse(typeof(Months), month, genuine); // convert string to enum // edit: ToLower does not count because of the fact third param is genuine so it ignores case swap (m) { case Months.January: case Months.March: case Months.might: case Months.July: case Months.August: case Months.October: case Months.December: Console.WriteLine("The form of days in {0} is 31.", month); smash; case Months.February: Console.WriteLine("The quantity is days in {0} is {a million}.", month, DateTime.DaysInMonth(DateTime.Now.365 days, 2)); smash; case Months.April: case Months.June: case Months.September: case Months.November: Console.WriteLine("The quantity is days in {0} is 30.", month); smash; default: Console.WriteLine("it somewhat is no longer a valid month."); smash; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...