Reputation: 39
one of the problems I'm working on requires an input of two time values (being in hours, minutes and seconds)in a 24h00 format. I have already declared my variables, input and output statements with regards to my main program. I'm just having trouble with my void statement CalcDiff to calculate the difference of the two time inputs. All values have been declared of type int. Should inputs(eg. sec and sec2) be compared first to see which is greater before calculating the difference? I'm assuming the order of variables would be important too(calculating the difference of the hours before the minutes, before the seconds)? I'm new to C++ so my apologies if I'm missing anything obvious.
// Computes time difference of two time periods in 24h00 format
// Time periods are input
#include <iostream>
using namespace std;
int seconds,seconds2, minutes, minutes2, hours, hours2;
void CalcDiff(int seconds, int seconds2 int minutes, int minutes2, int
hours, int hours2);
int main()
{
int sec, sec2, min, min2, hr, hr2, diff;
cout << "Enter the first time." << endl;
cout << "Enter hours, minutes and seconds: ";
cin >> hr >> min >> sec;
cout << "Enter the second time." << endl;
cout << "Enter hours, minutes and seconds: ";
cin >> hr2 >> min2 >> sec2;
CalcDiff(int sec, int sec2, int min, int min2, int hr, int hr2,
diff);
cout << endl << "Difference in times: " << hr << ":" << min << ":"
<< sec;
cout << " - " << hr2 << ":" << min2 << ":" << sec2;
return 0;
}
void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int
hour, int hour2, diff)
Upvotes: 3
Views: 9701
Reputation: 371
I had the same problem and my solution was this code, my perspective is to get both times in seconds and then subtract them to know the difference. time calculations are made inside the IF statement. the rest of the code is to print the results, I added many variables to try to make it more explicit and understandable.
#include <iostream>
using namespace std;
int main()
{
int h1,h2,m1,m2,s1,s2;
long TotalSeconds = 0;
cout << "Enter the first time." << endl;
cout << "Enter hours, minutes and seconds: ";
cin >> h1 >> m1 >> s1;
cout << "Enter the second time." << endl;
cout << "Enter hours, minutes and seconds: ";
cin >> h2 >> m2 >> s2;
// Difference Between Times IN Seconds
// this code must be in your function
if( h1 > h2 ){
TotalSeconds += 86400 - ((h1*3600)+(m1*60)+(s1));
TotalSeconds += ((h2*3600)+(m2*60)+(s2));
}else{
// Time 2 - Time 1
TotalSeconds = ((h2*3600)+(m2*60)+(s2)) - ((h1*3600)+(m1*60)+(s1));
}
cout << "Total Seconds: " << TotalSeconds << endl;
cout << "Time Difference :\t";
long hours, minutes, seconds;
hours = TotalSeconds / 3600;
cout << hours << ":";
TotalSeconds -= hours*3600;
minutes = TotalSeconds / 60;
cout << minutes << ":";
TotalSeconds -= minutes*60;
seconds = TotalSeconds;
cout << seconds << endl;
return 0;
}
Another Example using C' Style, in this example you must input time string like this 01:23:11
#include <stdio.h>
#include <stdlib.h>
int main()
{
int h1,h2,m1,m2,s1,s2;
long segundos = 0;
// Hora Inicial
scanf("%i : %i : %i",&h1,&m1,&s1);
// Hora Final
scanf("%i : %i : %i",&h2,&m2,&s2);
// HORAS
if( h1 > h2 ){
segundos += 86400 - ((h1*3600)+(m1*60)+(s1));
segundos += ((h2*3600)+(m2*60)+(s2));
}else{
// Tiempo 2 - Tiempo 1
segundos = ((h2*3600)+(m2*60)+(s2)) - ((h1*3600)+(m1*60)+(s1));
}
printf("%ld",segundos);
return 0;
}
Upvotes: 1
Reputation: 39
I decided to go with converting the minutes and hours to seconds and work the difference for the time periods through a void function I called CalcDiff. I used three referenced variables, FinSec, FinMin and FinHr to store the differences after converting the new DiffSec to the new values. My program for the void statement is:
void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int
hour, int hour2, int& FinSec, int& FinMin, int& FinHr)
{
int TotalSec1, TotalSec2, DiffSec, tempMin;
TotalSec1= (hour*3600)+(minutes*60)+seconds;
TotalSec2= (hour2*3600)+(minutes2*60)+seconds2;
if(TotalSec1>TotalSec2)
DiffSec = TotalSec1 - TotalSec2;
else
DiffSec = TotalSec2 - TotalSec1;
FinSec = DiffSec%60;
tempMin = DiffSec/60;
FinMin = tempMin%60;
FinHr = FinMin/60;
}
Upvotes: 0
Reputation: 219345
Use <chrono>
plus an existing library.
#include "date.h"
#include <iostream>
int
main()
{
std::chrono::seconds t1, t2;
std::cout << "Enter the first time [h]h:mm:ss: ";
std::cin >> date::parse("%T", t1);
if (std::cin.fail())
return 1;
std::cout << "Enter the second time [h]h:mm:ss: ";
std::cin >> date::parse(" %T", t2);
if (std::cin.fail())
return 1;
std::cout << date::format("Difference in times: %T\n", t1 - t2);
}
The above library is free, open-source, and being proposed for standardization.
Upvotes: 1
Reputation: 457
first of all the syntax of function call should be
CalcDiff( sec, sec2, min, min2, hr, hr2);
instead of
CalcDiff(int sec, int sec2, int min, int min2, int hr, int hr2,
diff);
in the main section
function definition the code should be
void CalcDiff(int seconds, int seconds2, int minutes, int minutes2, int
hour, int hour2, diff)
{
\\ write the code for subtraction here
}
Upvotes: -1
Reputation: 1420
Try using std::chrono
void calcDiff(int h1, int m1, int s1, int h2, int m2, int s2){
std::chrono::seconds d = std::chrono::hours(h2-h1)
+ std::chrono::minutes(m2-m1)
+ std::chrono::seconds(s2-s1);
std::cout << std::chrono::duration_cast<std::chrono::hours>(d).count() << "h" <<
std::chrono::duration_cast<std::chrono::minutes>(d % std::chrono::hours(1)).count() << "m" <<
std::chrono::duration_cast<std::chrono::seconds>(d % std::chrono::minutes(1)).count() << "s" << std::endl;
}
int main(){
calcDiff(13, 30, 45, 18, 40, 20); // 5h9m35s
calcDiff(20, 30, 45, 18, 40, 20); // -1h-50m-25s
return 0;
}
The negative result might be a little wierd, but i am sure you can make it work whatever way you want.
Upvotes: 0
Reputation: 3902
Aside from there already existing classes to handle this, here is a solution idea with explanation.
First of all, sec, sec2, min, min2
... this is a list of different variables. If you have such a long list, this is a sign that something is amiss. This is C++, so use OOP. That is, use classes.
One header for such a class could be
class Time {
private:
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
public:
Time(const unsigned char _seconds, const unsigned char _minutes,
const unsigned char __hours);
Time(const unsigned int _seconds);
Time difference(const Time& other) const;
unsigned int to_total_seconds() const;
}
This is a far cleaner approach - you don't need all the code right where you use it. Implementations:
Time Time::difference(const Time& other) const {
int difference_seconds = (int) this.to_total_seconds() - (int) other.to_total_seconds();
return Time((unsigned int) std::abs(difference_seconds));
}
unsigned int Time::to_total_seconds() const{
return seconds + 60.*minutes + 60*60*hours;
}
Time::Time(const unsigned int _seconds){
seconds = _seconds % (60*60);
minutes = (_seconds / 60) % 60;
hours = _seconds / (60*60);
}
Time::Time(const unsigned char _seconds, const unsigned char _minutes,
const unsigned char __hours) :
seconds(_seconds), minutes(_minutes), hours(_hours) {
assert(seconds < 60);
assert(minutes < 60);
}
Another approach would be to directly store the total seconds.
I mean, you could do things like doing actual subtractions, like doing difference 6:12 to 4:50 by subtracting 50 from 12 in minutes, resulting in 38 with a remainder, then 6 minus (4 + remainder) = 1 -> 1:38 difference. But why do that when you can simply subtract the seconds and take their absolute value?
But more importantly, keep your main clean. Large procedural code is a clear sign that a class is missing.
(Of course you'll have to add something to the code that gets the values out, preferably a printer. Since there are possibilities for inconsistence, public members are not recommended here.)
Upvotes: 0