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

Dani AI

Generated

Short summary of what went wrong and how to fix it.

As observed, calling nextMonth(a); by itself does nothing if you expect a to change — the function returns a value, and (unless you pass by reference) you must assign that return back to a. Also, plain arithmetic on an enum yields an integer expression and there is no implicit conversion back to the enum type; an explicit cast is required for the conversion. (en.cppreference.com)

Be careful with wrap-around and out-of-range values. Naive casting after adding 1 can produce a numeric value that is not one of the defined enumerators (for example Dec -> 13). Converting an out-of-range integer to an enum can be unspecified/undefined depending on language version, so check or constrain the integer before you cast it back to the enum type. (cppreference.com)

A robust, clear approach is: (1) convert the enum to its underlying integer type, (2) validate/wrap the integer in the 1..12 range, and (3) convert back to the enum with an explicit cast. Using std::underlying_type_t makes the conversion portable across compilers:

#include <type_traits>
#include <stdexcept>

months nextMonth(months currentMonth) {
    using UT = std::underlying_type_t<months>;
    UT v = static_cast<UT>(currentMonth);
    if (v < 1 || v > 12) throw std::out_of_range("nextMonth: invalid month");
    UT next = (v == 12) ? 1 : v + 1;
    return static_cast<months>(next);
}

This avoids UB and the off-by-one traps that tripped and follows the safety advice from . For modern code, consider enum class for stronger typing and std::to_underlying (C++23) or std::underlying_type_t to convert cleanly between enum and integer. Always test every enumerator (Jan..Dec) and decide how to handle invalid enum values (exceptions, assertions, or defined fallback). (cppreference.com)

Recommended Answers

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)...
Well, the right answer is:

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)...
Well, the right answer is:

currentMonth % 12 + 1

;)

Ha Ha Ha................. Thanks Once Again brother for mention my mistake.

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.