doubt on a question

Jul 15, 2014 at 2:15pm
Hi guys, I am a beginner c++ programmer. I was solvig the following question:
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. The output should look like this:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 46 minutes, 40 seconds


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // seconds.cpp -- converts seconds to days, hours and minutes
#include <iostream>
using namespace std; 
int main()
{
  long seconds;
  cout << "Enter the number of seconds: ";
  cin >> seconds;
  unsigned long days = seconds / 86400;
  unsigned long hours = seconds % days;
  unsigned long minutes = seconds % hours;
  unsigned long rem_seconds = seconds  % minutes;
  cout << seconds << " seconds = " << days << " days, " 
       << hours << " hours, " << minutes << " minutes, " 
       << rem_seconds << " seconds." << endl;
  return 0;
}


Unfortunately, when I enter the number of seconds (31600000), I get the following error:
Floating point exception (core dumped).
But, it works fine with smaller inpute (like 3160000). Can anyone please tell me what is going on here. (I use gcc 4.6.3)
Jul 15, 2014 at 2:20pm
You get the error because the right hand operand of % is zero.
Jul 15, 2014 at 2:48pm
where? I didn't understand...
Jul 15, 2014 at 3:00pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std; 
int main()
{
    long seconds = 31600000;
    
    unsigned long days = seconds / 86400;
    cout << "days =           " <<  days << endl;
    
    if (days == 0)
        return 1;
        
    unsigned long hours = seconds % days;
    cout << "hours =          " <<  hours << endl;
    
    if (hours == 0)
        return 1;    
        
    unsigned long minutes = seconds % hours;
    cout << "minutes =        " <<  minutes << endl;
    
    if (minutes == 0)
        return 1;     
        
    unsigned long rem_seconds = seconds  % minutes;
    cout << "rem_seconds =    " <<  rem_seconds << endl;
    
    return 0;
}

Ouput:
days =           365
hours =          125
minutes =        0

--------------------------------
Process exited with return value 1

Jul 15, 2014 at 3:40pm
Please tell me where I went wrong. (why I'm getting the error I'm getting) I don't want solutions to the problem. I want to fix my solution. Why does it say: Floating point exception (core dumped) ????
Jul 15, 2014 at 3:41pm
Chervil: the program must ask the user to enter the number of seconds. It shouldn't be hard coded!
Jul 15, 2014 at 3:46pm
it works fine with smaller inpute (like 3160000)

Are you sure? When I run it with 3160000 I get:
Enter the number of seconds: 3160000
3160000 seconds = 36 days, 28 hours, 4 minutes, 0 seconds.


But desk checking this 36*86400 + 28*3600 + 4*60 = 3,211,440, not 3,160,000, so your algorithm is wrong.

I suggest that you try working out the problem a few times with a calculator to get a sense of how to do it. Check your work to make sure you're coming up with the right answer.
Jul 15, 2014 at 3:49pm
Please tell me where I went wrong
Peter87 gave you the answer. Take a look at lines 9-10 in your original code. If the number of seconds is less than 86400, what is the value of days? Zero, right? Line 10, you take the modulo. Modulo is a form of division (keeping the remainder). Since days is zero, you're attempting to divide by 0. Computers get rude when you try to divide by 0.

Note that in Chervil's code, he tested for days == 0 at line 10 exited the program, thereby avoiding the attempt to divide by zero.

edit:
Chervil was writing an example program for you to show how to avoid the divide by zero issue. He wasn't writing your assignment for you. You're free to take his code and add back in acepting input from the user.
Last edited on Jul 15, 2014 at 3:55pm
Topic archived. No new replies allowed.