Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I have a code for converting given seconds to the [HH:MM:SS] format. Could you Please explain what is happening in the underlined code?

What I have tried:

C++
#include <iostream>
using namespace std;

int  main()
{

	int t,h, m, s;


	cout << "Enter a time in seconds: ";
	cin >> t;
	h = t/3600;
	t = t%3600;
	m = t/60;
	t = t%60;
	s = t;

	cout<<endl;cout<<endl;cout<<endl;

	cout<<"The time in HH:MM:SS format is: "<<h<<" hours, "
		<<m<<" minutes, and "<<s<<" seconds!";
cout<<endl;cout<<endl;cout<<endl;
	return 0;
}
Posted
Updated 24-May-18 10:25am
v3
Comments
Rick York 24-May-18 10:25am    
If you run this code in a debugger you should be able to see the values at each calculation and it should all become clear to you.

Take a look at Modulus operator: Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com[^]:

Take a simple arithmetic problem: what's left over when you divide 11 by 3? The answer is easy to compute: divide 11 by 3 and take the remainder: 2. But how would you compute this in a programming language like C or C++? It's not hard to come up with a formula, but the language provides a built-in mechanism, the modulus operator ('%'), that computes the remainder that results from performing integer division.

Assuming t = 3602,

h = t/3600; // h = 1 because of integer

t = t%3600; // t = 3602 - 3600 = 2;

and so on ...
 
Share this answer
 
v4
Comments
Richard MacCutchan 24-May-18 7:38am    
3602, not 36002.
Leo Chapiro 24-May-18 9:59am    
Thanks, Richard, updated.
Indeed, the time is being taken and divided down using standard division / and modulus % arithmetic. Each time you see t = , it's further subdividing the time based on the modulus.
 
Share this answer
 
Quote:
I have a code for converting given seconds to the [HH:MM:SS] format. Could you Please explain what is happening in the underlined code?

What about watching the code execute with the debugger ?
It is never too soon to read the language documentation too.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900