# stuck on this.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-25-2012
typeqwerty
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.

• 09-25-2012
Matticus
Hint: This assignment will require some math.
• 09-25-2012
typeqwerty
Quote:

Originally Posted by Matticus
Hint: This assignment will require some math.

should i use double or int or?
• 09-25-2012
whiteflags
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...
• 09-25-2012
Matticus
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?
• 09-25-2012
typeqwerty
is that supposed to be a trick question? 4hours?
• 09-25-2012
Matticus
And how did you arrive at that answer? (This is going somewhere, just bear with me.)
• 09-25-2012
novacain
Have you learnt the MODULUS operator '%' yet?

It returns the remainder of a division

ie 126/60 = 2 (minutes) and 126%60 = 6 (seconds)
• 09-25-2012
typeqwerty
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..
• 09-25-2012
Matticus
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?
• 09-25-2012
typeqwerty
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 = minutes/ 60;cout << "There are " << fixed << setprecision(3) << showpoint << minutes << " hours in" << seconds << hours << days << " seconds." << endl;}if (days >=86400 && days < 3600){days = hours/ 24;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.

• 09-25-2012
novacain
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'```
• 09-25-2012
typeqwerty
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.
• 09-26-2012
whiteflags
Quote:

Originally Posted by typeqwerty
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.
• 09-26-2012
typeqwerty
but when i run it on a site it where it automatically compile it would say i failed.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last