C++ Calendar
Mar 3, 2015 at 11:35pm UTC
I've nearly completed my C++ project to create a calendar, but am running into a small problem. After my calendar is displayed, the Saturday on the first week is always blank. I've nearly scratched my hair out trying to figure this out.
Any assistance would be appreciated.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
int daysInMonth, year, day;
int monthStartDay = 0;
string month;
char response = 'y' ;
while (response == 'y' || response == 'Y' ){
response = 'm' ;
cout << "Please enter a year: \n" ;
cin >> year;
cout << "Please enter the name of the month: \n" ;
cin >> month;
cout << "Enter the first day of the month: \n" ;
cout << "[0=SUN | 1=MON | 2=TUES | 3=WED | 4=THURS | 5=FRI | 6=SAT]\n" ;
cin >> monthStartDay;
while (monthStartDay < 0 || monthStartDay > 6)
{
cout << "Invalid entry. Re-Enter day. \n" ;
cin >> monthStartDay;
}
cout << "Enter how many days are in the month: \n" ;
cin >> daysInMonth;
cout << "\n\n\n" ;
cout << setw(14) << month << " " << year << "\n" ;
cout <<" Su Mo Tu We Th Fr Sa \n" ;
if (monthStartDay == 0)
cout << setw(0);
else if (monthStartDay == 1)
cout << setw(4) <<" " ;
else if (monthStartDay == 2)
cout << setw(8) <<" " ;
else if (monthStartDay == 3)
cout << setw(12) <<" " ;
else if (monthStartDay == 4)
cout << setw(16) <<" " ;
else if (monthStartDay == 5)
cout << setw(20) <<" " ;
else if (monthStartDay == 6)
cout << " " ;
for (day = 1; day <= daysInMonth; day++)
{
if ((monthStartDay + day) % 7 == 0)
cout << endl;
cout << setw(4) << day;
}
cout << "\n\n\nWould you like to repeat this program?\n" ;
cin >> response;
}
return 0;
}
Mar 4, 2015 at 12:07am UTC
Make day start at 0 not 1.
for (day = 0; day < daysInMonth; day++)
Then just display what you want it to look like by adding +1
cout << setw(4) << day+1;
Overall -
1 2 3 4 5 6
for (day = 0; day < daysInMonth; day++)
{
if ((monthStartDay + day) % 7 == 0)
cout << endl;
cout << setw(4) << day + 1;
}
Last edited on Mar 4, 2015 at 12:08am UTC
Mar 4, 2015 at 1:44am UTC
*smacked head
Thank you so much! If only I had asked sooner!
You rock @TarikNeaj!!
Mar 4, 2015 at 9:13am UTC
Glad I could help :) goodluck!
Mar 4, 2015 at 10:46am UTC
Last edited on Mar 4, 2015 at 10:47am UTC
Topic archived. No new replies allowed.