As Part of an assignment one of the requirments is to convert a date into month format. that is the user must enter start month and year of employment and end month and year of employment for example 4 1980 till 7 2009 (mmyyy)
these two dates must be coverted to months easy enough. Then those months need to converted back to years and months worked in this case it will work out to 28 years and 4 months. I have busted my head trying to get this right and for most it works but if i use a date such as 1 2003 till 12 2004 the output isnt correct. i dont know what im doing wrong as the formulas i made up seem legit on paper but not on program yet its basic addition subtraction and multiplication.

Could someone please assist me with finding my fault. code attempt below

#include <iostream>
#include <assert.h>

using namespace std;

int main()
    int smnt;
    int syear;
    int emnt;
    int eyear;
    int ty;
    int tm;
    int GetMonths;
    //int pension;
   // int calcty;
    int ConvertBacktoYears;
    int TotalYearsToMonths;
    int TotalYearsAndMonths;
    cout << "Please enter the first month of employment" << endl;
    cin >> smnt;
    //assert month sm>0 & sm<13
    assert(smnt>0 && smnt<13);

    cout << "Please enter the year you started work" << endl;
    cin >> syear;
    //assert syear
    assert(syear>1974 && syear<2011);

    // check what values where entered for start of employment
    //cout << (smnt)<<" and "<<(syear) << endl;

     cout << "Please enter the last month of employment " << endl;
    cin >> emnt;
    //assert month em>0 & em<13
    assert(emnt>0 && emnt<13);

    cout << "Please enter the last year of employment" << endl;
    cin >> eyear;
    // assert eyear
    assert(eyear>1974 && eyear<2011);

    // check what values where entered for end of employment
    //cout << (emnt)<<" and "<<(eyear) << endl;
    ty = eyear-syear;
    TotalYearsToMonths = (ty*12);
    tm = ((12-(smnt- 1)) + emnt);
    TotalYearsAndMonths = ((TotalYearsToMonths- (smnt)- (12-emnt))+tm);
    ConvertBacktoYears = (TotalYearsAndMonths/12);
    GetMonths = TotalYearsAndMonths-(ConvertBacktoYears *12);
    cout<< " years " <<ConvertBacktoYears<< " months "<< GetMonths<< endl;
    return 0;
5 Years
Discussion Span
Last Post by skannigan

I think the calculations are too complicated. Try this:

// Get the total months for full years worked
tm = (eyear - syear) * 12;

// Remove months not worked from the first year
tm -= (smnt - 1);

// Add months worked from the last year
tm += emnt;
cout << " years " << (tm / 12) << " months " << (tm % 12) << endl;

There are a number of ways to handle the odd months, the above assumes that the edge months are inclusive. So if the starting date was 1-31-2003 and the ending date was 12-1-2003, it would still come out to a full year of employment. To get more precise you would need to include days, or make an assumption about how many days are worked in those two months.


thanks so much, i just managed to work out a way and it seems legit through all my tests so far, but im looking at your code and i see you've done it in such a simpler way im going to try to adapt yours to my program since it would make better sense below is my attemp it works but its messy

ty = eyear-syear;
    TotalYearsToMonths = 12+(ty*12);
    tsm = 12-(12-smnt)-1;
    tem = 12-emnt;
    TotalYearsAndMonths = TotalYearsToMonths- tsm -tem;
    ConvertBacktoYears = (TotalYearsAndMonths/12);
    GetMonths = TotalYearsAndMonths-(ConvertBacktoYears *12);
    cout<< " years " <<ConvertBacktoYears<< " months "<< GetMonths<< endl;
    return 0;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.