[try Beta version]
Not logged in

 
time calculator

Jun 13, 2013 at 5:48pm
i have no idea how to do this problem..somebody help?

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.

 
  Put the code you need help with h
Last edited on Jun 14, 2013 at 6:55am
Jun 13, 2013 at 5:56pm
i know you are suppose to divide the amount of seconds the user inputs with the seconds that is listed above..but the thing is lets say i input 90 seconds and i divide that with 60..that will give me 1.5. the thing is that 1.5 is suppose to translate to minutes. so in minutes that should be a minute and thirty seconds. how do i do that?
Jun 13, 2013 at 6:27pm
90 / 60 == 1 minute (integer division)

90 % 60 == 30 seconds ( modulo or remainder after division )
Jun 13, 2013 at 6:33pm
ahhh i see.. thank you
Jun 13, 2013 at 6:35pm
Here's a different way to do it that is completely obsolete and over complicated:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

int S;	
int M;
int H;
int D;
int seconds;

int Calcadd(int seconds, int subtract, int unit)
{
while(seconds>=subtract)
{
	seconds-=subtract;
	unit++;
}

return unit;
}
int Calcsub(int seconds, int subtract)
{
while(seconds>=subtract)
{
	seconds-=subtract;
}
return seconds;
}

int main()
{
/// uneffiencent way to do it
	
using namespace std;

cin>>seconds;

D=Calcadd(seconds, 86400, D);
seconds=Calcsub(seconds, 86400);
H=Calcadd(seconds, 3600, H);
seconds=Calcsub(seconds, 3600);
M=Calcadd(seconds, 60, M);
seconds=Calcsub(seconds, 60);
S=Calcadd(seconds, 1, S);
seconds=Calcsub(seconds, 1);
cout<<D<<" Days "<<H<<" Hours "<<M<<" Minutes "<<S<<" Seconds "<<endl;
system("pause");

return 0;
}



Last edited on Jun 13, 2013 at 6:35pm
Jun 14, 2013 at 12:58am
closed account (NyhkoG1T)
You can launch it and answer the prompt or pass a flag of -s with the seconds and bypass the prompt
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// testingcode.cpp : Defines the entry point for the console application.
//

#include <iostream>

using namespace std;


const unsigned int DECADES = 314496000;
const unsigned int YEAR = 31449600;
const unsigned int WEEK = 604800;
const unsigned int DAY = 86400;
const unsigned int HOUR = 3600;
const unsigned int MINUTE = 60;

unsigned int CALCULATE(unsigned int *sInput, const unsigned int Divisor) {
	unsigned int temp;
	if(!(*sInput<Divisor)) {
		temp = *sInput / Divisor;
		*sInput = (*sInput % Divisor);
		return temp;
	}
	return 0;
}

int main(int argc, char * argv[]) {
	unsigned int seconds;

	if((argc==3) && (!strcmp(argv[1], "-s"))) {
		seconds = atoi(argv[2]);
	}else{
		cout << "Enter any number that represents seconds: ";
		cin >> seconds;
		cout << endl << endl;
	}

	unsigned int CalcDec = CALCULATE(&seconds, DECADES);
	unsigned int CalcYear = CALCULATE(&seconds, YEAR);
	unsigned int CalcWeek = CALCULATE(&seconds, WEEK);
	unsigned int CalcDay = CALCULATE(&seconds, DAY);
	unsigned int CalcHour = CALCULATE(&seconds, HOUR);
	unsigned int CalcMinute = CALCULATE(&seconds, MINUTE);

	if(CalcDec != 0) {
		cout << CalcDec << ((CalcDec > 1) ? " decades " : " decade ");
	}
	if(CalcYear != 0) {
		cout << CalcYear << ((CalcYear > 1) ? " years " : " year ");
	}
	if(CalcWeek != 0) {
		cout << CalcWeek << ((CalcWeek > 1) ? " weeks " : " week ");
	}
	if(CalcDay != 0) {
		cout << CalcDay << ((CalcDay > 1) ? " days " : " day ");
	}
	if(CalcHour != 0) {
		cout << CalcHour << ((CalcHour > 1) ? " hours " : " hour " );
	}
	if(CalcMinute != 0) {
		cout << CalcMinute << ((CalcMinute > 1) ? " minutes " : " minute ");
	}
	if(seconds != 0) {
		cout << seconds << ((seconds > 1) ? " seconds " : " second ");
	}

	cout << endl;
	return 0;
}


Basically, the calculate function returns the number of decades,years,weeks, etc, if calculable (i.e. enough seconds to make a decade) and the seconds variable is over-written with the remainder by using a pointer and passing the reference of seconds to the calculate function. I set-up some constant variables to represent how many seconds it take to make a minute, hour, etc and used that to pass a divisor to the calculate function.. pretty simple when you know the math.

You could also define the constants like this
1
2
3
4
5
6
const unsigned int DECADES = 1*60*60*24*7*52*10;
const unsigned int YEAR = 1*60*60*24*7*52;
const unsigned int WEEK = 1*60*60*24*7;
const unsigned int DAY = 1*60*60*24;
const unsigned int HOUR = 1*60*60;
const unsigned int MINUTE = 1*60;

which would be an easier step than whipping out the calculator and doing it manually.
using this you could easily incorporate centuries if you wanted to
 
const unsigned int CENTURY = 1*60*60*24*7*52*10*10;

however, you could only calculate one century before going beyond the capabilities of an integer.
Last edited on Jun 14, 2013 at 1:08am
Jun 14, 2013 at 3:18am
closed account (NyhkoG1T)
I'm really not trying to hijack the thread, but this issue is based on this code I provided and I am stumped.

