Tibo
Tibo

Reputation: 1

c++ Changing from second into days,hours.minutes and second

cout << "Please enter the elapsed time in seconds or (0 to end the program): " ;
cin >> totalSeconds;

days = totalSeconds / 86400 % 60;
hours = totalSeconds/ 3600 % 60;
minutes =  totalSeconds/ 60 % 60;
seconds = totalSeconds % 60;

The small number works fine but once I start using bigger number it's not working anyone knows why? Here are the logs :

Please enter the elapsed time in seconds or (0 to end the program): 62
The equivalent time of 62 seconds in days:hours:minutes:seconds is: :0:0:1:2
Please enter the elapsed time in seconds or (0 to end the program): 9630
The equivalent time of 9630 seconds in days:hours:minutes:seconds is: :0:2:40:30
Please enter the elapsed time in seconds or (0 to end the program): 216000
The equivalent time of 216000 seconds in days:hours:minutes:seconds is: :2:0:0:0

both the 62 and 9630 second works fine but not the 216000s

Upvotes: 0

Views: 428

Answers (1)

selbie
selbie

Reputation: 104484

Let's start with your formula for days:

days = totalSeconds / 86400 % 60;

If totalSeconds is 5270400 (61 days), that equation will compute 1 for days. That's probably not your only bug.

Your modulus parameter of 60 on the right of the % doesn't appear correct or it's not needed as you are using it.

This is probably what you want without being clever.

days = totalSeconds / 86400;
totalSeconds = totalSeconds % 86400;

hours = totalSeconds / 3600;
totalSeconds = totalSeconds % 3600;

minutes = totalSeconds / 60;
totalSeconds = totalSeconds % 60;

seconds = totalSeconds;

Upvotes: 2

Related Questions