Help with some code.
We have an assignment for class, and my code compiles fine, but only runs correctly when I have a "+1" near the end of this line of code.
dayValid = (day >= 1 && day <= daysInMonth+1);
I will post the exact instructions, though with my comments and organization it should be apparent to anyone with experience what is the issue.
Any help or explanation will be greatly appreciated!
1. An assignment statement that sets monthValid to true if the month entered is between 1 and 12, inclusive.
2. An assignment statement that sets yearValid to true if the year is between 1000 and 1999, inclusive.
3. An assignment statement that sets leap Year to true if the year is a leap year. The rule for a leap year can be found in the lecture slide.
4. An if statement that determines the number of days in the month entered and stores that value in variable daysInMonth. If the month entered is not valid, daysInMonth should get 0. Note that to figure out the number of days in February you’ll need to check if it’s a leap year.
5. An assignment statement that sets dayValid to true if the day entered is legal for the given month and year.
6. If the month, day, and year entered are all valid, print “Date is valid” and indicate whether or not it is a leap year. If any of the items entered is not valid, just print “Date is not valid” without any comment on leap year.
The problem looks to me that its not actually caused by the daysInMonth+1 as being a requirement, rather that daysInMonth is incorrect. Of course the daysInMonth + 1 would be invalid for something like april 31, which will check out as fine here. So you should just be checking for <= daysInMonth as you have assumed and that's correct.
Here's the problem:
I'd recommend the use of braced evaluations as well as using elseif (since it can only be one of these possible options) or a switch.
Thank you so much for the detailed explanation. You made it really easy to understand what the problem was, and why.
I took your advice and used the switch method and got the program to work properly, and now I want to go back and try the other method too.
Again thank you!
|All times are GMT +1. The time now is 02:25 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.