CHAPTER 5
Decision and loop statements
63
1.
Continue working with the project from the previous exercise.
2.
Replace the GetDay function with the following code so that it uses an if-else-if statement to
determine the maximum allowable number of days.
int GetDay(int year, int month)
{
int maxDay;
if (month == 4 || month == 6 || month == 9 || month == 11)
{
maxDay = 30;
}
else if (month == 2)
{
maxDay = 28;
}
else
{
maxDay = 31;
}
Console::Write("Day [1 to ");
Console::Write(maxDay);
Console::Write("]? ");
String ^input = Console::ReadLine();
int day = Convert::ToInt32(input);
return day;
}
3.
Build and run the application. Type the year 2012 and the month 1.
The application prompts you to enter a day between 1 and 31, as illustrated in the following
screen shot:
4.
Type a valid day and close the console window when the date is displayed.
Do'stlaringiz bilan baham: |