|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Problem adding time by breaking hours to seconds...
I have a problem for school: I am to enter two times IE: 12:59:59, then I must break the hours and minuts to seconds add everything together and reconstitute everything back to the original format -- 12:59:59. Everything works great until I put large values in IE: 25:67:82. I was thinking that my prog would work fine, I even tried unsigned int (this at least stopped my prog from returning negitive values). What might I try next?
Please help! Thanks to anyone that can. Here's my current source: # include "iostream.h" /* * * C++ Programming * Problem 11 Chapter 4 * by Quinn Peters * * Time adding problem * */ int convert (unsigned int h, unsigned int m, unsigned int s) { unsigned int hours = h*3600; unsigned int minuts = m*60; unsigned int seconds = s+minuts+hours; return (seconds); }; // I MUST use a structure here. That's the chapter were in. struct time { unsigned int hours; unsigned int minuts; unsigned int seconds; }; int main() { unsigned int sec1 = 0; unsigned int sec2 = 0; unsigned int tsec = 0; unsigned int fhours = 0; unsigned int fminuts = 0; unsigned int fseconds = 0; time time1 = {0,0,0}; time time2 = {0,0,0}; char voidchar; cout << "Enter the first time: "; cin >> time1.hours >> voidchar >> time1.minuts >> voidchar >> time1.seconds; cout << '\n' << "Enter second time: "; cin >> time2.hours >> voidchar >> time2.minuts >> voidchar >> time2.seconds; sec1 = convert (time1.hours, time1.minuts, time1.seconds); sec2 = convert (time2.hours, time2.minuts, time2.seconds); tsec = sec1+sec2; if (tsec>=3600) { while (tsec>=3600) { tsec = tsec-3600; fhours++; } } if (tsec>=60) { while (tsec>=60) { tsec = tsec-60; fminuts++; } } fseconds = tsec; cout << "Your final added time is: " << fhours << " hours, " << fminuts << " minuts, " << fseconds << " seconds." << '\n'; return 0; } |
|
#2
|
||||
|
||||
|
What platform are you running this on? Can you give the two times you used that cause the program to break?
__________________
Jon Sagara "Me fail English? That's unpossible!" |
|
#3
|
|||
|
|||
|
Platform is win32
All on Win 2000 (Server(sp3) && Professional(sp3))
I wrote this program with int instead of unsigned int and broke it adding: 26:35:41 with 15:47:10 this returned a negitive integer - Somthing like: -2 hours -285 Minuts and -48 seconds. I also broke it with: unsigned int and the same times except this time it returned: 0 hours, 0 minuts and 255 seconds. BTW my compiler is atiquated but it's the one I have to use because that's the one my instructor uses. turbo C++ v. 3.0 Thanks for responding! Quinn |
|
#4
|
||||
|
||||
|
Ah yes, you're probably using the 16 bit DOS compiler there. Try changing the types to unsigned long instead. IIRC, most 16-bit borland compilers would take unsigned long as 32 bits. Also your function convert is returning int instead of unsigned int. You might want to change the return type to unsigned long anyways. If you want to be portable, you could use some preprocessor ad-hackery and define your own type which adjusts depending on your compiler/environment. Something like this ought to do the trick:
Code:
#include <limits.h>
#if (UINT_MAX < 4294967295U)
typedef unsigned long UINT;
#else
typedef unsigned int UINT;
#endif
#if (ULONG_MAX < 4294967295U)
#error "Cannot compile as I can't find a data type long enough!"
#endif
...
UINT convert(UINT h, UINT m, UINT s) {
UINT hours = h*3600;
UINT minuts = m*60;
UINT seconds = s+minuts+hours;
return (seconds);
}
The second #if will cause the compiler to abort if it can't find a data type long enough. This way, your code should be relatively portable. All you have to do is declare all the variables that you anticipate to have large values, as your own data type (UINT). Hope this helps! |
|
#5
|
|||
|
|||
|
fixed it!
Beautiful! That worked like a charm. My whole class had the same problem and it was unsigned long that fixed it. You just kept 25 good people from pulling all their hair out. Thanks for real!
Quinn. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Problem adding time by breaking hours to seconds... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|