C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old September 27th, 2002, 08:05 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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;
}

Reply With Quote
  #2  
Old September 29th, 2002, 12:06 PM
jonsagara's Avatar
jonsagara jonsagara is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: USA
Posts: 286 jonsagara User rank is Private First Class (20 - 50 Reputation Level)jonsagara User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 m 23 sec
Reputation Power: 7
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!"

Reply With Quote
  #3  
Old October 1st, 2002, 07:13 PM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #4  
Old October 1st, 2002, 07:38 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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!

Reply With Quote
  #5  
Old October 3rd, 2002, 11:24 AM
Rdesign Rdesign is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Redding
Posts: 49 Rdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem adding time by breaking hours to seconds...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway