So, i got this much done so far but im stuck and i cant get my third case statement to work, also for some reason it prints off the calendar like days 8 9 10, etc, if you copy and paste and run it you will see it doesnt line up correctly.

So, just looking for some help, maybe alittle clean up of my code etc.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int month, days, day, count=1;

cout<<"\t\t\t\tCalendar\n";

cout<<"Enter number of month (1 for january, 2 for february...): ";
cin>>month;
cout<<"Enter day of the month (1 for Sunday, 2 for Monday...): ";
cin>>day;

if (month ==1){cout<<"\n\t\t\tJanuary\n\n"; days=31;}
if (month ==2){cout<<"\n\t\t\tFebruary\n\n"; days=28;}
if (month ==3){cout<<"\n\t\t\tMarch\n\n"; days=31;}
if (month ==4){cout<<"\n\t\t\tApril\n\n"; days=30;}
if (month ==5){cout<<"\n\t\t\tMay\n\n"; days=31;}
if (month ==6){cout<<"\n\t\t\tJun\n\n"; days=30;}
if (month ==7){cout<<"\n\t\t\tJuly\n\n"; days=31;}
if (month ==8){cout<<"\n\t\t\tAugust\n\n"; days=31;}
if (month ==9){cout<<"\n\t\t\tSeptember\n\n"; days=30;}
if (month ==10){cout<<"\n\t\t\tOctober\n\n"; days=31;}
if (month ==11){cout<<"\n\t\t\tNovember\n\n"; days=30;}
if (month ==12){cout<<"\n\t\t\tDecember\n\n"; days=31;}

cout<<"\nSun\tMon\tTue\tWen\tThu\tFri\tSat\n";

while (count<=days){
	switch(day){
	case 1:

		cout<<count<<"\t";break;

	case 2:

        cout<<"\t"<<count<<" "; break;
   
	case 3:

		cout<<"\t\t"<<count<<" "; break;}// by the way case 3 does not display correctly at all...
        count++;}

    return 0;

}

Dani AI

Generated

Three quick, concrete points that explain what went wrong in the original thread (and how to fix it), followed by a compact, reliable printing approach.

  • Root causes seen in ’s code: mixing literal tabs with numbers of varying width causes columns to shift; the month switch falls through because case 4 is missing a break (April ends up with 31 days); and the logic that switches on the starting weekday only handled the first three cases, so most start values produced no output. ’s suggestion to first print a simple sequential grid is the right debugging step — get plain numbers aligned before adding user input handling.

  • Reliable approach (algorithm): convert the user start-day to a zero-based column (0 = Sunday), print a fixed-width header, output a number of fixed-width blank cells equal to the start offset, then print each date using a fixed cell width (std::setw) and emit a newline whenever the column reaches Saturday. Avoid tabs entirely for column alignment. For February, compute 29 days when a full year is known and is a leap year.

  • Minimal implementation pattern (compact, robust)

    #include <iostream>
    #include <iomanip>
    
    // get month (1-12), startDay (1=Sun..7=Sat), optional year (0 to skip)
    // uses fixed 4-char cells so single- and double-digit days align
    int monthNumber = /* input */, startDay = /* input */, year = /* input */;
    const int dim[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int days = dim[monthNumber];
    if (monthNumber == 2 && year != 0) {
      bool leap = (year%4==0 && (year%100!=0 || year%400==0));
      if (leap) days = 29;
    }
    int start = (startDay - 1) % 7;
    std::cout << "Sun Mon Tue Wed Thu Fri Sat\n";
    for (int i = 0; i < start; ++i) std::cout << "    ";
    for (int d = 1; d <= days; ++d) {
      std::cout << std::setw(3) << d << ' ';
      if ((start + d) % 7 == 0) std::cout << '\n';
    }
    std::cout << '\n';

Troubleshooting checklist: validate inputs (month 1..12, start 1..7), add missing break statements in month switches, test first by printing 1..N in a fixed grid as suggested, and prefer std::setw over \t so columns stay aligned regardless of terminal tab settings. Note: the OP later reported the problem was fixed, confirming these were the likely issues.

Recommended Answers

All 6 Replies

anyone? can anyone help me out please?

can someone give me an answer on whats wrong? its really frustrating and i wanna finish this.

What are you trying to do? can you please try to explain the logic behind this while loop?
I think you want to print out a calendar, but the logic below wont work at all.

while (count<=days){
	switch(day){
	case 1:
 
		cout<<count<<"\t";break;
 
	case 2:
 
        cout<<"\t"<<count<<" "; break;
 
	case 3:
 
		cout<<"\t\t"<<count<<" "; break;}// by the way case 3 does not display correctly at all...
        
count++; //WHY COUNT IS HERE??????
}

First try to give only following output using a loop.

1 2 3 4 5
6 7 8 9 19
11 12 13 14 15


after that..

which day of the week is starting day of the month?

ok, you dont have to worry this right now, first try to give a proper output of the calendar assuming that Monday is the 1st day of the month.

After that we can concentrate on this question.

Stop bumping your thread. It's rude! We are not staff waiting to answer your question, we are volunteers that have lives. We'll get to you when we log in and have an answer for you.

So this is my updated code, but still it isnt printing out right inside the command window. But this code is alittle bit more cleaned up and looks better and runs more efficiently, but if you seem something wrong let me know, also i have to make it print out all 12 months based on the users input but i started small doing just one month, so if u have any ideas on how i can do that please let me know.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int month, days, day = 0;

	cout << "\t\t\t\tCalendar" << endl;

	cout << "Enter number of month (1 for january, 2 for february...): ";
	cin >> month;
	cout << endl << "Enter day of the month (1 for Sunday, 2 for Monday...): ";
	cin >> day;
	cout << endl;

	switch (month)
	{
	case 1:
		cout << "\t\t\tJanuary" << endl << endl;
		days = 31;
		break;
	case 2:
		cout << "\t\t\tFebruary" << endl << endl;
		days = 28;
		break;
	case 3:
		cout << endl << "\t\t\tMarch" << endl << endl;
		days = 31;
		break;
	case 4:
		cout << endl << "\t\t\tApril" << endl << endl;
		days = 30;
	case 5:
		cout << endl << "\t\t\tMay" << endl << endl;
		days = 31;
	break;
	case 6:
		cout << endl << "\t\t\tJun" << endl << endl;
		days = 30;
		break;
	case 7:
		cout << endl << "\t\t\tJuly" << endl << endl;
		days = 31;
		break;
	case 8:
		cout << endl << "\t\t\tAugust" << endl << endl;
		days = 31;
		break;
	case 9:
		cout << endl << "\t\t\tSeptember" << endl << endl;
		days = 30;
		break;
	case 10:
		cout << endl << "\t\t\tOctober" << endl << endl;
		days = 31;
		break;
	case 11:
		cout << endl << "\t\t\tNovember" << endl << endl;
		days = 30;
		break;
	case 12:
		cout << endl << "\t\t\tDecember" << endl << endl;
		days = 31;
		break;
	default:
		break;
	}

	cout << endl << "Sun\tMon\tTue\tWen\tThu\tFri\tSat" << endl;

	for (int i = 1; i <= days; ++i)
	{
			switch(day)
			{
			case 1:
				cout<< i << "\t";
				break;
			case 2:
				cout<<"\t"<< i <<" ";
				break;
			case 3:
				cout<<"\t\t"<< i <<" ";
				break;
			default:
				break;
			}
	}

	char k;
	cin >> k;
	return 0;
}

This problem was solved i fixed everything thank you guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.