-1

A line occurs twice after executing a method. Most thing I know is that the problem comes from tasks[i].getdue() line. If I remove this line totally nothing go wrong but this function makes this bug. I'm guessing this line actually resume the code flow and does not interrupt it.

Is there any way to fix this?

This is my code:

#include<iostream>
#include<string.h>

using namespace std;

class Time
{
    private:
        int days,hour,minute,seconds;
    public:
        Time(int hour=0,int minute=0,int seconds=0)
        {
            minute+=seconds/60;
            seconds=seconds%60;
            hour+=minute/60;
            minute=minute%60;
            days=hour/24;
            hour=hour%24;
            this->hour=hour;
            this->minute=minute;
            this->seconds=seconds;
        }
        void set(int hour=0,int minute=0,int seconds=0)
        {
            minute+=seconds/60;
            seconds=seconds%60;
            hour+=minute/60;
            minute=minute%60;
            days=hour/24;
            hour=hour%24;
            this->hour=hour;
            this->minute=minute;
            this->seconds=seconds;
        }
        int get_days()
        {
            return(days);
        }
        int ToSec()
        {
            return(days*86400+hour*60*60+minute*60+seconds);
        }
        void add(int seconds)
        {
            set(0,0,ToSec()+seconds);
        }
        void del_days()
        {
            days=0;
        }
        void show(int choice=1)
        {
            switch (choice)
            {
                case 0:
                    cout<<hour<<':'<<minute<<':'<<seconds;
                    break;
                case 1:
                    if(hour<10)
                    {
                        cout<<0<<hour;
                    }
                    else
                    {
                        cout<<hour;
                    }
                    cout<<':';
                    if(minute<10)
                    {
                        cout<<0<<minute;
                    }
                    else
                    {
                        cout<<minute;
                    }
                    cout<<':';
                    if(seconds<10)
                    {
                        cout<<0<<seconds;
                    }
                    else
                    {
                        cout<<seconds;
                    }
            }
        }
};
class DeltaTime
{
    private:
        int Delta_days,Delta_hours,Delta_minutes,Delta_seconds;
    public:
        int give_DD()
        {
            return Delta_days;
        }
        int give_DH()
        {
            return Delta_hours;
        }
        int give_minutes()
        {
            return Delta_minutes;
        }
        int give_seconds()
        {
            return Delta_seconds;
        }
        int give_seconds_total()
        {
            return(Delta_days*60*60*24+Delta_hours*60*60+Delta_minutes*60+Delta_seconds);
        }
        DeltaTime(int dd=0,int dh=0,int dm=0,int ds=0)
        {
            dm+=ds/60;
            ds=ds%60;
            dh+=dm/60;
            dm=dm%60;
            dd+=dh/24;
            dh=dh%24;
            Delta_days=dd;
            Delta_hours=dh;
            Delta_minutes=dm;
            Delta_seconds=ds;
        }
};
class Date
{
    private:
        char mname[12][4]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
        char weekname[7][4]={"sun","mon","tue","wed","thu","fri","sat"};
        short int mlengh[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int year,month,day;
        bool isleapyear2(int years)
        {
            bool flag;
            if(years%100==0 && years%400==0)
            {
                flag=1;
            }
            else if(years%100==0)
            {
                flag=0;
            }
            else if(years%4==0)
            {
                flag=1;
            }
            else
            {
                flag=0;
            }
            return(flag);
        }
    public:
        Date(int year=0,int month=1,int day=1)
        {
            short int mlen[12]={31,28,31,30,31,30,31,31,30,31,30,31};
            bool kabise;
            kabise=isleapyear();
            if(kabise)mlen[1]=29;
            if(year>=0 and(month>=1 && month<=12) && (day>0 && day<=mlen[month-1]))
            {
                this->year=year;
                this->month=month;
                this->day=day;
            }
            else
            {
                cout<<"this input is not correct.setting default of 0/1/1"<<endl;
                this->year=1;
                this->month=1;
                this->day=1;
            }
        }
        void get()
        {
            int year;
            do
            {
                cout<<"enter year : ";
                cin>>year;
                if(year<1)cout<<"err:bad year number";
            }while(year<1);
            this->year=year;
            cout<<endl;
            int month;
            do
            {
                cout<<"enter month : ";
                cin>>month;
                if(month<=0 || month>12)cout<<"err:bad month number";
            }while(month<=0 || month>12);
            this->month=month;
            cout<<endl;
            int day;
            do
            {
                cout<<"enter day : ";
                cin>>day;
                if(day<1 || day>(mlengh[month]+(isleapyear2(year) && month==2)))cout<<"err: bad day number";
            }while(day<1 || day>(mlengh[month]+(isleapyear2(year) && month==2)));
            this->day=day;
        }
        void set(int year=1,int month=1,int day=1)
        {
            bool kabise;
            kabise=isleapyear();
            if(kabise)mlengh[1]=29;
            if(!((month>0 && month<=12) && (day>0 && day<=mlengh[month])))
            {
                cout<<"ERR : incorrect input"<<endl;
            }
            else
            {
                this->year=year;
                this->month=month;
                this->day=day;
            }
        }
        bool isleapyear()
        {
            bool flag;
            if(year%100==0 && year%400==0)
            {
                flag=1;
            }
            else if(year%100==0)
            {
                flag=0;
            }
            else if(year%4==0)
            {
                flag=1;
            }
            else
            {
                flag=0;
            }
            return(flag);
        }
        unsigned long int todays()
        {
            unsigned long int days=0;
            for(int i=0;i<year;i++)
            {
                days+=(365+isleapyear2(i));
            }
            switch(month)
            {
                case 12:days+=(334+isleapyear2(year));break;
                case 11:days+=(304+isleapyear2(year));break;
                case 10:days+=(273+isleapyear2(year));break;
                case  9:days+=(243+isleapyear2(year));break;
                case  8:days+=(212+isleapyear2(year));break;
                case  7:days+=(181+isleapyear2(year));break;
                case  6:days+=(151+isleapyear2(year));break;
                case  5:days+=(120+isleapyear2(year));break;
                case  4:days+=( 90+isleapyear2(year));break;
                case  3:days+=( 59+isleapyear2(year));break;
                case  2:days+= 31;break;
                case  1:days+=  0;break;
                default:days+=0;
            }
            days+=day;
            return days;
        }
        void todate(int days=0)
        {
            int years=0;
            while(days>(365+isleapyear2(years)))
            {
                days-=(365+isleapyear2(years));
                years++;
            }
            if(days>(334+isleapyear2(years)))
            {
                days-=(334+isleapyear2(years));
                month=12;
            }
            else if(days>(304+isleapyear2(years)))
            {
                days-=(304+isleapyear2(years));
                month=11;
            }
            else if(days>(273+isleapyear2(years)))
            {
                days-=(273+isleapyear2(years));
                month=10;
            }
            else if(days>(243+isleapyear2(years)))
            {
                days-=(243+isleapyear2(years));
                month=9;
            }
            else if(days>(212+isleapyear2(years)))
            {
                days-=(212+isleapyear2(years));
                month=8;
            }
            else if(days>(181+isleapyear2(years)))
            {
                days-=(181+isleapyear2(years));
                month=7;
            }
            else if(days>(151+isleapyear2(years)))
            {
                days-=(151+isleapyear2(years));
                month=6;
            }
            else if(days>(120+isleapyear2(years)))
            {
                days-=(120+isleapyear2(years));
                month=5;
            }
            else if(days>(90+isleapyear2(years)))
            {
                days-=(90+isleapyear2(years));
                month=4;
            }
            else if(days>(59+isleapyear2(years)))
            {
                days-=(59+isleapyear2(years));
                month=3;
            }
            else if(days>31)
            {
                days-=31;
                month=2;
            }
            else
            {
                month=1;
            }
            year=years;
            day=days;
        }
        void show()
        {
            cout<<weekname[(todays()-1)%7]<<'\t'<<year<<'/'<<mname[month-1]<<'/'<<day;
        }
        Date operator +(int days)
        {
            Date x;
            x.todate(todays()+days);
            return x;
        }
        Date operator +(Time t1)
        {
            Date x;
            x.todate(todays()+t1.get_days());
            t1.del_days();
            return x;
        }
        Date operator =(Date d2)
        {
            unsigned long int ddays;
            ddays=d2.todays();
            Date d1(0,0,ddays);
            return d1;
        }
};
class DateTime
{
    public:
        Date d8;
        Time tym;
        void set(Date idate,Time itime)
        {
            d8=idate;
            tym=itime;
        }
        void set(DateTime d2)
        {
            d8=d2.d8;
            tym=d2.tym;
        }
        Time get_time()
        {
            return tym;
        }
        Date get_date()
        {
            return d8;
        }
        void operator = (DateTime d2)
        {
            d8=d2.d8;
            tym=d2.tym;
        }
        void add(DeltaTime x)
        {
            tym.add(x.give_seconds_total());
            d8=d8+tym;
        }
        void show()
        {
            d8.show();
            cout<<'\t';
            tym.show(1);
        }
};
class task
{
    private:
        char title[100];
        DateTime begin,end;
        DeltaTime repeat;
    public:
            string name;
            void set(DateTime begin,DateTime end,DeltaTime repeat,string name)
            {
                this->begin.set(begin);
                this->end.set(end);
                this->name=name;
            }
            void done()
            {
                if(repeat.give_seconds_total()!=0)
                {
                    //begin=now;
                    end.add(repeat);
                }
                else
                {
                    cout<<"good job :)";
                    delete this;
                }
            }
            void showdue()
            {
                cout<<"due at : ";
                end.show();
            }
            void getdue()
            {
                end.d8.get();
            }
};
bool check_for(char str[100],char substr[100],int start=0)
{
    int lengh=strlen(substr);
    bool flag=true;
    for (int i = start ; i <(start+lengh) ; i++)
    {
        if(str[i]!=substr[i-start])
        {
            flag=false;
        }
    }
    return flag;
}
string cut_str(char str[100],int adr)
{
    string opt="",h;
    for(int i=adr;i<int(strlen(str));i++)
    {
        h=str[i];
        opt+=h;
    }
    return opt;
}
string split_first(string str)
{
    bool d=false;
    string opt="";
    for(int i=0;i<int(str.length());i++)
    {
        if(str[i]==' ' && d==true)
        {
            break;
        }
        else
        {
            if(str[i]!=' ')
            {
                d=true;
                opt+=str[i];
            }
        }
    }
    return opt;
}
int check_inp(string inp)
{
    if(inp=="exit")return 0;
    else if(inp=="addtask")return 1;
    else if(inp=="setdue")return 2;
    else if(inp.size()==0)return -1;
    else return -2;
}
int main()
{
    task tasks[100];
    int t_counter=0;
    string str2,name;
    string inp_status;
    char cmd[100];
    bool done=false;
    while(!done)
    {
        cout<<"enter commmand : ";
        cin.getline(cmd,100);
        inp_status=split_first(cmd);
        switch(check_inp(inp_status))
        {
            case 0:
            {
                done=1;
                break;
            }
            case 1:
            {
                str2=cut_str(cmd,8);
                tasks[t_counter].name=str2;
                break;
            }
            case 2:
            {
                tasks[0].getdue();
                break;
            }
            case -2:
            {
                cout<<split_first(cmd)<<":unknown command"<<endl;
                break;
            }
            default:
            {
                cout<<'\a';
            }
        }
    }
}

And every time I do setdue x after it's done it always show two enter commands : in one single line.

like this one:

enter commmand : addtask 1
enter commmand : setdue 1
enter year : 1

enter month : 1

enter day : 1
enter commmand : enter commmand : 
enter commmand : exit


------------------
(program exited with code: 0)
Press return to continue
bad_coder
  • 5,829
  • 13
  • 26
  • 41
SomeGuy04
  • 1
  • 3
  • 5
    Would you please make a minimal verifyable example, as 553 lines are quite long. – JCWasmx86 Aug 05 '20 at 14:04
  • Yep, please focus on the minimal part of [mre]. Your input along with expected and acutal output will also help. – churill Aug 05 '20 at 14:08
  • added the command line example :) – SomeGuy04 Aug 05 '20 at 14:13
  • 1
    That's a lot of code, but it's most likely a case of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – molbdnilo Aug 05 '20 at 14:39

1 Answers1

0

problem solved thanks link provided by @molbdnilo all i had to do was to place a cin.ignore() line in here:

case 2:
            {
                tasks[0].getdue();
                //here
                break;
            }

thanks a lot for your support :D

SomeGuy04
  • 1
  • 3