Program 8-6

Post date: Apr 10, 2013 7:52:20 AM

Program 8-6

1 // This program displays the number of days in each month. It uses an

2 // array of string objects to hold the month names and an int array

3 // to hold the number of days in each month. Both are initialized with

4 // initialization lists at the time they are created.

5 #include <iostream>

6 #include <iomanip>

7 #include <string>

8 using namespace std;

9

10 int main()

11 {

12 Const int NUM_MONTHS = 12;

13 string name[NUM_MONTHS] = {"January", "February", "March",

14 "April", "May", "June",

15 "July", "August", "September",

16 "October", "November", "December" };

17

18 int days[NUM_MONTHS] = {31, 28, 31, 30,

19 31, 30, 31, 31,

20 30, 31, 30, 31};

21

22 for (int month = 0; month < NUM_MONTHS; month++)

23 {

24 cout << setw(9) << left << name[month] << " has ";

25 cout << days[month] << " days.\n";

26 }

27 return 0;

28 }

Program Output

January has 31 days.

February has 28 days.

March has 31 days.

April has 30 days.

May has 31 days.

June has 30 days.

July has 31 days.

August has 31 days.

September has 30 days.

October has 31 days.

November has 30 days.

December has 31 days.