• Welcome to Valhalla Legends Archive.
 

First Project - Simple Time Converter

Started by Coltz, January 17, 2006, 11:17 AM

Previous topic - Next topic

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.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

shout


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? ;)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

shout

Quote from: Kp on January 17, 2006, 08:33 PM
Or were you somehow trying to coerce him into using math to solve a mathemtical problem? ;)

Why would I do something like that!?

rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

AntiVirus

#6
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. :)
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

MyndFyre

Quote from: Brandon on January 18, 2006, 06:33 PM
Coltz, why are you worring about decimal places when you are using int?  If you are going to use decimal places, use float.
Maybe the project's specifications say to use decimal places, which would mean that using int is an incorrect plan.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

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 from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

AntiVirus

Quote from: MyndFyre on January 18, 2006, 07:29 PM
Quote from: Brandon on January 18, 2006, 06:33 PM
Coltz, why are you worring about decimal places when you are using int?  If you are going to use decimal places, use float.
Maybe the project's specifications say to use decimal places, which would mean that using int is an incorrect plan.
That was my point. :D
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

K

Quote from: Joe on January 18, 2006, 09:32 PM
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.

That's going to give you some really wrong answers.

Eric

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.

Explicit

Quote from: Lord[nK] on January 18, 2006, 10:32 PM
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.

Is that just talk as a result of your online courses?  :P
I'm awake in the infinite cold.

[13:41:45]<@Fapiko> Why is TehUser asking for wang pictures?
[13:42:03]<@TehUser> I wasn't asking for wang pictures, I was looking at them.
[13:47:40]<@TehUser> Mine's fairly short.

AntiVirus

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.
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

rabbit

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;
}

Shoddy code.  Probably has a bug somewhere.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.