Hello ladies and gents,
I just made the next exercise of this book and although it works as it should, I was wondering if any of you guys could tell me whether or not it could be improved.
The exercise goes as follows:
- Define a table of the names of months of the year and the number of days in each month. Write out that table. Do this twice: once using an array of char for the names and an array for the number fo days and once using an array of structures, with each structure holding the name of a month and the number of days in it.
I also added the following, initialized the struct with the values of the two arrays days and months and see the sizeof both arrays and the size of the struct and struct array.
Here's the code:
#include <iostream>
#include <string>
struct Date
{
std::string strMonth;
int strDays;
};
int main()
{
Date dt[12];
std::cout << "Using two arrays:\n";
char month[][12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (size_t i = 0; i < 12; i++)
std::cout << days[i] << ' ' << month[i] << '\n';
std::cout << std::endl;
std::cout << "Using an array of struct:\n";
for (size_t i = 0; i < 12; i++)
{
dt[i].strMonth = month[i];
dt[i].strDays = …