Create 3 month calendar with (For Loop)

Hi.

i want to create a 3 month calendar use c++ based on input start of month-day from user input with for loop; so i wrote this codes :

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>
using namespace std;

int main(int argc, char *argv[]) {
int i,d; 
cout<<"Enter Start Day : ";
cin>>d;
cout<<"\nSAT SUN MON TUE WED THU FRI\n";
for (i=1;i<=d;i++) {
    if (d>1)
    cout<<"   ";
    }
for (i=1;i<=90;i++) {
    if(i<10)
cout<<i<<"   ";
else 
cout<<i<<"  ";
if ((d-1+i)%7==0)
    cout<<"\n";
if (i%30==0 && i<90) {
    cout<<"\n\n";
    cout<<"SAT SUN MON TUE WED THU FRI\n";
}
}  
  return 0;
}


everything is ok,1st month is currectly created,but the problem is on 2nd and 3rd month. i want to start new month from a day after previous Month ended with.

like This :

1
2
3
4
5
6
SAT SUN MON TUE WED THU FRI
         27  28  29  30

**NEW MONTH**
SAT SUN MON TUE WED THU FRI
                          1


what i should to do.?
Thanks.
Last edited on
Use a variable to keep track of the last day of the month, then you will know that the next month will be the next day after it, you can use an enum class for that

 
enum class days: int {MON=1,TUE,WED,THU,FRI,SAT,SUN};


If the last day of the month will be Sunday, then the first day of the next month will be Monday, otherwise it will be last day+1
Thanks, but i'm newbie in c++ and it's my homework exercise, and we only learn (if,for,int,float) and arrays. and i can't use enum class.
anyway with if and for.?
thanks.
Then just use numbers, enum class is more for convenience. If the last day of the month is 6(Sunday), then the first day of the next month is 0 (Monday), otherwise, it's gonna be last day+1.
you'r right,but i don't know how to do it..and how to use another integer to Keep track of what the current column is.
You can use a bi-dimensional array for this or use another variable that increases every time you add a day and resets to 0 if it goes over 6 (because you start from 0). Just integrate it in your loop for days.

1
2
3
if(day_counter==6) // check if the last value is Sunday
day_counter=0; //then the next will be Monday
else ++day_counter; //otherwise increase it by 1 
thanks,but i think it's doesn't work because the calendar create by user input start day and it's doesn't static.
Last edited on
I don't see the problem, the day_counter will take the same value as the day the user inputs.
thanks,anyway i don't know how to use your code to complete my code,i'm newbie to c++.
any other help.?
Last edited on
Topic archived. No new replies allowed.