Exam Q: Using Enumerations

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Exam Q: Using Enumerations

 
0
  #1
Jun 9th, 2009
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):
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Exam Q: Using Enumerations

 
0
  #2
Jun 9th, 2009
Your not catching the value return by the function.
  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. }
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Exam Q: Using Enumerations

 
0
  #3
Jun 9th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

Re: Exam Q: Using Enumerations

 
0
  #4
Jun 9th, 2009
below code will work fine, may be there is a little error in syntax as i didnot follow ur code...
  1. Months nextMonth(Months month){
  2. return Months((month+1)%12) ;
  3. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Exam Q: Using Enumerations

 
0
  #5
Jun 9th, 2009
Originally Posted by yun View Post
below code will work fine, may be there is a little error in syntax as i didnot follow ur code...
  1. Months nextMonth(Months month){
  2. return Months((month+1)%12) ;
  3. }
You are on the right way, but...
Test it for November
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

Re: Exam Q: Using Enumerations

 
0
  #6
Jun 9th, 2009
Originally Posted by ArkM View Post
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Exam Q: Using Enumerations

 
1
  #7
Jun 9th, 2009
>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...
Well, the right answer is:
  1. currentMonth % 12 + 1
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

Re: Exam Q: Using Enumerations

 
0
  #8
Jun 9th, 2009
Originally Posted by ArkM View Post
>i checked for Nov and for Dec
Hmm... For December the code returns a wrong answer months(0)...
Well, the right answer is:
  1. currentMonth % 12 + 1

Ha Ha Ha................. Thanks Once Again brother for mention my mistake.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC