dimanche 6 septembre 2020

Creating calendar, output formatting problem

Trying to create a calendar for my lab at school and need some help. My program works fine as in showing the days according to the month and if its a leap year or not. But it doesn't work well on displaying the date with the corresponding day. For example, my output is this :

Jan 1996
---------------------------------
Sun  Mon  Tue  Wed  Thu  Fri  Sat
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

as you can see, the numbers arent lined up with the days and I cant seem to get them properly lined up. I'd like to understand this so if anyone can help please let me know what I'm doing wrong rather than flat out telling the answer.

Full Code:

class Calendar {

public:
    int Year;
    int Month;
    int Day;
    string MonthNames[12] = { "Jan","Feb","March","April","May", "June","July", "August", "Sept","Oct","Nov","Dec" };
    string Name;
    string DayNames[7] = { "Sun", "Mon", "Tue", "Wed","Thu", "Fri", "Sat" };
    int AmountOfDays = 31; //base value

    void AskForDate() {

        cout << "Enter Year" << endl;

        cin >> Year;

        SetUpCalendarUI();
    }
    void SetUpCalendarUI()
    {
            if (LeapYear()) {
                SetDaysUI();
            }
            else {
                cout << "Not Leap year";
            }
        
        
    }
    bool LeapYear() {
        if (Year % 4 == 0 || Year % 400 == 0 && Year % 100 != 0) {
            return true;
        }
        else {
            return false;
        }
    }

    int ChangeAmountOfDays(int Month) {
            switch (Month) {

            case 1: return 31;
            case 2:
                if (!LeapYear()) {
                    return 28;
                }
                else {
                    return 29;
                }
            case 3: return 31;
            case 4: return 30;
            case 5: return 31;
            case 6: return 30;
            case 7: return 31;
            case 8: return 31;
            case 9: return 30;
            case 10: return 31;
            case 11: return 30;
            case 12: return 31;
            }
            return 0;

    }

    void SetDaysUI() {
        int i;

        //print out month names 1 by 1
        for (int k = 0; k < 12; k++) {
            AmountOfDays = ChangeAmountOfDays(k+1);
            cout << "\t" << MonthNames[k] << " " << Year << "\n";
            cout << "---------------------------------" << "\n";
            //print out days 1 by 1
            for (int DayOfWeek = 0; DayOfWeek < 7; DayOfWeek++) {
                cout << DayNames[DayOfWeek] << setw(5);
            }
            cout << "\n";

            //we want to set the amount of days == to the month. 
            //i = current Day put a space in between.
            //j = Total Amount of days
            for (i = 0; i < Day; i++)
                cout << "   ";
                for (int j = 1; j <= AmountOfDays; j++) {
                    cout << j << setw(3);

                    if (i++ > 5) {
                        i = 0;
                        cout << "\n";
                    }
                }
            
            if (i) {
                cout << "\n";
                Day = i;
            }
        }
        
        return;
    }

};

Aucun commentaire:

Enregistrer un commentaire