Out of curiousity, I typed in a letter instead of an integer to see what would happen.

1
2
3
4
5
6
7
8
9
10
11
'testingcode.exe': Loaded 'C:\Users\Lucian\Documents\Visual Studio 2010\Projects\testingcode\Debug\testingcode.exe', Symbols loaded.
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x1100) has exited with code -1073741510 (0xc000013a).
'testingcode.exe': Loaded 'C:\Windows\SysWOW64\apphelp.dll', Cannot find or open the PDB file
'testingcode.exe': Loaded 'ImageAtBase0x49f60000', Loading disabled by Include/Exclude setting.
'testingcode.exe': Unloaded 'ImageAtBase0x49f60000'
The program '[1072] testingcode.exe: Native' has exited with code -1073741510 (0xc000013a).


It threw this up. I tried wrapping the assignment to seconds through cin with try/catch, nothing. So I wrapped the contents of the CALCULATE function with try/catch, nothing.

EDIT: SIGH, n/m. I walked away in frustration and it came to me.

1
2
3
4
5
6
			if(!cin) {
				cout << endl << "You entered an ERRONEOUS value. Please try again." << endl;
				cin.clear();
				cin.ignore();
				continue; //test code is inside infinite loop
			}
Last edited on Jun 14, 2013 at 3:33am
Jun 14, 2013 at 5:08am
this is what i have so far...
int seconds;
int total1,total2,total3;

cout << "please enter a number of seconds"<< endl;
cin >> seconds;

if (seconds >= 60)
{
total1 = seconds % 60;
cout << total1 << endl;
}
else if (seconds >= 3,600)
{
total2 = seconds % 3,600;
}
else if (seconds >= 86,400 )
{
total3 = seconds % 86,400;
}
else
cout << "sorry try again"<< endl;




when i put for example 90 as an put it only outputs 30 cause im using the % so it only displays the remainder...i want it to display the whole thing the integer and the remainder ( 1:30 ) a minute and thirty seconds
Jun 14, 2013 at 7:31am
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
30
31
32
33
34
35
36
#include <iostream>

int main()
{
    constexpr int SECS_PER_MINUTE = 60 ;
    constexpr int SECS_PER_HOUR = 3600 ;
    constexpr int SECS_PER_DAY = 86400 ;

    int seconds ;
    std::cout << "total seconds? " ;
    if( std::cin >> seconds && seconds > 0 )
    {
        const int days = seconds / SECS_PER_DAY ;
        if( days > 0 )
        {
            std::cout << days << " day(s), " ;
            seconds = seconds % SECS_PER_DAY ;
        }

        const int hours = seconds / SECS_PER_HOUR ;
        if( hours > 0 )
        {
            std::cout << hours << " hour(s), " ;
            seconds = seconds % SECS_PER_HOUR ;
        }

        const int minutes = seconds / SECS_PER_MINUTE ;
        if( minutes > 0 )
        {
            std::cout << minutes << " minute(s), " ;
            seconds = seconds % SECS_PER_MINUTE ;
        }

        std::cout << seconds << " second(s).\n" ;
    }
}
Jun 15, 2013 at 10:54am
hello JLBorges..i kind of understand your code but why for the cin did you put it i the if stattement? im confused...
Jun 15, 2013 at 1:39pm
> why for the cin did you put it i the if stattement?

To check if the user entered a number.
std::cin >> seconds will evaluate to false if the user entered, say, abcd instead of a number.

1
2
3
if( std::cin >> seconds // if the user did enter a number
    && seconds > 0 // and the number entered is greater than zero
  )
Jun 17, 2013 at 6:57am
oh i see. i understand thanks for the help
Jun 17, 2013 at 2:30pm
My little addition for this morning:

1
2
3
constexpr int SECS_PER_MINUTE = 60 ;
constexpr int SECS_PER_HOUR   = 60 * SECS_PER_MINUTE ;
constexpr int SECS_PER_DAY    = 24 * SECS_PER_HOUR ;

Last edited on Jun 17, 2013 at 2:31pm
Jun 18, 2013 at 8:33am
the following code might help u....its a simple one:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std;
int main()
{
    int d,h,m,s,n; long t,r; char ch='y';
    while(ch=='y'||ch=='Y')
    {
    cout<<"Menu:\n1.Convert total time to seconds.\n2.Convert the given time in seconds to standard notation.";
    cout<<"\nEnter your choice:";
    cin>>n;
    switch(n)
    {
    case 1:         
    cout<<"\nPlease enter the no. of days:";
    cin>>d;
    cout<<"\nPlease enter the no. of hours:";
    cin>>h;
    cout<<"\nPlease enter the no. of minutes:";
    cin>>m;
    cout<<"\nPlease enter the no. of seconds:";
    cin>>s;
    t=86400*d+3600*h+60*m+s;
    cout<<"\nThe total time in seconds is:\n";
    cout<<t<<"secs";
    break;
    case 2:
    cout<<"Please enter the time in seconds:";
    cin>>t;
    if(t>=86400)
    {
    d=t/86400;  
    cout<<d<<"days ";
    }
    r=t%86400;
    if(r>=3600)
    {
    h=r/3600;
    cout<<h<<"hours ";
    }
    r%=3600;
    if(r>=60)
    {
    m=r/60;
    cout<<m<<"mins ";
    }
    s=r%60;
    if(s>0)
    cout<<s<<"secs ";
   }
    cout<<"\nWant to try again?\nPress y to try again.\nPress any other key to terminate.";
    cin>>ch;
    }
    return 0;
}
Jun 18, 2013 at 11:06am
Create class Time and then in Constructor set function Check(int,....)
this function should be private
Topic archived. No new replies allowed.