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.