Print Month Program

Hello guys.

I am new to C++ and I am trying to make a program that will print out some random months by using a function. I have the main written, but I need to figure out the function so the main knows what it is trying to execute.

I want to use these parameters for the function:
const int month : Range (1..12) The numeric value for the month to be printed out. For example if the number 1 is passed in than January will be printed out, if 2 is passed in than February will be printed out. Etc.

const int startday: Range (0..6) This value will tell the routine which day of the week to start the 1st of the month on. The calendar will be laid out so that Sunday is the first day and Saturday the last. If the number 0 is passed in, the 1st will be on a Sunday, if 2 than the 1st of the month will be Monday, etc.

const bool leapyear: Range (true or false) This value will tell the routine to print out the Month of February with 29 days instead of 28.


Here would be an example of the output:

1
2
3
4
5
6
7
          February
Sun Mon Tue Wed Thu Fri Sat
      1   2   3   4   5   6 
  7   8   9  10  11  12  13 
 14  15  16  17  18  19  20 
 21  22  23  24  25  26  27 
 28  29 


I basically just need to get the ball rolling. I have trouble with arrays.

Any ideas?

Oh, here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string> 
#include <iomanip>
using namespace std;
void printmonth(const int month, const int startday, const bool leap);


int main(void)
{
  printmonth(1,0,false);   // Print January 1st, on a Sunday
  printmonth(2,1,true);    // Print February 1st leap year, on Monday
  printmonth(1,2,false);   // Print January 1st, on a Tuesday
  printmonth(2,3,false);   // Print February 1st not leap, on Wednesday
  printmonth(1,4,false);   // Print January 1st, on a Thursday
  printmonth(2,5,false);   // Print February 1st, on a Friday
  printmonth(1,6,false);   // Print January 1st, on a Saturday
  printmonth(6,1,false);   // Print June 1st, on Monday
  printmonth(12,4,false);  // Print December 12th, on a Thursday
  
return 0;
}

void printmonth()
{

}
Last edited on
> I basically just need to get the ball rolling. I have trouble with arrays.

Think of an array of size N as a collection of N variables of the same type. For example int array_one[20] ; is a collection of 20 variables (elements) of type int.

Each element is associated with a number 0, 1, 2, 3, ...., N-1. [ 0, N-1 ). We can access each element by specifying the associated number. With int array_one[20] ; the first int is array_one[0], the second int is array_one[1], .... , and the last one is array_one[19].

An array can be initialized the specifying the value with which each element is to be initialized within a pair of braces {}. For example,
double array_two[4] = { 1.2, 3.4, 5.6, 7.8 } ; array_two is an array containing 4 double elements, array_two[0] is initialized with 1.2, array_two[1] is initialized with 3.4 and so on.

This would add 10 to every element in array_two:
for( int i=0 ; i<4 ; ++i ) array_two[i] += 10 ;

And this would print out every element in array_one:
for( int i=0 ; i<20 ; ++i ) std::cout << array_one[i] << '\n' ;

Putting this knowledge to work, a function that would take an integer in [1,12] and
print out the name of the month (English, Gregorian calendar) could be written as

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

void print_month( int month /* invariant 1 <= month <= 12 */ )
{
    const std::string month_name[13] = { "nothing", // there is no month 0
       "January", "February", /* march etc... */ "December" } ;
    if( month>0 && month<13 ) std::cout << month_name[month] << '\n' ;
    else std::cerr << "month number " << month << " is invalid\n" ;
}


Take it up from there.

Last edited on
I am having trouble setting up the second array. How do I declare startday properly? I tried doing it, but I couldn't compile. So I sort of stripped it out to isolate the problem, so that way I can go in each step to make sure I'm doing it right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string> 
#include <iomanip>
using namespace std;
void printmonth(const int month);


int main(void)
{
  printmonth(1);   // Print January 1st, on a Sunday
  printmonth(2);    // Print February 1st leap year, on Monday
  printmonth(1);   // Print January 1st, on a Tuesday
  printmonth(2);   // Print February 1st not leap, on Wednesday
  printmonth(1);   // Print January 1st, on a Thursday
  printmonth(2);   // Print February 1st, on a Friday
  printmonth(1);   // Print January 1st, on a Saturday
  printmonth(6);   // Print June 1st, on Monday
  printmonth(12);  // Print December 12th, on a Thursday
  
return 0;
}

void printmonth(int month)
{
    const std::string month_name[13] = { "Nothing", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ;
    if( month>0 && month<13 ) std::cout << month_name[month] << '\n' ;
    else std::cerr << "month number " << month << " is invalid\n" ;

}
Last edited on
> I am having trouble setting up the second array.

Isn't it very much like setting up the first?

Once you have got void printmonth(int month) working correctly, write a second function
void printday(int day). Once you have got that working correctly, write a function that can print both
void print_month_and_day( int month, int day).

Take baby steps, one at a time; do not take the next step until you have become absolutely stable after the previous step.
Topic archived. No new replies allowed.