Converting hours to seconds, seconds to hours

Hello,

Ive got this assignment where we need to calculate the inputted seconds into hours, minutes and seconds and vice versa.

Ive been at this for a while now, but I cant seem to find my mistake.

The program needs to do the calculation using functions, but my function seems to be broken..

any pointers would be much appreciated.

[code]
Sorry, the codes a bit messy at the moment.

So essentially the program doesnt do the calculations correctly. At this point I think ive just messed it up so badly I dont know what to do...

Any help please?

Thank you.
Last edited on
@wirelesskill
i think you need to pass total variable to the function time in 45th line and you are not returning anything from function time.
Last edited on
1
2
3
4
5
6
7
8
9
10
// Given a duration in hours, minutes and seconds, return the duration in seconds.
unsigned toSeconds(unsigned hours, unsigned minutes, unsigned seconds)
{
    ...
}

// Given a duration in seconds, normalize it to hours, minutes, and seconds.
void toHMS(unsigned &hours, unsigned &minutes, unsigned &seconds)
{
}

How about something like this:

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
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#define HSEC 3600        // seconds in an hour

struct s_clock {
int hours;
int mins;
int secs;
};

void calc_to_hms(int total, s_clock &myclock) {
  myclock.hours = (total / HSEC);
  myclock.mins  = (total % HSEC) / 60;
  myclock.secs  = (total % HSEC) % 60;
}

int main () {
  int choice, total;
  s_clock myclock{0,0,0};

  do{
  cout <<"1 - ENTER SECONDS;" << endl;
  cout <<"2 - ENTER HOURS, MINUTES and SECONDS;" << endl;
  cout <<"3 - EXIT" << endl;
  cout <<"-> ";
  cin >> choice;
  cin.clear();
  cin.ignore(1000,'\n');
  cout << endl;

    switch(choice) {
    case 1:
    cout <<"ENTER SECONDS: ";
    cin >> total;
  
    calc_to_hms(total, myclock);
  
    cout << endl
         << total << " seconds is " 
         << myclock.hours << "h " 
         << myclock.mins << "min " 
         << myclock.secs << "sec" 
         << endl;
    break;
    case 2:
         // for menu option 2
    break;
    default:
         // for everything else
         ;
    }

  } while(choice != 3);

return (0);
}


I'm still pretty new with C++ myself, so please forgive anything that is considered inelegant, poor practice, or just plain wrong in C++ world. However, the code does work. I didn't try to do anything for the second menu option, but I'm sure similar logic could be employed for that too.

Also, for the original poster, I don't see anything that requires you to use "#include <iomanip>" in your code, unless you were including it for some future part of your project.
Last edited on
Thats good, and it works! haha
However, I cant use structures just yet. I have to stick to regular functions and pass by reference. But being me I cant figure out the reference parts.

Heres what I have so far:

#include <iostre


Its a complete mess right now. But I hope someone gets what Im trying to do there.

Essentailly I have references for every value that I want to return but it does not work..

Any further pointers would be much appreciated.

Thank you again
Last edited on
No problem. However, unless I'm mistaken, you seem to be getting a bit confused with the passing-by-reference business, as your syntax is not right. Also, even though it's incorrect anyway, you're passing in "secondss" to the time function when your function uses "seconds". (i.e. you have an extra "s").

Additionally, you seem to be passing just the one argument to time(), but you have it accepting several.

Aside, I think the following quick explanation and examples of using functions/references at IBM may prove helpful:

https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr233.htm

I think you can probably get your code at least a bit further along after reading that. In fact just the first example on that page should give you a much better idea.
Last edited on
I mean the only error im getting right now is :
7 error: invalid operands of types 'int' and 'int' to binary 'operator/'

I fixed the little mistakes i.e. the double 's'.

But cant get rid of the error.


Oh and I had a look at the IBM site, it did help me a bit more to understand the syntax.
Last edited on
Well, I don't know what your current version looks like, but based on the most recent version which you posted, (and also taking into consideration that I'm kinda new to C++ myself) I would imagine the error may have to do with you using pointer-like syntax in the function parameters (i.e. "*"), and the body, while you're not actually using pointers at all.

While I understand you can't use structs, on line 15 of the code I provided, you can see how the function accepts a reference by prefixing the variable with a "&", and on line 40 you can see how the function is called with that variable (i.e. no prefix).

I'm sure if you post your current version then someone with more experience than me will be able to help you further.
Last edited on
Im facepalming right now. Yes, it was the pointer like syntax, guess I wasnt as on point as I thought...

Heres the updated code:

1
2
3
4
5
#include <iostream>

using namespace std;




Right now my issue is with returning the calculation values done in function 'time' back to the main function to be displayed in line 34. Clearly that syntax is wrong as well.

Sorry to keep coming back to it...
Last edited on
I don't mind helping you as I also learn by trying to figure out stuff like this, but there may well be others who can provide more insightful and useful comments.

Nonetheless, I think you'll find the modified version of your code below does what you want (i.e. no structs, but using references and functions); however, that's not to say it's the best way - it's just a way:

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
#include <iostream>

using namespace std;

void time(int &a, int &hours, int &minutes, int &seconds) {
  hours = (a / 3600);
  minutes = (a % 3600) / 60;
  seconds = (a % 3600) % 60;
}

int main () {
  int choice, total, hours, minutes, seconds;

  do{
  cout <<"CHOICE" <<endl;
  cout <<"1 - ENTER SECONDS;" <<endl;
  cout <<"2 - ENTER HOURS, MINUTES AND SECONDS;" <<endl;
  cout <<"3 - EXIT " <<endl;
  cout <<"-> ";
  cin >>choice;
  cin.clear();
  cin.ignore(1000,'\n');
  cout <<endl;

  switch(choice){
    case 1:
    cout <<"Enter seconds: ";
    cin >> total;
    cout <<endl;
    time(total,hours,minutes,seconds);
    cout << total <<" seconds is  " << hours <<"h " << minutes <<"min " << seconds <<"sec" <<endl;
    cout <<"\n";
    break;
    }
  } while(choice != 3);

return (0);
}


Note that my version of your time function returns void (as opposed to the int which you had), and that it also accepts four int parameters which are references. See how these are passed in line 30.

The time function doesn't actually return anything (which is why it's prefixed with "void"), but modifies the actual variables passed to it (i.e. not just a copy of the variables). Such is the nature of references.

Note also lines 21 and 22, which will help ensure that dodgy input gets ignored.

Note too the changes on line 31; the variables have already been populated with the values you need (line 30 accomplished this), so no need to call your function again to access them or do anything else with them.

Hopefully you'll now be able to implement the second menu option, from HMS to seconds; other than a new function I guess the program logic should be pretty similar.

Anyway, like I said; this code does what you want, but whether it's the best way or not is debatable. It's a pity you can't use structs, as in the initial example I provided.
Last edited on
Oh right, think i get the references now.
I didnt realise i can simply pass 4 values that easily.....

And didnt realise that you simply put the reference in line 31 without any pointers or anything.

This has helped alot.

Yeah, we're still going through the basics so they only want us to use what we have covered so far.

Thank you so much @Cheddar99 you have explained this better than you realise! :D
Topic archived. No new replies allowed.