Tick function

So, i have this code and my tick function doesn't work as i wanted to, how can i change the problem to show 11:00:00 after i input 10:59:59(the ints for hour)?


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using std::cout;
using std::endl;
using namespace std;

class DateAndTime
{
    public:
    DateAndTime(int = 23, int = 59, int = 0,int = 19, int = 3, int = 2020);
    void setDateAndTime(int, int, int, int, int, int );
    void printLong ();
    void tick ();
    private:
 int hour; //0-23
 int minute; //0-59
 int second; //0-59
 int day;
 int month;
 int year;
};

DateAndTime::DateAndTime(int hr, int min, int sec, int d, int m, int y)
{
day = d;
month = m;
year = y;
setDateAndTime(hr,min,sec,d,m,y);
}
void DateAndTime::setDateAndTime(int h, int min, int s,int d, int m, int y)
{
hour = (h >= 0 && h < 24) ? h : 0;
minute = (min >= 0 && min < 60) ? min : 0;
second = (s >= 0 && s < 60) ? s : 0;
}

void DateAndTime::printLong()
{
 cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)<< ":" << (minute < 10 ? "0" : "") << minute<< ":" << (second < 10 ? "0" : "") << second<< (hour < 12 ? " AM " : " PM ");
 cout << day << '-' << month << '-' << year;
}
void DateAndTime::tick()
{
    if(second<60)
        second=second+1;
    else second=0;
    if (minute<60 && second ==0)
        minute=minute+1;
    if (hour<24 && minute==0 && second ==0 )
        hour=hour+1;
}

int main()
{

 DateAndTime t1;
 DateAndTime dateandtime1;
 cout << endl << " ";
 t1.printLong();
 t1.tick();
 cout <<endl;
 t1.printLong();
  return 0;
  }
Last edited on
They're dependent conditions, not independent.
1
2
3
4
5
6
7
8
9
10
if ( second < 60 ) {
  second++;
} else {
  second = 0;
  if ( minute < 60 ) {
    minute++;
  } else {
    // hour
  }
}
Topic archived. No new replies allowed.