| | |
Exam Q: Using Enumerations
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
Hi
I am studying for my C++ exam coming up & I have this practice question thats confusing me?
my attempt is (which doesn't work & I dont know why):
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
}
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by gretty; Jun 9th, 2009 at 3:55 am.
Your not catching the value return by the function.
C++ Syntax (Toggle Plain Text)
#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); }
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
It's possible to convert enumeration value to int but no an implicit reverse conversion. That's why
However it's possible to convert int value to enumerator explicitly, for example:
It seems that
Fortunately, it's so easy to correct an expression in return statement...
Do it yourself
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:
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: May 2009
Posts: 37
Reputation:
Solved Threads: 2
below code will work fine, may be there is a little error in syntax as i didnot follow ur code...
C++ Syntax (Toggle Plain Text)
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...
C++ Syntax (Toggle Plain Text)
Months nextMonth(Months month){ return Months((month+1)%12) ; }
Test it for November
•
•
Join Date: May 2009
Posts: 37
Reputation:
Solved Threads: 2
Bhai as i mention in my post that i didnt check the code, well Jan = 1 was the issue 
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.

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.
C++ Syntax (Toggle Plain Text)
#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:
Hmm... For December the code returns a wrong answer months(0)...
Well, the right answer is:
C++ Syntax (Toggle Plain Text)
currentMonth % 12 + 1
•
•
Join Date: May 2009
Posts: 37
Reputation:
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:
C++ Syntax (Toggle Plain Text)
currentMonth % 12 + 1
Ha Ha Ha................. Thanks Once Again brother for mention my mistake.
![]() |
Similar Threads
- SUN java Exam.....Need help (Java)
- Java Sun Programmer 1.4 Certification Exam HELP!!! (Java)
- Mock exam help? (Java)
Other Threads in the C++ Forum
- Previous Thread: how to include path to binaries
- Next Thread: Insert Tree
| Thread Tools | Search this Thread |
add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






