a program that turns work into a monthly basis

Asked 2 years ago, Updated 2 years ago, 19 views

#include <iostream>

using namespace std;

void mthchange(int day) {

    int mday = 0, mth = 0;
    int arr[12] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};

    while (mday <= day) {

        mday += arr[mth];
        mth++;  
    }

    cout <<mth << "Month" << day - mday << "Day" << endl;
}

int main (void) {

    int i;

    cin >> i;

    mthchange(i);

    return 0;
}

I did it like this. If I enter 12, it says February 19th.

c++

2022-09-21 17:56

2 Answers

#include <iostream>

using namespace std;

void mthchange(int day) {
    int mday = day, mth = 0;
    int arr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    For (mth = 0; mday > arr[mth]; mth = (mth + 1) % 12) //Exit if the number of days in the month is less than or equal to the number of days
    {
        mday -= arr[mth];
    }
    cout << mth + 1 << "Month" << mday << "Day" << endl;
}

int main(void) {

    int i;

    cin >> i;

    mthchange(i);

    return 0;
}

If you enter more than 366, you will get the value you want.

Leap year treatment is excluded.


2022-09-21 17:56

If you want to keep the code to a minimum, Let's just change the conditions of the while statement.

while (mday + arr[mth] < day)

I think this will work, but I don't know exactly because I haven't tried it.


2022-09-21 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.