Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting Seconds to Days, Hours, Minutes and Seconds

godkillzyougodkillzyou Posts: 9Member
Ok, so I'm studying with SAMS C++ Primer Plus (Fifth Edition). In chapter 3 "Dealing With Data," programming exercise #4 says:

"Write a program that asks the user to enter the number of seconds as an integer value (use type long) and that then displays the equivalent time in days, hours, minutes and seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute."

This is driving me up a wall. I can't figure out how to do this.

This is what I've got so far...

[color=Blue]// Converting an input of seconds to
// Days, Hours, Minutes and Seconds.

#include
using namespace std;

long InSeconds;

const int HoursInDay = 24;
const int MinutesInHour = 60;
const int SecondsInMinute = 60;


int main()
{
cout << "This program will turn your seconds into days,"
<< " hours, minutes and seconds." << endl;
cout << "Enter the amount of seconds:__________";
cin >> InSeconds;

cout << "That's " << InSeconds / (SecondsInMinute * MinutesInHour * HoursInDay) << " day(s)..." << endl;

return 0; [/color]

I'm not sure where to go from here. I'm sure that somehow the Modulus "%" has to come into play, but I'm not sure how. After calculating the number of days, that total should be subtracted, and the remainder used to calculate the remaining number of hours, minutes, and seconds, etc.

Any help would be greatly appreciated!!
Everyone is entitled to their own opinion... not their own facts.

Comments

  • ActorActor Posts: 438Member
    [code]
    Seconds = InSeconds % SECONDS_IN_MINUTE ;
    InMinutes = InSeconds / SECONDS_IN_MINUTE ;

    Minutes = InMinutes % MINUTES_IN_HOUR ;
    InHours = InMinutes / MINUTES_IN_HOUR ;

    Hours = InHours % HOURS_IN_DAY ;
    Days = InHours / HOURS_IN_DAY ;
    [/code]
  • godkillzyougodkillzyou Posts: 9Member
    I think I see what you're saying here. I'm just having a tough time implementing it into my code. Basically, I'm confused. I hope it's not too much to ask for an explanation?

    Thanks again!
    Everyone is entitled to their own opinion... not their own facts.
  • ActorActor Posts: 438Member
    [blue]
    "Write a program that asks the user to enter the number of seconds as an integer value (use type long) and that then displays the equivalent time in days, hours, minutes and seconds. Use symbolic constants to represent the number of hours in the day, the number of minutes in an hour, and the number of seconds in a minute."
    [/blue]
    OK. Suppose the number of seconds (InSeconds) is 63. You can immediately see that this is 1 minute, 3 seconds. How do you know? Because if you divide 63 by 60 (number of seconds in a minute) you get 1 with a remainder of 3, i.e., 1 minute 3 seconds. For the computer to get these two values two operations (and two statements) are required. First, the [b]%[/b] operator (a.k.a., the modulus or remaindering operator) gives the number of seconds.
    [code]Seconds = InSeconds % 60 ;[/code]
    Second, the [b]/[/b] operator gives the number of seconds, throwing away the remainder.
    [code]Minutes = InSeconds / 60 ;[/code]
    NOTE, in order for the remainder to be thrown away both operands of the [b]/[/b] must be [b]int[/b] or some other of variety of ordinal, i.e., no floats or doubles.

    Repeat the process to get hours and days.
  • godkillzyougodkillzyou Posts: 9Member
    Ok. I've got what you're saying. The trouble I'm having now is going from minutes to hours.

    For example, the output of my program will say...

    "That's 1 hour, 60 minutes and 1 second" when dealing with 3601 seconds.

    Somehow I've got to use the modulus operator to eliminate the "60" minutes when it's already been accounted for in the "hours" section.

    I've been trying to do this for a while, but the only thing I've been able to come up with are elaborate uses of functions, drawn out calculations, etc.

    This exercise is in the beginning of the book, assuming no knowledge of C++ programming. I must be thinking about this too hard. It's probably something simple I'm overlooking.

    [code]// Converting an input of seconds to
    // Days, Hours, Minutes and Seconds.

    #include
    using namespace std;

    long InSeconds;

    const int HoursInDay = 24;
    const int MinutesInHour = 60;
    const int SecondsInMinute = 60;

    int seconds;
    int minutes;
    int hours;
    int days;


    int main()
    {
    cout << "This program will turn your seconds into days,"
    << " hours, minutes and seconds." << endl;
    cout << "Enter the amount of seconds:__________";
    cin >> InSeconds;

    days = InSeconds / (SecondsInMinute * MinutesInHour * HoursInDay);
    hours = InSeconds / (SecondsInMinute * MinutesInHour);
    minutes = InSeconds / SecondsInMinute;
    seconds = InSeconds % SecondsInMinute;

    if (hours > 24)
    {
    goto RecalcHours;
    }

    if (minutes > 60)
    {
    goto SkipMinutes;
    }

    cout << "That's " << hours << " hours..." << endl;

    SkipHours:
    cout << "That's " << minutes << " minute(s)..." << endl;

    SkipMinutes:
    cout << "And " << seconds << " second(s)." << endl;

    return 0;
    }[/code]

    That's what I've got so far. The "goto" calls haven't been defined yet, so this program is not compiler-ready. It's just an example of my thought process... where I'm headed. Probably in the completely wrong direction.

    I've already finished the other programming exercises with ease, and am on to subsequent chapters, but this program is killing me. I honestly feel like I'm probably making this more difficult than it really is.
    Everyone is entitled to their own opinion... not their own facts.
  • ActorActor Posts: 438Member
    [code]
    // Converting an input of seconds to
    // Days, Hours, Minutes and Seconds.

    #include
    using namespace std;

    long InSeconds; [red]// why is this external?[/red]

    const int [RED]HOURS_IN_DAY[/RED] = 24; [red]// it's good form to make[/red]
    const int [RED]MINUTES_IN_HOUR[/RED] = 60; [red]// symbolic constants all caps[/red]
    const int [RED]SECONDS_IN_MINUTE[/RED] = 60;

    int seconds; [red]// why are all these external?[/red]
    int minutes;
    int hours;
    int days;


    int main()
    {
    [red]long InMinutes, InHours ;[/red]

    cout << "This program will turn your seconds into days,"
    << " hours, minutes and seconds." << endl;
    cout << "Enter the amount of seconds:__________";
    cin >> InSeconds;
    [red]/*
    Compute seconds, minutes, hours, days in that order.
    Trying to do it in reverse order is causing your problem
    */[/red]

    [red]// compute seconds[/red]
    seconds = InSeconds % SECONDS_IN_MINUTE ;

    [red]// throw away seconds used in previous statement and convert to minutes[/red]
    InMinutes = InSeconds / SECONDS_IN_MINUTE ;

    [red]// compute minutes[/red]
    minutes = InMinutes % MINUTES_IN_HOUR ;

    [red]// throw away minutes used in previous statement and convert to hours[/red]
    InHours = InMinutes / MINUTES_IN_HOUR ;

    [red]// compute hours[/red]
    hours = InHours % HOURS_IN_DAY ;

    [red]// throw away hours used in previous statement and convert to days[/red]
    days = InHours / HOURS_IN_DAY ;

    [red]
    cout << "That's " ;
    if (days > 0)
    cout << days << " days " ;
    if (hours > 0)
    cout << hours << " hours " ;
    if (minutes > 0)
    cout << minutes << " minutes " ;
    if (seconds >0)
    cout << seconds << " seconds " ;
    cout << endl ;
    [/red]

    return 0;
    }
    [/code]


  • godkillzyougodkillzyou Posts: 9Member
    Wow, that makes complete sense now...

    It had not even entered my mind to solve this problem in reverse order from what I had been. I got so wrapped up in the calculations, functions and whatnot, that I hadn't even considered that alternative. I should have stopped and tried to grasp an objective look at the project. To rethink what I was doing.

    I suppose this is a good lesson to learn early on in my programming "career."

    What I was wondering... what is the significance of my variables being "external" versus being inside the main function? I hadn't really considered this before, and was wondering what the implications were of making these variables external vs. internal? Is it a memory issue?

    Again, I want to thank you for the help you've given me. It is greatly appreciated.
    Everyone is entitled to their own opinion... not their own facts.
  • LundinLundin Posts: 3,711Member
    : What I was wondering... what is the significance of my variables
    : being "external" versus being inside the main function? I hadn't
    : really considered this before, and was wondering what the
    : implications were of making these variables external vs. internal?
    : Is it a memory issue?


    They aren't external, external refers to variables declared in a different file. They are "global", or "declared at file scoop". Global variables are considered bad programming for the following reasons:

    1) They become available to the whole program, making it possible to accidently use them. Or worse: intently use them from elsewhere in the code because they are accessible. The latter leads to completely unreadable/unmaintainable code.

    2) They exist in RAM for the whole duration of the program, instead of existing on the stack for a short period of time. That is, they take up memory when they don't have to.

    3) They are implicitly initialized to zero at program startup. This steals execution time on computers that runs the code from non-volatile memory.

    There is actually -never- a reason to use global (file scoop) variables in a C program, so don't make it a habit.

    (And before people start replying with various cases where they think global variables are needed... variables declared as "static" aren't considered global, using static is fine.)
  • ActorActor Posts: 438Member
    [blue]
    : : What I was wondering... what is the significance of my variables
    : : being "external" versus being inside the main function? I hadn't
    : : really considered this before, and was wondering what the
    : : implications were of making these variables external vs. internal?
    : : Is it a memory issue?
    :
    :
    : They aren't external, external refers to variables declared in a
    : different file.
    [/blue]
    I'm actually more comfortable with "global" than with "external."

    I'm afraid I've been mislead by another "bad C book." This one says "an external variable is any variable declared outside a function," which seems to fit in his case. I think the book in question actually gives both definitions, which does not help my schizophrenia one bit. :-)

  • LundinLundin Posts: 3,711Member
Sign In or Register to comment.