Loop Works; Output Not Perfect, Polish Needed

My loop is funked here.
The output for the first day is perfect.
However - If you see my input / output below - the other days are junky.
I'm kinda stuck.
Any ideas?

***//INPUT//***
Give Me A Start Date
12/01/17
What Is The Start Time?:
01:00:00
What Interval Would You Like Your Time Jump In?:
01:00:00
What's Your End Time?:
18:00:00
***//OUTPUT//***
12/2/17,2:0:0
12/2/17,3:0:0
12/2/17,4:0:0
12/2/17,5:0:0
12/2/17,6:0:0
12/2/17,7:0:0
12/2/17,8:0:0
12/2/17,9:0:0
12/2/17,10:0:0
12/2/17,11:0:0
12/2/17,12:0:0
12/2/17,13:0:0
12/2/17,14:0:0
12/2/17,15:0:0
12/2/17,16:0:0
12/2/17,17:0:0
12/2/17,18:0:0
12/2/17,19:0:0
12/3/17,20:0:0
12/4/17,21:0:0
12/5/17,22:0:0
12/6/17,23:0:0
12/7/17,0:0:0
12/8/17,1:0:0
12/9/17,2:0:0
12/10/17,3:0:0
12/11/17,4:0:0
12/12/17,5:0:0
12/13/17,6:0:0
12/14/17,7:0:0
12/15/17,8:0:0
12/16/17,9:0:0
12/17/17,10:0:0
12/18/17,11:0:0
12/19/17,12:0:0
12/20/17,13:0:0
12/21/17,14:0:0
12/22/17,15:0:0
12/23/17,16:0:0
12/24/17,17:0:0
12/25/17,18:0:0
12/26/17,19:0:0
12/27/17,20:0:0
12/28/17,21:0:0
12/29/17,22:0:0
12/30/17,23:0:0
12/31/17,0:0:0

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
64
65
66
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
int start_date_month;
int start_date_day;
int start_date_year;
int inc_day;
int inc_mon;
int inc_year;
int n;
int hh;
int mm;
int ss;
int inc_hh;
int inc_mm;
int inc_ss;
int end_hh;
int end_mm;
int end_ss;
int str_hh;
int str_mm;
int str_ss;
unsigned int facetime;
unsigned int hola0;
unsigned int hola1;
unsigned int hola2;
cout << "Give Me A Start Date" << "\n";
char sep;
cin >> start_date_month >> sep >> start_date_day >> sep >> start_date_year;
cout << "What Is The Start Time?:" << "\n";
cin >> str_hh >> sep >> str_mm >> sep >> str_ss;
hola0 = str_hh * 3600 + str_mm * 60 + str_ss;
cout << "What Interval Would You Like Your Time Jump In?:\n";
cin >> inc_hh >> sep >> inc_mm >> sep >> inc_ss;
hola1 = inc_hh * 3600 + inc_mm * 60 + inc_ss;
cout << "What's Your End Time?:\n";
cin >> end_hh >> sep >> end_mm >> sep >> end_ss;
hola2 = end_hh * 3600 + end_mm * 60 + end_ss;
if (start_date_month==1) {(n=31);}//jan
if (start_date_month==2){(n=28);}//feb
if (start_date_month==3){(n=31);}//mar
if (start_date_month==4) {(n=30);}//apr
if (start_date_month==5){(n=31);}//may
if (start_date_month==6){(n=30);}//jun
if (start_date_month==7) {(n=31);}//jul
if (start_date_month==8){(n=31);}//aug
if (start_date_month==9){(n=30);}//sep
if (start_date_month==10) {(n=31);}//oct
if (start_date_month==11){(n=30);}//nov
if (start_date_month==12){(n=31);}//dec
do {start_date_day++;
do {	facetime = (hola0 += hola1);
	hh = (facetime / 3600) % 24;
	mm = (facetime / 60) % 60;
	ss = facetime % 60;
	cout << start_date_month << "/" << start_date_day << "/" << start_date_year << "," << hh << ":" << mm << ":" << ss << "\n";
	}
	while (hola2 >= hola0);
}
	while (start_date_day<n);
}
borrowing from Josuttis' (C++ Standard Library, 2nd ed) section 5.7 (Clocks and Timers) - (if you really want to understand this program I suggest you try read this section carefully and search for anything that's unfamiliar):
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
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>

//http://cppstdlib.com/code/util/timepoint.hpp.html
// convert timepoint of system clock to calendar time string
inline
std::string asString (const std::chrono::system_clock::time_point& tp)
{
    // convert to system time:
    std::time_t t = std::chrono::system_clock::to_time_t(tp);
    std::string ts = ctime(&t);    // convert to calendar time
    ts.resize(ts.size()-1);        // skip trailing newline
    return ts;
}

// convert calendar time to timepoint of system clock
inline
std::chrono::system_clock::time_point
makeTimePoint (int year, int mon, int day,
               int hour, int min, int sec=0)
{
    struct std::tm t;
    t.tm_sec = sec;        // second of minute (0 .. 59 and 60 for leap seconds)
    t.tm_min = min;        // minute of hour (0 .. 59)
    t.tm_hour = hour;      // hour of day (0 .. 23)
    t.tm_mday = day;       // day of month (1 .. 31)
    t.tm_mon = mon-1;      // month of year (0 .. 11)
    t.tm_year = year-1900; // year since 1900
    t.tm_isdst = -1;       // determine whether daylight saving time
    std::time_t tt = std::mktime(&t);
    if (tt == -1) {
        throw "no valid system time";
    }
    return std::chrono::system_clock::from_time_t(tt);
}

int main()
{
    using Days = std::chrono::duration<int, std::ratio<3600*24>>;
    // define type for durations that represent day(s): 

    auto start_tp = makeTimePoint(2012, 5, 4, 7, 42);//2012 4th May 7:42
    auto end_tp = makeTimePoint (2015, 11, 19, 23, 6);//2015 19th Nov 23:06

    while (start_tp < end_tp)
    {
        start_tp += Days(3) + std::chrono::hours (13) + std::chrono::minutes(47);
        //every 3 days, 13 hours and 47 minutes
        std::cout << asString(start_tp) << "\n";//prints the incremental timepoints
    }
}
Last edited on
Hello shycas2008,

gunnerfunner has a different approach that will work, but to your original program and problem "hours, minutes and seconds" are read into an "int" variable, so if you type "02" it will only store 2 in the variable. The leading 0 is dropped. When you output it will only print 2. If you want to print "02" you will have to break up the cout with something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << start_date_month << "/" << start_date_day << "/" << start_date_year << ",";
if (hh < 10)
    cout << "0" << hh  << ":" ;
else
    cout << hh  << ":" ;

if (mm < 10)
    cout << "0" << mm << ":";
else
   cout<< mm << ":";

if (ss < 10)
cout <<"0" << ss << "\n";
else
    cout << ss << "\n";


Hope that helps,

Andy
Last edited on
Gunner - you just changed the way I will do things.
Thank you so much.
Andy - your description has added additional utility.

Thank you both!!! Much appreciated!
Topic archived. No new replies allowed.