Basic calendar output based on the day of the week entered

Hey everyone!
So as the title says, I need a c++ basic program that outputs a calendar based on the day of the week and days in a month that is inputted. I.e. 1= monday, 7=sunday and 31 days. And if I enter 3, the code would display a calendar like this:

1
2
3
4
5
6
7
M  T  W  Th  F  Sat  Sun

        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 30 31


Heres a link to a picture of how it should look - https://imgur.com/a/mQJfXvG

There is a requirement that it is displayed using a "for".
So the program would ask the user to input how many days in a month there are as well as what day does the month start on.

Any guide or help would be appreciated!

Thanks.


Last edited on
Here's the most simple and short approach (feel free to ask for more help):

First let's assign a number to each day of the week starting from Monday (or Sunday if you're that kind of person) to Sunday. We go from 0 to 6, so every day of the week has a unique number from 0 to 6, including both 0 and 6.

Now we ask the user how many days the month has, we carefully stow away this input into a variable.

Next, we ask him which day the month starts on. Using what we had assigned earlier, we use a for-loop to print spaces (i=0). The for-loop's condition is: until iterating variable is less than to the day entered.

So if I had typed in Monday, the for-loop would have no iterations and no spaces would be filled. Hence we get desired output of month starting from Monday.

If I typed something else for day, then the for-loop would print " " till for condition. So Tuesday would make it so that the for-loop prints one " ", and the " " occupies Monday's spot so we got what we wanted.

(You have to worry about tidying up the use of " "s)

Now we use a second for-loop which would loop until it's < or <= (depending on what value you gave the iterating variable) to the days of the month entered. Print iterating variable and " " (to tidy it a bit).

Remember: Make sure days cannot be greater than 31.


Now that's all done what about putting the days to the next line? See this is when our day's numeric value comes in handy.

So we need to print a "\n" after 5 in your example, but how do we know when to do it!

Inside the for-loop for printing days, we add an if condition:
1
2
3
if(inputted day's numeric value + i % 7 = 0) {
   cout << "\n";
} 


Brief explanation of above if condition: Try to relate this to your example, if Wednesday was the first day, we would have a numeric value of 2 for the day. 2 + 5 = 7, which is divisible by 7 to give a quotient of 0. Hence after the compiler prints 5, it comes to the if condition, checks it as true and prints a new line. Then days 6 onwards are printed on the next line. Similarly 2+12 = 14 which is divisible by 7 to give 0 quotient.

That's the logic behind this.

Now make sure that the if condition is the last thing in the for-loop, otherwise it would print "\n" too early!


Alright now you know what to do, go ahead and rock this! ;)
Last edited on
Hey thanks for this! Really helpful.
I did try following your explanation but being me I ran into some difficulties, so I tweaked it a bit and almost got it.

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
30
31
32
33
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main () {

  int i, j, days, dayofweek;

  cout <<"Enter days in the month: ";
  cin >> days;

  cout <<"Enter day of the week" <<endl;
  cout <<"(1 = Monday, 7 = Sunday): "; // I changed this to 1-7 instead of 0-6. Im not a 100% if this is an issue for the for loop
  cin >> dayofweek;

  for(i=0; i<dayofweek; i++)
    cout <<"    ";

  for (j=0; j<=days; j++){
    i++;
    cout << "   " << i;
    if (dayofweek + i % 7 == 0){
    cout << ("\n");
  }
  }

cout <<("\n");

  cin.sync();
  cin.get();
  return(0);
}



With this code it almost works :D
Obviously I have to change the if conditions because I changed the days of week from 0-6 to 1-7. But im not 100% sure how its supposed to look now..
Also the when I put in the day of week it removes that day from the "calendar" and starts from the date 2 instead of 1 for example.
And it doesnt go into a new line when the days have reached 7.


Again, thanks for the explanation, that really helped, but clearly Im still ways off hah any further help you can give?

Thank youu
Last edited on
cout << ("\n") doesn't seem to print a newline so stick with cout << "\n";

So since you want to take input from 1-7, you can do that, just decrease the variable that holds the day's numeric value by 1. Simple ;)

I see the confusion here. You seem to think that the iterating variable i's value is supposed to be carried to the second loop. But it's not like that. The first and the second for-loops are completely different and have nothing to do with each other. First one is for printing whitespaces and second one is for printing the days itself.

So I changed
(1) the cout statements for "\n"
(2) Made i and j loop specific variables by declaring them inside the loop
(3) Changed the for loops

And there you should have it! Now you need to work on tidying it up that's all

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
30
31
32
33
34
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main () {

  int days, dayofweek;

  cout << "Enter days in the month: ";
  cin >> days;

  cout <<"Enter day of the week" <<endl;
  cout <<"(1 = Monday, 7 = Sunday): ";
  cin >> dayofweek;
  
  dayofweek -= 1;

  for(int i=0; i<dayofweek; i++)
    cout <<"    ";

  for (int j=1; j<=days; j++){
    cout << "   " << j;
    if ((dayofweek + j) % 7 == 0){
    cout << "\n";
  }
  }

cout <<("\n");

  cin.sync();
  cin.get();
  return(0);
}


Let me know if you didn't understand a specific part.
Glad to know I was at least somewhat close haha
I do get it now!

Youre much better at explaining this stuff than my teacher :D

Thank you so much!
Topic archived. No new replies allowed.