#include <iostream>
usingnamespace 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....
We add two parameters to our functionint & 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.
#include <iostream>
usingnamespace 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;
}
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.
}