Valhalla Legends Forums Archive | C/C++ Programming | First Project - Simple Time Converter

AuthorMessageTime
Coltz
Ok so just a quick question, need a little help on my first project for CSE 231.  Our project is take take input from the use in terms of seconds, and convert that time into minutes, hours, and days.  Seems easy enough for me at first - Use an int variable for seconds to make sure the user doesn't do anything stupid like 1.1 to mess up any conversions, convert that and then use simple division in order to output to the user the minutes, hours, and days.  The problem is one of the requirements for the project is that each output is restricted with 3 decimal points.  As in instead of displaying 3509039430 seconds = .0000329 days ( Yes I know this is not correct, just an example) I'm required to output to the user 0.000 days.  Or 3023902 seconds = 0.012 hours (again not correct, but you get the idea).  I'm brand new to C++... My instructor specified that all the information we would need is included in the first chapter of our text book, yet I am unable to find ANYTHING pertaining to rounding variables off and the works.  I searched the C++ board already and could not find what I was looking for.  Any help as to how I would go about writing this program would be greatly appreciated.
January 17, 2006, 5:17 PM
Kp
Well, the good news is that there are a great many jobs in which you'll simply never need floating point.  The bad news is this is so obvious that it feels like cheating to tell you.  Remind me next month and I'll explain it.
January 18, 2006, 12:25 AM
shout
* and / are your friends here.
January 18, 2006, 2:07 AM
Kp
I would've put them in the other order, since /* is the opener of a comment and */ is the closer of the comment.  Or were you somehow trying to coerce him into using math to solve a mathemtical problem? ;)
January 18, 2006, 2:33 AM
shout
[quote author=Kp link=topic=13939.msg142258#msg142258 date=1137551627]
Or were you somehow trying to coerce him into using math to solve a mathemtical problem? ;)
[/quote]

Why would I do something like that!?
January 18, 2006, 3:30 AM
rabbit
*, /, %.  Learn to love them.
January 18, 2006, 3:48 AM
Brandon
Coltz, why are you worring about decimal places when you are using int?  If you are going to use decimal places, use float.

And to do this find the equation for converting seconds into mins. and mins. into hours, and hours into days.  Once you know how to do that, your program should be simple enough. :)
January 19, 2006, 12:33 AM
Myndfyr
[quote author=Brandon link=topic=13939.msg142385#msg142385 date=1137630814]
Coltz, why are you worring about decimal places when you are using int?  If you are going to use decimal places, use float.
[/quote]
Maybe the project's specifications say to use decimal places, which would mean that using int is an incorrect plan.
January 19, 2006, 1:29 AM
JoeTheOdd
int seconds;
cin >> seconds;
int minutes, hours, days;
days = seconds / 60*60*24; seconds -= days*60*60*24;
hours = seconds / 60*60; seconds -= hours*60*60;
minutes = seconds / 60; seconds -= minutes * 60;
cout << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << "seconds." << endl;

I love how int is.. integral, not floating point.
January 19, 2006, 3:32 AM
Brandon
[quote author=MyndFyre link=topic=13939.msg142390#msg142390 date=1137634142]
[quote author=Brandon link=topic=13939.msg142385#msg142385 date=1137630814]
Coltz, why are you worring about decimal places when you are using int?  If you are going to use decimal places, use float.
[/quote]
Maybe the project's specifications say to use decimal places, which would mean that using int is an incorrect plan.
[/quote]
That was my point. :D
January 19, 2006, 3:58 AM
K
[quote author=Joe link=topic=13939.msg142406#msg142406 date=1137641522]
int seconds;
cin >> seconds;
int minutes, hours, days;
days = seconds / 60*60*24; seconds -= days*60*60*24;
hours = seconds / 60*60; seconds -= hours*60*60;
minutes = seconds / 60; seconds -= minutes * 60;
cout << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << "seconds." << endl;

I love how int is.. integral, not floating point.
[/quote]

That's going to give you some really wrong answers.
January 19, 2006, 4:17 AM
LoRd
I'm sure you were provided with a textbook or at minimum given instruction on how to complete this task.  You should be able to manage with the materials that have been provided to you.
January 19, 2006, 4:32 AM
Explicit[nK]
[quote author=Lord[nK] link=topic=13939.msg142426#msg142426 date=1137645149]
I'm sure you were provided with a textbook or at minimum given instruction on how to complete this task. You should be able to manage with the materials that have been provided to you.
[/quote]

Is that just talk as a result of your online courses?  :P
January 19, 2006, 5:07 AM
Brandon
Even so, all he needs is the equations on how to convert, the rest should be realivly simple.  If the teacher gave them the project, then they *should* know how to do it.
January 19, 2006, 10:11 PM
rabbit
[code]char *convertSeconds(long time)
{
char *buff;
long data = time;
long days, hours, minutes, seconds;

days = time / 86400;
if(days > 0) time -= days * 86400;

hours = time / 3600;
if(hours > 0) time -= hours * 3600;

minutes = time / 60;
seconds = time % 60;

sprintf(buff, "%i%s, %i%s, %i%s, %i%s",
days, (days != 1) ? "s" : "",
hours, (hours != 1) ? "s" : "",
minutes, (minutes != 1) ? "s" : "",
seconds, (seconds != 1) ? "s" : ""
);

return buff;
}[/code]
Shoddy code.  Probably has a bug somewhere.
January 20, 2006, 1:59 AM

Search