954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Exam Q: Using Enumerations

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;
}
gretty
Junior Poster
158 posts since Apr 2009
Reputation Points: 10
Solved Threads: 7
 

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);
}
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) ; 
}
yun
Light Poster
42 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

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
 
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);
}
yun
Light Poster
42 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

>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
 

>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.

yun
Light Poster
42 posts since May 2009
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You