Subtracting time
Jan 6, 2012 at 5:08am UTC
I created a program that helps me keep track of my time at work. I come in at 2:30Pm and leave around 5:00Pm each day. And my code.
cout << day[b] << " " << fixed << showpoint << setprecision(2) << abs (time1[b] - time2[b])<< endl;
shows 2.70 which is correct, but incorrect for time. Is there a way that I can set it to show a half hour? Thanks!
Jan 6, 2012 at 9:22am UTC
Better yet. Is there a way to round to a set number? Like say if it equals .70 round down to .30 and if it equals .60 round up to the nearest whole number?
Jan 6, 2012 at 10:13am UTC
It would not serve you the purpose. Suppose you check in at 2:40 and check out at 5, the time it would show would be 2.60.. By your algorithm, this would be rounded to 3 hrs, which is incorrect. And I can't think of any particular algorithm for your problem as long as you're dealing with time in decimal form.
I would suggest converting the time into minutes, carrying out your calculations, and then reconvert it back into hours... This will never go wrong... Like:
2:30 = 2*60 + 30 = 150(mins)
5:00 = 5*60 = 300(mins)
300-150=150(mins)
time = (150%60)/100 +(150/60) = 2.30
(These are not statements, but just the basic logic to be followed...)
Hope this helps... :)
Jan 12, 2012 at 10:17am UTC
It may help to see my code in order to explain this, and sorry for my late post. But how could I do this within my array? *Don't ridicule my sloppiness, it's in the rough now and I will clean it up later*
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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
string day[6] =
{
"Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" ,
};
float time1 [6];
float time2 [6];
cout << "Enter walk-in and walk-out time" << endl;
cout << "Example 2.00 5.00" << endl;
cout << "\n" ;
for (int i = 0; i < 6; i++)
{
cout << day[i] << " " ;
cin >> time1 [i];
cin >> time2 [i];
}
cout << "\n" ;
for (int a = 0; a < 6; a++)
{
cout << day[a] << " " << fixed << showpoint << setprecision(2) << time1[a] << "--" <<
fixed << showpoint << setprecision(2) << time2[a]<< endl;
cout << "\n" ;
}
cout << "\n" ;
for (int b = 0; b < 6; b++)
{
cout << day[b] << " " << fixed << showpoint << setprecision(2) << abs (time1[b] - time2[b])<< endl; // Division
}
ofstream Time;
Time.open ("Time.txt" );
for (int c = 0; c < 6; c++)
{
Time << day[c] << " " << fixed << showpoint << setprecision(2) << abs (time1[c] - time2[c]) << endl;
}
Time.close();
}
Jan 12, 2012 at 6:25pm UTC
I'm a newbie to C++ but what Caprico is saying seems to make sense.
I assume you are working 2am - 5am in the example?(Since 24hr time would be a hell of a lot easier)
I'm also curious how you would use those calculations on the input before it's stored in the array.
Something like this will round it, but no idea how to store it in an array.(don't laugh, not sure if this syntax is right, but the logic should be)
1 2 3 4
IF ((time1 %= 0.5) >= 0.25 && (time1 %= 0.5) < 0.75)
time1 - ((time1 % 0.5) + 0.5) * 60;
ELSE
time1 - (time1 % 0.5) * 60;
If that doesn't work, it's probably because I've been doing this for a week.
These forums have taught me more in a week than a 5month class ever did.
Topic archived. No new replies allowed.