need help with problem... (dont know how to explain..)

1
2
3
4
5

3-40 Printing a Time 
In printing an item such as a time it is often more realistic to print out the time taken in hours, minutes, and seconds. Write a program that will read a positive integer representing an amount of time in seconds; then convert this to hours, minutes, and seconds; and print out the result with appropriate text. For example, the number 03812 should give as output

03812 Seconds = 1 Hour 3 Minutes 32 Seconds


so that's the problem i have. D: and so far, i've gotten only this far... (sorry i'm like. beginner of all beginners.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void main()
{
  double Seconds;
  double Hours;

  cout << "If you input a time in seconds, I will tell you the time in hours, minutes and seconds: \n";
  cin >> Seconds;

 Hours = Seconds / 3600;
  cout << Hours
        << endl;
  
  system ("pause");
}


this would give the hours and decimals. would i try to retrieve the values after the decimal and multiply it by 3600 to get back seconds and redivide by 60 to get minutes and keep repeating it?
How you do this: You divide the number of seconds and return the number of hours, no matter what you type as input. So you have to use % (modulo).
1
2
3
4
5
6
7
8
9
int sec=0;
int min=0;
int h=0;

//your input stuff

int tmp=sec%3600; //Here you get the number of seconds you have to substract to get a integer for h
tmp=sec-tmp;
h=tmp/3600; //So you have a integer number for the hours. 


For minutes it's quite equal. So try it on your own and post it in here so that I can say you if you're right.
void main()


main always returns int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	unsigned value = 3812 ;

	unsigned seconds = value % 60 ;  // seconds = remainder when value is divided by 60.
	value /= 60; 

	unsigned minutes = value % 60 ;  // ...
	value /= 60 ;

	unsigned hours = value ;         // and we're left with hours.

	std::cout << hours << ':' << minutes << ':' << seconds << '\n' ;
}

@ cire, heey what do you mean by main always returns int. ? sorry!

and @ FlashDrive thanks for enlightening me on the modulo!
main's return type in c++ is required to be int.

If the code in main doesn't return an int, the implicit value returned is 0. main is the only function for which this is true.
Topic archived. No new replies allowed.