Thread: stuck on this.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    stuck on this.

    Here's what i'm supposed to do.

    Write a program that asks the user to enter a number of seconds.

    There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.

    There are 3,600 seconds in an hour. If the number of seconds entered by the user
    is greater than or equal to 3,600, the program should display the number of hours
    in that many seconds.

    There are 86,400 seconds in a day. If the number of seconds entered by the user is
    greater than or equal to 86,400, the program should display the number of days in that many seconds.

    There are 1666.667 minutes in 100000.000 seconds.
    There are 27.778 hours in 100000.000 seconds.
    There are 1.157 days in 100000.000 seconds.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {    
    double seconds;    
    double minutes = 60.0;    
    double hours = 3600;   
    double days = 86400;    
    
    cout << "Enter a number of seconds: " << endl;    
    cin >> seconds;    
    cout << fixed << showpoint << setprecision(1);   
    
     if (minutes >=60)    
    { cout << "There are minutes in seconds." << setw(1) << minutes << endl;}    
    cout << fixed << showpoint << setprecision(1);    
    
    if (hours >=3600)    { cout << "There are hours in seconds." << setw(1) << hours << endl;}  
    cout << fixed << showpoint << setprecision(1)
     
     
    if (days >=86400)   
    { cout << "There are days in seconds." << setw(1) << days << endl;} 
    return 0; }
    can i do this problem with a if statement because i can't use loops since i didnt learn it yet.

    i don't know if i'm doing it right so please help.
    Last edited by typeqwerty; 09-25-2012 at 06:52 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hint: This assignment will require some math.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by Matticus View Post
    Hint: This assignment will require some math.
    should i use double or int or?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Doubles are fine. As in the example you have 27.778 hours, well, you can take that 0.778 and multiply it into minutes etc...
    Last edited by whiteflags; 09-25-2012 at 08:26 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Okay, let's back up a bit here. Forget about code for a moment.

    If I tell you that, "I've waited 15,000 seconds for you to respond!" ... how many hours is that?

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    is that supposed to be a trick question? 4hours?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    And how did you arrive at that answer? (This is going somewhere, just bear with me.)

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you learnt the MODULUS operator '%' yet?

    It returns the remainder of a division

    ie 126/60 = 2 (minutes) and 126%60 = 6 (seconds)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    15000/60 = 250 then divided it by 60.

    i didn't learn about the modulus yet. the teacher wants me to use the if statement..

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A simple "if()" statement isn't enough for this assignment.

    Code:
    if(true)
        // execute
    if(false)
        // don't execute
    There's nothing in there to solve for given values. You need to do some calculations, and that means math.

    You wrote the math in plain text above. Now just include it in your program.

    Code:
    int result;
    int seconds = 15000;
    
    result = seconds/(60*60);
    Integers will truncate the result in the above calculation, so do as whiteflags says and use doubles.

    Does this make sense?

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    PHP Code:
    [CODE]
    #include <iostream>
    #include <iomanip>
    #include<string>
    #include<cmath>

    using namespace std;
    int main()
    {
    double seconds;
    double minutes seconds /60;
    double hours minutes 60;
    double days hours 24;

    cout <<"Enter a number of seconds: ";
    cin >> seconds
    if (
    seconds >=60 && seconds 3600){minutes seconds/60;cout << "There are " << fixed << setprecision(3) << showpoint << minutes << " minutes in " << setw(1) << seconds << " seconds.\n";}

    if (
    hours >=3600 && minutes 3600){hours minutes60;cout << "There are " << fixed << setprecision(3) << showpoint << minutes << " hours in" << seconds << hours << days << " seconds." << endl;}

    if (
    days >=86400 && days 3600){days hours24;cout << "There are " << fixed << setprecision(3) << showpoint << minutes << " days in" << seconds << "seconds." << endl;}
    cout << endl;}[/CODE
    when I put input 10000 the output would say nothing.

    how do i get the output as .

    Enter a number of seconds:
    There are 166.667 minutes in 10000.000 seconds.
    There are 2.778 hours in 10000.000 seconds.



  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    EDIT: Not sure I got the question right...

    Mine prints out the whole (integer) number of days, hours, minutes and seconds.
    INPUT = 90
    OUTPUT = 0 days, 0 hours, 1 minute and 30 seconds

    Is that right?

    Or do you want something more like;
    INPUT = 90
    OUTPUT = 0 days, 0 hours, and 1.5 minutes

    Code:
    double seconds = 0; //always init variables
    
    //move these lines 
    cout <<"Enter a number of seconds: ";
    cin >> seconds; 
    
    //validate input (ie what happens if the user enters -1000?)
    
    //now these lines will work as 'seconds' has the correct value
    double minutes = seconds /60;
    double hours = minutes / 60;
    double days = hours / 24;


    You need to start with the biggest value first (ie DAYS) and work to the smaller numbers of seconds

    This way it works for any number of seconds.

    Note how we reduce the RemainingSeconds each time so that it holds the number of seconds left after we find the number of days, hours and minutes.
    Also note how we keep the user inputed number of seconds (TotalSeconds) to display at the end of our calculations.

    Code:
    //get user to enter number of seconds
    //collect this number in a variable (lets call it TotalSeconds)
    //create a vaiable to hold the remaining seconds after we take off the days, hours and minutes (lets call it RemainingSeconds)
    
    //RemainingSeconds = TotalSeconds 
    
    //if RemainingSeconds >= seconds in a day (86,400)
         //NumDays is RemainingSeconds/  seconds in a day (86,400)
         //find the remaining seconds (ie minus the seconds in the days from the user entered amount) RemainingSeconds = RemainingSeconds- (NumDays * seconds in a day (86,400) [this is where the MODULUS operator is handy]
    //if RemainingSeconds >= seconds in an Hour (3,600)
         //NumHours is RemainingSeconds /  seconds in a hour 
         //find the remaining seconds RemainingSeconds = RemainingSeconds - (NumHours * seconds in a hour)
    
    //etc for minutes
    
    //display results
    //print 'In [TotalSeconds] seconds there are [NumDays] days, [NumHours] hours, [NumMinutes] minutes and [RemainingSeconds] seconds'
    Last edited by novacain; 09-25-2012 at 11:02 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    i need it to look like this. for example when i input 10000 it should say this as output. E


    Enter a number of seconds:
    There are 166.667 minutes in 10000.000 seconds.
    There are 2.778 hours in 10000.000 seconds.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by typeqwerty View Post
    Enter a number of seconds:
    There are 166.667 minutes in 10000.000 seconds.
    There are 2.778 hours in 10000.000 seconds.
    The assignment gives you all of the factors you need to do that.

    If a input number is larger than some factor, you need to divide. That is basically the whole assignment.

    You don't even need to worry about breaking it down like so 1 billion secs is 31 years, 36 weeks, 6 days, 1 hour, 49 minutes, 26 seconds.

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    but when i run it on a site it where it automatically compile it would say i failed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just stuck indeed
    By TAboy24 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2007, 04:32 PM
  2. Stuck - Need a little help
    By nevek in forum C Programming
    Replies: 9
    Last Post: 06-11-2007, 03:57 PM
  3. A little stuck.. is this possible?
    By Ganoosh in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2005, 03:06 PM
  4. Stuck with '\b'
    By pratip in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 07:49 AM
  5. Stuck again!
    By shewolf in forum C++ Programming
    Replies: 17
    Last Post: 10-23-2003, 12:27 PM