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

  • Thread starter Thread starter frixis
  • Start date Start date
  • Tags Tags
    C++ Visual
AI Thread Summary
The discussion revolves around creating a calendar program in C++ that generates a full year of months based on user input for the starting month and the first day of that month. The user has successfully implemented a loop to generate the calendar for months starting from the input month but is struggling with correctly cycling through the months and managing the transition from December back to January. Key points include the need to ensure that the program counts through all 12 months regardless of the starting month input. Suggestions include using a modulo operation to handle month cycling and utilizing arrays to store month names and days, which would simplify the code and improve readability. There are also considerations about handling leap years and the correct day of the week for the start of each month, indicating the complexity of date manipulation without advanced features or libraries. The user expresses limitations in their current knowledge of strings and advanced data structures, focusing instead on basic programming concepts.
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;
cout<<"\t\t\t::CALENDAR::\n";
cout<<"\n\nenter number of month:\t";
cin>>month;

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

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

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


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

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


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

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


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

}
}

month++;
cout<<"\n\n";

}
cout<<"\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++) cout << "\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++) cout << "\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...
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
3K
Replies
23
Views
2K
Replies
19
Views
1K
Replies
2
Views
1K
Replies
23
Views
2K
Replies
5
Views
2K
Back
Top