How Can I Generate a Calendar in C++ Using User Input for First Day and Month?

  • Context: C/C++ 
  • Thread starter Thread starter frixis
  • Start date Start date
  • Tags Tags
    C++ Visual
Click For Summary
SUMMARY

The discussion focuses on generating a calendar in C++ based on user input for the starting month and day of the week. The user has successfully created a basic calendar for a single month but struggles with iterating through all twelve months starting from a user-defined month. Key suggestions include using the modulo operator to cycle through months and implementing arrays to store month names and days, which simplifies the code structure. The importance of handling leap years and the transition between months is also emphasized.

PREREQUISITES
  • Basic C++ programming knowledge
  • Understanding of loops and conditionals in C++
  • Familiarity with arrays in C++
  • Knowledge of date manipulation concepts
NEXT STEPS
  • Implement C++ arrays to store month names and days in each month
  • Learn about the modulo operator in C++ for cycling through months
  • Research date manipulation libraries or APIs in C++ for advanced functionality
  • Explore handling leap years in calendar calculations
USEFUL FOR

C++ beginners, software developers creating calendar applications, and anyone interested in learning about date manipulation in programming.

frixis
Messages
17
Reaction score
0
okay so they asked us to make an entire calendar where the user inputs the first day of the month and month itself and a calendar is generated for an entire year..
so far I've gotten to being able to generate a good thingi for a month.. and well I've used a while loop to repeat till the months get to 12... as in no matter wat month number u enter it'll get to december.. but like if u entered 4 it wudnt do months 4,5,6,7,8,9,10,11,12,1,2,3,4.. it'd do 4 to 12 only
and i also don't get how the next month to start from where the previous month ended..
i don't need the program thingi itself. but hints'd be nice..
heres my code so far:

#include<iostream.h>
int space;
int day;
int date;
int month;


int main ()
{
space=0;
count<<"\t\t\t::CALENDAR::\n";
count<<"\n\nenter number of month:\t";
cin>>month;

count<<"\n\nenter first day of the month:\t";
count<<"\n\nMonday = 0\nTuesady = 1\nWednesday = 2";
count<<"\nThursday = 3\nFriday = 4\nSaturday = 5";
count<<"\nSunday = 6\n";
cin>>day;
count<<"\n\n\t\t";
while (month<=12)
{ if (month==1)
count<<"JANUARY";
else if (month==2)
count<<"FEBRUARY";
else if (month==3)
count<<"MARCH";
else if (month==4)
count<<"APRIL";
else if (month==5)
count<<"MAY";
else if (month==6)
count<<"JUNE";
else if (month==7)
count<<"JULY";
else if (month==8)
count<<"AUGUST";
else if (month==9)
count<<"SEPTEMBER";
else if (month==10)
count<<"OCTOBER";
else if (month==11)
count<<"NOVEMBER";
else if (month==12)
count<<"DECEMBER";
else
{count<<"invalid month, calendar will close";
return 0;
}
count<<"\n\nMon\tTue\tWed\tThu\tFri\tSat\tSun\n";

while (space<day)
{count<<"\t";
space++;
}

if (month==4||month==6||month==9||month==11)
{
for (date=1;date<31;date++)
{
++space;
count<<date;
count<<"\t";


if (space%7==0)
{count<<"\n";
}

}
}
else if (month==2)
{
for (date=1;date<29;date++)
{
++space;
count<<date;
count<<"\t";


if (space%7==0)
{count<<"\n";
}

}
}
else
{for (date=1;date<=31;date++)
{
++space;
count<<date;
count<<"\t";


if (space%7==0)
{count<<"\n";
}

}
}

month++;
count<<"\n\n";

}
count<<"\n\n";
return 0;

}
 
Technology news on Phys.org
Your month-counting approach is wrong. If starting at 4 has to produce 4 through 12 and 1 through 3, then you cannot simply increment to 12 months.

We know that regardless of what the user enters, the program has to return 12 different months, right?

So we have to count to 12 no matter what, and still take the user input (lets say 4) into consideration. So how about this:

for(i = 0; i < 12; i++) count << "\nCurrent month number: " + (i + 4) % 12;

(I haven't used c++ in years, but I think % is modulo.)

As for the days and the names of the months, you should look into using an API for that if you are allowed to. MFC classes or the win32 API or *something*. Manipulating dates is a major pain without it.

If you are not allowed to, I would suggest that you make some sort of structure to contain the different names of the months, the names of the days, the amount of days in each month and so on. Your code gets ugly and hard to work with if you spell it out like you are currently doing.

You can put the data in arrays like this:

String *NameOfMonth [] = { "January", "February" ... };
String *DaysInMonth [] = { 31, 29, 31, 30 .. };
String *NameOfDay [] = { "Sunday", "Monday" ..};

Then do something like this:

for(i = 0; i < 12; i++) count << "\n" + NameOfMonth[(i + 4) % 12];

Typical pitfalls if you cannot use an API: Different numbers of days in February in leapyears. And if June ends on a Friday, Juli starts on a Saturday (ie, you cannot use a simple loop to iterate through all the days of a month, you need an independent variable to keep track of the day of the week).

Hope this helps point you in the right direction.

k
 
we haven't done strings and stuff yet... its just very basic stuff at the momment...
but i did the date thingi...
i set space to 0 if space%7==0,
and then in the end i set date to space
and then set space to 0 again...
so that worked out well...
i don't get how to do the month thing... sorry... it totally escapes me...
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K