Your not catching the value return by the function.
#include <iostream>
using namespace std;
enum months {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
months nextMonth(months& currentMonth);
int main()
{
months a = nov;
a=nextMonth(a);
cout << a << endl;
return 0;
}
months nextMonth(months ¤tMonth)
{
return months(currentMonth+jan);
}
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
It's possible to convert enumeration value to int but no an implicit reverse conversion. That's why currentMonth = currentMonth + 1 expression is wrong. More precisely, the right side is legal subexpression (treated as (int)currentMonth + 1 and has int type) but noimplicit int to months conversion and the assignment is not legal here.
However it's possible to convert int value to enumerator explicitly, for example:
months m = static_cast<months>(1); // = jan
It seems that return static_cast<month>(currentMonth+1) solves the problem. No, it's wrong. If currenMonth == dec, we have 13 - no months type value of static_cast(13).
Fortunately, it's so easy to correct an expression in return statement...
Do it yourself ;)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
below code will work fine, may be there is a little error in syntax as i didnot follow ur code...
Months nextMonth(Months month){
return Months((month+1)%12) ;
}
You are on the right way, but...
Test it for November ;)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...
Well, the right answer is:
currentMonth % 12 + 1
;)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348