Program 8-7

posted Apr 10, 2013, 12:52 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:52 AM ]
Program 8-7
1 // This program allows the user to select a month and then
2 // displays how many days are in that month. It does this
3 // by "looking up" information it has stored in arrays.
4 #include <iostream>
5 #include <iomanip>
6 #include <string>
7 using namespace std;
8
9 int main()
10 {
11 const int NUM_MONTHS = 12;
12 int choice;
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 cout << "This program will tell you how many days are "
23 << "in any month.\n\n";
24
25 // Display the months
26 for (int month = 1; month <= NUM_MONTHS; month++)
27 cout << setw(2) << month << " " << name[month-1] << endl;
28
29 cout << "\nEnter the number of the month you want: ";
30 cin >> choice;
31
32 // Use the choice the user entered to get the name of
33 // the month and its number of days from the arrays.
34 cout << "The month of " << name[choice-1] << " has "
35 << days[choice-1] << " days.\n";
36 return 0;
37 }
Program Output with Example Input Shown in Bold
This program will tell you how many days are in any month.
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
Enter the number of the month you want: 4[Enter]
The month of April has 30 days.
Comments