Hi

I am studying for my C++ exam coming up & I have this practice question thats confusing me?

Given

enum months{Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

write a C++ function that takes currentMonth of type months as the parameter, and returns the next month. For example, nextMonth(Nov) should return Nov.

months nextMonth(months currentMonth)
{
//missing code
}

my attempt is (which doesn't work & I dont know why):

``````#include <iostream>
#include <cmath>

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;

nextMonth(a);

cout << a << endl;

return 0;
}

months nextMonth(months currentMonth)
{
currentMonth = currentMonth + 1;
return currentMonth;
}``````

Answered by siddhant3s 1,429 in a post from

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 …``````
Answered by ArkM 1,090 in a post from

>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...

``currentMonth % 12 + 1``

;)

All 7 Replies

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 &currentMonth)
{
return months(currentMonth+jan);
}``````

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 no implicit 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<months>(13).

Fortunately, it's so easy to correct an expression in return statement...
Do it yourself ;)

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) ;
}``````

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 ;)

You are on the right way, but...
Test it for November ;)

Bhai as i mention in my post that i didnt check the code, well Jan = 1 was the issue :D
thanks for resolving the logical error.

below is the update version of the program... i checked for Nov and for Dec. Every Entry work fine.
Regards.

``````#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 = Dec;
a=nextMonth(a);
cout << a << endl;

a = Nov;
a=nextMonth(a);
cout << a << endl;

return 0;
}

months nextMonth(months currentMonth){
return months((currentMonth+1)%13);
}``````

>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...

``currentMonth % 12 + 1``

;)

commented: U r Best Man.. +1

>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...
``currentMonth % 12 + 1``