Converting minutes to hours and minutes with my code

Hi people,
I'm trying to do this small program for at least 2 hours and that's what I got so far. It should convert minutes to hours AND minutes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <iostream>
using namespace std;

int Transform(int c);

int main()
{
int rezultat2;
rezultat2=Transform(90);


cout<<"90 minutes means "<<a<<" hours and "<<b<<" minutes\n";
return 0;
}


int Transform(int c)
       {
	int a=c/60;
        double b=c%60;
	
		return result;
        }


*I know I shouldn't use masking variables as I don't want to get used to it 'cause It will get dificulties errors for more advanced programs.
*only one function should be used (apart from main) - only in case there's no other way
*should be as simple as possible with as few code lines as possible....
Any idea where am I doing wrong?
I've tried to look on other topics but I didn't understand, and besides I want to understand and remember with my code....
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int TransformToMinutes(int minutes_in)
{
  return minutes_in%60;
}

int TransformToHours(int minutes_in)
{
  return minutes_in/60;
}

int main()
{
  cout << "90 minutes means " << TransformToHours(90) << " hours and " << TransformToMinutes(90) << " minutes\n";
  return 0;
}



Thanks Stew,

It seems it works.
The exercise I was having though, it was saying to make "one" function....Can't it be done in only one?
closed account (2UD8vCM9)
Here is one way you could do it in one function

I didn't test this, but it should work

We add two parameters to our function int & minutes and int & hours The reason that we put the "&" is because that way instead of passing the variable by value, we're passing by reference. When we pass by reference, we can in result change the value of the variable from inside of the function. So this way we are setting the values for minutes & hours inside of the function. This also results in us changing the function to void, because instead of returning anything, it can just modify the values from within the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void Transform(int value, int & minutes, int & hours);

int main()
{
int hours;
int minutes;
int Minutes_To_Convert = 90;

Transform( Minutes_To_Convert, minutes, hours );


cout<<"90 minutes means "<<hours<<" hours and "<<minutes<<" minutes\n";
return 0;
}


void Transform(int value, int & minutes, int & hours)
{
int minutes=value%60;
int hours=value/60;
}

@Dizzybee

One small change has to be done to Pindrought's code.
1
2
3
4
5
void Transform(int value, int & minutes, int & hours)
{
int minutes=value%60; // Remove int from minutes and hours
int hours=value/60;  // Otherwise you get 're-defining variable' errors.
}
aaaah so that's what was wrong!

thank you all for ur help!
Topic archived. No new replies allowed.