943,748 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 850
  • C++ RSS
Jun 9th, 2009
0

Exam Q: Using Enumerations

Expand Post »
Hi

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

Quote ...
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):
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. enum months {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
  7.  
  8. months nextMonth(months currentMonth);
  9.  
  10. int main()
  11. {
  12. months a = nov;
  13.  
  14. nextMonth(a);
  15.  
  16. cout << a << endl;
  17.  
  18.  
  19. return 0;
  20. }
  21.  
  22. months nextMonth(months currentMonth)
  23. {
  24. currentMonth = currentMonth + 1;
  25. return currentMonth;
  26. }
Last edited by gretty; Jun 9th, 2009 at 3:55 am.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

Your not catching the value return by the function.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. enum months {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
  4. months nextMonth(months& currentMonth);
  5. int main()
  6. {
  7. months a = nov;
  8.  
  9. a=nextMonth(a);
  10.  
  11. cout << a << endl;
  12.  
  13.  
  14. return 0;
  15. }
  16.  
  17. months nextMonth(months &currentMonth)
  18. {
  19. return months(currentMonth+jan);
  20. }
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

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:
C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

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)
  1. Months nextMonth(Months month){
  2. return Months((month+1)%12) ;
  3. }
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

Click to Expand / Collapse  Quote originally posted by yun ...
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)
  1. Months nextMonth(Months month){
  2. return Months((month+1)%12) ;
  3. }
You are on the right way, but...
Test it for November
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

Click to Expand / Collapse  Quote originally posted by ArkM ...
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
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)
  1. #include <iostream>
  2. using namespace std;
  3. enum months {Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
  4.  
  5. months nextMonth(months currentMonth);
  6.  
  7. int main(){
  8. months a = Dec;
  9. a=nextMonth(a);
  10. cout << a << endl;
  11.  
  12. a = Nov;
  13. a=nextMonth(a);
  14. cout << a << endl;
  15.  
  16. return 0;
  17. }
  18.  
  19. months nextMonth(months currentMonth){
  20. return months((currentMonth+1)%13);
  21. }
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009
Jun 9th, 2009
1

Re: Exam Q: Using Enumerations

>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)
  1. currentMonth % 12 + 1
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 9th, 2009
0

Re: Exam Q: Using Enumerations

Click to Expand / Collapse  Quote originally posted by ArkM ...
>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)
  1. currentMonth % 12 + 1

Ha Ha Ha................. Thanks Once Again brother for mention my mistake.
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to include path to binaries
Next Thread in C++ Forum Timeline: Insert Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC