mitchweb1
asked on
display one record from an array
I have an array of records and wish to only output one of the records depending on a value entered by the user. I have requested the user enters a time as a float, and intend to print out the next record which has the time after the one entered.
this is what i have and i guess what i need is the code to fit where i've put a row of *'s
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
for (i=0; i<sizeof(e)/sizeof(employe e); i++) {
*******
e[i].print_out();
}
thanks in advance for any help
this is what i have and i guess what i need is the code to fit where i've put a row of *'s
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
for (i=0; i<sizeof(e)/sizeof(employe
*******
e[i].print_out();
}
thanks in advance for any help
ASKER
say i have 6 records each containing a time and a name
9.00 fred
10.00 james
11.20 john
12.23 Jill
15.21 ian
16.45 mavis
if the user enters 11 then i want to print out "11.20 john"
9.00 fred
10.00 james
11.20 john
12.23 Jill
15.21 ian
16.45 mavis
if the user enters 11 then i want to print out "11.20 john"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... got my logic backwards. You need this:
if (e[i].theTime > timewanted)
{
....
}
if (e[i].theTime > timewanted)
{
....
}
ASKER
I've increased the points because I'm getting the following error
in function 'int main()':
'float employee::depTime' is protected
within this context
where depTime replaces timewanted, do you know how to get around this?
in function 'int main()':
'float employee::depTime' is protected
within this context
where depTime replaces timewanted, do you know how to get around this?
Can you post more of your source code. I expect that depTime is a protected member of your employee object, and you are trying to access it from outside the object. You should create a "getter" for this member variable, and then call this method whenever you need access to it:
float employee::getDepTime()
{
return depTime;
}
...
if (e[i].getDepTime() > timewanted)
{
}
float employee::getDepTime()
{
return depTime;
}
...
if (e[i].getDepTime() > timewanted)
{
}
ASKER
i've mailed the entire thing to you, hope its ok, just don't want to put the entire thing here
I cannot check this email address until tonight.
Did the information about protected members/getters help?
Did the information about protected members/getters help?
ASKER
Still getting the following errors, not sure about most but one is definately struggling with the getDepTime
in function 'int main()':
no 'float employee::getDepTime()' outside of class
is not definition
parse error before 'if'
at global scope:
parse error before'}' token
KHKremer told you this:
float employee::getDepTime()
{
return depTime;
}
That is the implementation of a member f unction, that MUST be declared as public member in class/struct employee:
class employee
{
protected:
float depTime;
....
public:
...
float getDepTime();
....
};
Another solution is to change 'protected' by 'public'. Then, you may use e[i].depTime in your main func.
Note, all members (data or functions) of a class that are declared as private or protected may only used within member functions of that class (and main() isn't a member function) or - if protected - by member functions of a derived class.
Your function now is:
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
float diff = 24.; // init to 24 hours difference
int idx = -1; // int index to invalid
float d = 0.; // temporyr variable
for (i=0; i<sizeof(e)/sizeof(employe e); i++)
{
// calculate difference
d = e[i].getDepTime() - timewanted; // if deptime is public, you may use e[i].depTime instead of e[i].getDepTime()
// make diff positive
if (d < 0.0) d = -d;
// check if new minimum found
if (idx < 0 || d < diff)
idx = i;
}
// print the record closest in time
if (idx >= 0) // .. if any
e[idx].print_out();
Regards, Alex
float employee::getDepTime()
{
return depTime;
}
That is the implementation of a member f unction, that MUST be declared as public member in class/struct employee:
class employee
{
protected:
float depTime;
....
public:
...
float getDepTime();
....
};
Another solution is to change 'protected' by 'public'. Then, you may use e[i].depTime in your main func.
Note, all members (data or functions) of a class that are declared as private or protected may only used within member functions of that class (and main() isn't a member function) or - if protected - by member functions of a derived class.
Your function now is:
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
float diff = 24.; // init to 24 hours difference
int idx = -1; // int index to invalid
float d = 0.; // temporyr variable
for (i=0; i<sizeof(e)/sizeof(employe
{
// calculate difference
d = e[i].getDepTime() - timewanted; // if deptime is public, you may use e[i].depTime instead of e[i].getDepTime()
// make diff positive
if (d < 0.0) d = -d;
// check if new minimum found
if (idx < 0 || d < diff)
idx = i;
}
// print the record closest in time
if (idx >= 0) // .. if any
e[idx].print_out();
Regards, Alex
ASKER
int i = 0;
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
for (i=0; i<sizeof(e)/sizeof(employe e); i++) {
float employee::getDepTime()}
if (timewanted < e[i].getDepTime)
{
e[i].print_out();
break;
i'm trying this, but getting two parse errors one before the if statement, and one slightly later.. have i put something in wrong here?
float timewanted;
cout<< "Enter a number:";
cin >> timewanted;
for (i=0; i<sizeof(e)/sizeof(employe
float employee::getDepTime()}
if (timewanted < e[i].getDepTime)
{
e[i].print_out();
break;
i'm trying this, but getting two parse errors one before the if statement, and one slightly later.. have i put something in wrong here?
>> float employee::getDepTime()}
- remove the closing bracket '}'
- if you want to call the function you have to do something like this:
float d = e[i].getDepTime();
Did you try using the code i posted above?
Regards, Alex
- remove the closing bracket '}'
- if you want to call the function you have to do something like this:
float d = e[i].getDepTime();
Did you try using the code i posted above?
Regards, Alex
ASKER
i tried it but got quite a few other errors, so decided to try and go with the one with less to start with
ASKER
sorry, i had already taken that closing bracket, and following that now have the two parse errors i described earlier, before removing it i had more. :(
What are you trying to accomplish with this line?
float employee::getDepTime();
I don't think you need it. Remove it and see if things get better. Also, please try to copy&paste the error messages, and some information about what line of code they refer to.
float employee::getDepTime();
I don't think you need it. Remove it and see if things get better. Also, please try to copy&paste the error messages, and some information about what line of code they refer to.
ASKER
I've taken that line out but now get the error message
in function 'int main()':
'class employee' has no member name 'getDepTime'
in regards to the following line
if (timewanted < e[i].getDepTime)
in function 'int main()':
'class employee' has no member name 'getDepTime'
in regards to the following line
if (timewanted < e[i].getDepTime)
You need to implement this as part of your employee class. Please see the the previous comments by Alex and myself about this method.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks guys, finally got it working using a combination of your help. Have increased the points and will split them. Thanks!
Have you considered doing this with an STL vector? This would probably be much simpler.