Jump to content

[C++]Simple program problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
<C>

<C>

    Newbie

  • Members
  • Pip
  • 1 posts
Just started programming in C++, using Dev IDE. Been working a lot with data types, variables, constants and type converting the last 4 weeks and now I'm trying out some exercises.

This is the code I have so far:


#include<iostream>

using namespace std;

int main()

{

    const int min_per_hour = 60;

    const int sec_per_min = 60;

    const int hours_per_day = 24;

    cout << "Input seconds: ";

    long int seconds;

    cin >> seconds;

    cout << seconds << " seconds = " << (seconds/sec_per_min)/(hours_per_day*60) <<

    " days, " <<  << " minutes, " << <<

    " seconds." << endl;

    cin.get();

    cin.get();

    return 0;

}

    


When you read the code you probably noticed a empty space between two operators <<. Between those I want the rest of the "days" field. I want the rest of "days" converted to "minutes" and the rest of the minutes converted to "seconds" as you probably can see. This is probably easy to solve but for some reason I cant manage to do it. I tried using the modulus operator % like this:

(seconds/sec_per_min)%(hours_per_day*60)

To get the rest out of it.

This is an example of output I have followed:

Please enter seconds: 31600000
31600000 seconds = 365 days, 46 minutes, 40 seconds

With my code I get out the 365 days, but the minutes(using modulus operator) is 1066 minutes.

And another thing, please don't discuss better structure in my program, like if you think there are a better way than using constants. The exercise wanted me to use constants and structure my program like this.

Im thankfull for all help I can get.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What I would recommend:
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
minutes = minutes % 60;
days = hours / 24;
hours = hours % 24;
Then you can output the individual variables.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users