-1

I'm a novice in coding... I've written a code in C++ to calculate the date after adding new numbers to date, month, and year. However, it's become too long and my computer can't process it if the date goes over 50.

Could you give me a few tips if there was a way to shorten this?

#include <iostream>
class Date{
  int year; int month; int day;
}
public:
void set_date(int _year, int _month, int _day){
    year=_year;
    month=_month;
    day=_day;
}
void add_day(int inc){
    day+=inc;
}
void add_month(int inc){
    month+=inc;
}
void add_year(int inc){
    year+=inc;
}

void get_date(){
    while (day>31){
        if ((year%4)==0 && month==2 && day > 29){
            month+=1;day-=29;
        }
        else if(month==2 && day > 28){
            month+=1;day-=28;
        }
        else if((day>30)&&(month==4||month==6||month==9||month==11)){
            month+=1;day-=30;
        }
        else if((day>31)&&(month==3||month==5||month==7||month==8||month==10||month==12||month==1)){
            month+=1;day-=31;
        }
    }
    if (month>12){
        year+=month/12;
        month=month%12;
    }
    std::cout<<"year"<<year<<std::endl;
    std::cout<<"month"<<month<<std::endl;
    std::cout<<"day"<<day<<std::endl;
}

int main(){
    Date date;
    date.set_data(191,2,10);
    date.add_day(60);
    date.add_month(10);
    date.add_year(3);

    date.get_date();
    return 0;
}
dcbadger
  • 9
  • 1
  • If you do not have the necessary used an your implementation of the Data, you can used the [ctime](http://www.cplusplus.com/reference/ctime/time/), you can add the time, look this [response](https://stackoverflow.com/questions/310363/how-to-add-one-day-to-a-time-obtained-from-time) – vincenzopalazzo Sep 22 '19 at 09:47
  • If *what* "goes over 50"? – Some programmer dude Sep 22 '19 at 09:47
  • 1
    And have you tried to *debug* your program? – Some programmer dude Sep 22 '19 at 09:48
  • Your `get_date()` doesn't have an `else`, and your conditions look so convoluted that it's highly probable you missed a condition and thus you go into an endless loop. You need to learn how to debug your code, because something like that is hard to spot when reading code, but incredibly easy to find when stepping through code with a debugger: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Max Vollmer Sep 22 '19 at 09:54
  • ok thank you I found out yesterday that that is actually what happened... – dcbadger Sep 28 '19 at 10:43

1 Answers1

1

When I run your code it loops forever with day=39 and month=13, which is a condition you don't check for. You might want to move the whole if (month>12) into your loop, so that the months get corrected while processing the days. You also want to add an else that stops the loop:

bool doneProcessing = false;
do
{
    if ((year % 4) == 0 && month == 2 && day > 29)
    {
        month += 1;
        day -= 29;
    }
    else if (month == 2 && day > 28)
    {
        month += 1;
        day -= 28;
    }
    else if ((day > 30) && (month == 4 || month == 6 || month == 9 || month == 11))
    {
        month += 1;
        day -= 30;
    }
    else if ((day > 31) && (month == 3  ||  month == 5 || month == 7 || month == 8 || month == 10 || month == 12 || month == 1))
    {
        month += 1;
        day -= 31;
    }
    else
    {
        doneProcessing = true;
    }
    if (month > 12)
    {
        year += month / 12;
        month = month % 12;
    }
}
while (!doneProcessing);

I didn't check the validity of your semantics, but I did notice one thing: When you have month=2 and day=30, you'd want to process that as well, but you only check day>31, thus I changed the loop to simply run until the else is hit (all days are valid now).

With this change your code properly terminates after printing this result:

year195
month2
day8

PS: With a debugger you can easily and quickly find such issues. See What is a debugger and how can it help me diagnose problems?

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43