#include <iostream.h>
#include <conio.h>
void daystohrsmins(int days, int &hours, int &min)
{
hours=days*24;
min=hours*60;
}
int main()
{
int days = 0;
int hours, min;
for(days=1;days<11;days++)
{
daystohrsmins(days, 0, 0);
cout<
For some reason it produces the output:
1 day(s) is 18201 hours or 32255 minutes
2 day(s) is 18201 hours or 32255 minutes
...
10 day(s) is 18201 hours or 32255 minutes
instead of
1 day(s) is 24 hours or 1440 minutes
2 day(s) is 48 hours or 2880 minutes
...
10 day(s) is 240 hours or 14400 minutes
Please help
Help with time program
March 12, 2003 06:00 PM
I think its because your referencing(& ''min'' and ''hrs'', because that would explain why they always display the same values and never change.
You''re getting yourself all confused there.
void daystohrsmins(int days, int &hours, int &min)
The hours and min here refer to the variables hours and min within the function.
daystohrsmins(days, 0, 0);
Here you''re passing literals, not variables. You seem to think that the function will update the variables within your main function, but it has no way of knowing that. You need to pass the variables from your main to your function.
Perhaps if you rewrote your function using different variables names it will be clearer:
void daystohrsmins(int inDays, int &outHours, int &outMins)
{
outHours=days*24;
outMins=outHours*60;
}
So its clearer what scope you''re working in.
- Ben Scott
Just starting out
void daystohrsmins(int days, int &hours, int &min)
The hours and min here refer to the variables hours and min within the function.
daystohrsmins(days, 0, 0);
Here you''re passing literals, not variables. You seem to think that the function will update the variables within your main function, but it has no way of knowing that. You need to pass the variables from your main to your function.
Perhaps if you rewrote your function using different variables names it will be clearer:
void daystohrsmins(int inDays, int &outHours, int &outMins)
{
outHours=days*24;
outMins=outHours*60;
}
So its clearer what scope you''re working in.
- Ben Scott
Just starting out
- Ben Scott
My Help
void daystohrsmins(int days, int &hours, int &min)
The &hours and &min is because the hours and min you want to update are outside the function daystohrsmins()
With your declaration you cannot send two constant integers, 0 and 0 to this function, it has to be an int… so send hour and min. But wait! Why do you have to send the “2 constant zeros” again and again? It’s a waste, but that’s a different lesson.
Read on pointers and pass by reference, everything will be clear.
All the best!
Here’s my working version
#include <iostream.h>
#include <conio.h>
void daystohrsmins(int days, int &hours, int &min)
{
hours=days*24;
min=hours*60;
}
int main()
{
int days = hours = min = 0;
for(days=1;days<11;days++)
{
daystohrsmins(days, hours, min);
cout << days << " day(s) is " << hours << " hours " << " or " << min << " minutes\n";
}
return 0;
}
void daystohrsmins(int days, int &hours, int &min)
The &hours and &min is because the hours and min you want to update are outside the function daystohrsmins()
With your declaration you cannot send two constant integers, 0 and 0 to this function, it has to be an int… so send hour and min. But wait! Why do you have to send the “2 constant zeros” again and again? It’s a waste, but that’s a different lesson.
Read on pointers and pass by reference, everything will be clear.
All the best!
Here’s my working version
#include <iostream.h>
#include <conio.h>
void daystohrsmins(int days, int &hours, int &min)
{
hours=days*24;
min=hours*60;
}
int main()
{
int days = hours = min = 0;
for(days=1;days<11;days++)
{
daystohrsmins(days, hours, min);
cout << days << " day(s) is " << hours << " hours " << " or " << min << " minutes\n";
}
return 0;
}
The sky is the limit !
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement