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

  • C/C++
  • Thread starter frixis
  • Start date
  • Tags
    C++ Visual
In summary, the programmer is trying to create a calendar where the user inputs the first day of the month and month itself, and a calendar is generated for an entire year. They are having trouble with Month, and want some help. They have code that generates a calendar for a month, but they need help with the first day of the month. They also mention that they have not used C++ in years, and that there are some common pitfalls when programming without an API.
  • #1
frixis
19
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
  • #2
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
 
  • #3
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...
 

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

What is "Calendar visual c++ help"?

"Calendar visual c++ help" is a term used to refer to a programming tool or feature in the C++ programming language that allows for the creation and manipulation of calendars and dates within a computer program.

How do I use "Calendar visual c++ help"?

To use "Calendar visual c++ help", you first need to have a basic understanding of the C++ programming language. Then, you can refer to the documentation or tutorials provided by the tool or feature to learn how to implement it in your code.

What are the benefits of using "Calendar visual c++ help"?

Using "Calendar visual c++ help" can save time and effort when working with dates and calendars in a program. It also allows for more precise and accurate calculations and manipulations of dates and times.

Are there any limitations to "Calendar visual c++ help"?

Like any programming tool or feature, there may be limitations to "Calendar visual c++ help" depending on the specific implementation or version. It is important to refer to the documentation or seek help from experienced programmers if you encounter any issues.

Can "Calendar visual c++ help" be used in any type of program?

Yes, "Calendar visual c++ help" can be used in any type of program that is written in the C++ programming language. However, the specific implementation and features may vary depending on the programming environment or platform.

Similar threads

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