Need help with a clock related code

Hello there good people of the cpluspluss forums.
I am currently writing code for a program that is supposed to take two different digital clock timestamps (hh mm ss), compare them and find out which is the earlies one/latest one, then calulate the difference between them.

I am really new to this kind of stuff, and i have encountered many snags so far.
Can someone take a look at what i have so far? i would really appreciate any feedback/help.

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
#include <iostream>
#include <string>

struct timestamp {
    int hour, min, sec;
};


// overload handles the prepend-0 issue
std::ostream& operator<<( std::ostream& stream, const timestamp& ts ) {
    return stream 
        << (ts.hour < 10 ? "0" : "") << ts.hour << ":"
        << (ts.min < 10 ? "0" : "") << ts.min << ":"
        << (ts.sec < 10 ? "0" : "") << ts.sec;
}

timestamp modify( const timestamp& ts, const std::string& name ) {
    std::cout << name << "'s starting value is: " << ts << "\nWould you like to modify it? [y/n] ";
    std::cout.flush();

    char response;
    std::cin >> response;

    // If no modification; return original
    if( !( response == 'y' || response == 'Y' ) )
        return ts;

    int hour, min, sec;

    hour = min = sec = -1;
    while( hour < 0 || hour > 23 ) {
        std::cout << "Please enter hour [0-23]: ";
        std::cout.flush();
        std::cin >> hour;
    }

    while( min < 0 || min > 59 ) {
        std::cout << "Please enter minutes [0-59]: ";
        std::cout.flush();
        std::cin >> min;
    }

    while( sec < 0 || sec > 59 ) {
        std::cout << "Please enter seconds [0-59]: ";
        std::cout.flush();
        std::cin >> sec;
    }

    return timestamp { hour, min, sec };
}

int main() {
	timestamp t1 { 20, 30, 45 };
    timestamp t2 { 19, 25, 55 };

    t1 = modify( t1, "Time1" );
    t2 = modify( t2, "Time2" );

    std::cout << "Modified:\n" << t1 << "\n" << t2 << std::endl;
}
Let's start somewhere. Can name something specific you want help with?
The problem is that it won't compile. line 49 gives me an error (typename not allowed) for the struct. Also, there is an error for the ({) in line 53 and 54. I am wondering if this is my own doing or my compiler. I am using MS VS.
Thanks for the reply!
It's probably because MSVC doesn't support certain C++11 features that you're using - it compiles fine:
http://ideone.com/tQk28z
Thanks.
Now i need to make it find out which timestamp that is earlier and later or if they are the same, and calculate the time in between them. Does anyone have some helpful tips on this? my head is kinda blank, and its getting late.
Ok, so i tried to figure it out, but it didn't go so well. Read the comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	//continunes from my first post. this is part of the int main
    struct diff
    std::cout << "Modified:\n" << t1 << "\n" << t2 << std::endl;
	{
		if (t1>t2) {              // This doesn't work at all
     diff = (t1-t2);              // How?
} else if (t2>t1) {
     diff = (t2-t1);
} else if (t1==t2) {
    diff = 0;

    std::cout << "Do you like to modify again? ";  /*Here it is supposed to be able to
	                                                      "restart" from line 18"*/

  } while( response2 != 'N'  &&  response2 != 'n'); //if no, terminate
  return 0;
}

Last edited on
After line 12, you need to use cin to ask the user, otherwise how will response2 be updated for the while loop condition?

As for lines 5 to 10, look at your indentation style - because of this, you forgot the closing brace on line 11. Also it could all be replaced by diff = std::abs(t2-t1).
Topic archived. No new replies allowed.