Help with code

Can anyone help me with this program I am kind a stuck, I have no idea what I have to do or where to start with because my tutor did not show me how to do this due to covid I can not come to school, but he want me to finish this.
This is the topic
Write a program that reads two times (each time in hours, minutes and seconds using a 24 hour format and using a colon as a delimiter (i.e. read as a C-style string), eg 13:44:33 ) where the second time is later than the first time. Calculate the elapsed time between the two times, giving this in hours, minutes and seconds.
Thankyou very much
Last edited on
We're not going to do your homework for you.

What have you done so far?
Show your code with code tags.
Where are you stuck?
Can you work out the problem using pencil and paper?

If you were given this problem, you should already know how to read data from a file.
You can read each time as a string and then break it apart, or you can input each time field by field. Your choice.
Convert the times to a common format, such as seconds elapsed since midnight.
Subtract the earlier time from the later time.
That gives you the elapsed seconds between the two times.
Convert the elapsed seconds to something suitable for display.
Display the difference.
What's so hard?
Last edited on
Here is a start but it needs to be expanded to get the second time and then, use the two sets of values to do the subtraction. Look up the functions if you aren't clear on what they are doing. There is a reference section on this site. Doing that calculation on paper first will help, especially where 'borrow/carryovers' are required.

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

int main()
{
    char start[9];
    char finish[9];
    
    std::cout << "Enter start time: ";
    std::cin.get(start,9);
    
    std::cin.get(finish,9);
    
    int t_start[3]{0};
    
    char* pch;
    pch = strtok (start,":");
    
    int count{0};
    while (pch != NULL)
    {
        t_start[count] = atoi(pch);
        pch = strtok (NULL, ":");
        count++;
    }
    
    // DISPLAY HOURS, MINUTES, SECONDS
    for(int i = 0; i < 3; i++)
        std::cout << t_start[i] << ' ';
    
    return 0;
}


Enter start time: 13:24:34
13 24 34 Program ended with exit code: 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
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

auto getTime(const std::string& prm) {
	const auto tt {time(nullptr)};
	auto tms {std::gmtime(&tt)};
	std::istringstream ist;

	do {
		std::string tim;

		std::cout << prm;
		std::getline(std::cin, tim);
		ist.clear();
		ist.str(tim);
	} while (!(ist >> std::get_time(tms, "%H:%M:%S")) && (std::cout << "Invalid time\n"));

	return mktime(tms);
}

int main()
{
	const auto ts {getTime("Enter start time (hh:m:ss): ")};
	const auto tf {getTime("Enter end time (hh:m:ss): ")};

	if (tf < ts)
		return (std::cout << "Start time after end time\n"), 1;

	const auto diff {tf - ts};

	std::cout << "The time difference is " << std::put_time(gmtime(&diff), "%H:%M:%S") << '\n';
}



Enter start time (hh:m:ss): 13:34:56
Enter end time (hh:m:ss): 15:34:23
The time difference is 01:59:27

An alternative way to handle the times after they are parsed is to convert to seconds, do the calculation, then convert the answer to hh:mm:ss
I can tell you that now to find experts in lead generation is very difficult for the site, the code is important of course, but also without leads that there is nothing to do. That is why I recommend everyone to look and read in detail the information about https://wow24-7.io/lead-generation , I highly recommend reading it and there will be no problems. I hope I was able to help. Good luck!
Topic archived. No new replies allowed.