Solved

display one record from an array

Posted on 2004-04-12
19
173 Views
Last Modified: 2010-04-01
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(employee); i++) {
    *******
     e[i].print_out();
   }

thanks in advance for any help
0
Comment
Question by:mitchweb1
  • 9
  • 7
  • 3
19 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
I don't understand your question. What exacty are you looking for? Which record do you want to print? If the user enters 5, do you want to print the record number 5, or the record number 6? Why are you looping over all records?

Have you considered doing this with an STL vector? This would probably be much simpler.
0
 

Author Comment

by:mitchweb1
Comment Utility
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"
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
Comment Utility
OK, now it's much clearer. How is your class (or struct) defined that holds these records? In general, you would do something like this (this assumes that your records are sorted by the time):

for (i=0; i<sizeof(e)/sizeof(employee); i++) {
     if (timewanted>e[i].theTime)
     {
          e[i].print_out();
          break;
     }
   }

 
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
... got my logic backwards. You need this:

if (e[i].theTime > timewanted)
{
    ....
}
0
 

Author Comment

by:mitchweb1
Comment Utility
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?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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)
{
}

0
 

Author Comment

by:mitchweb1
Comment Utility
i've mailed the entire thing to you, hope its ok, just don't want to put the entire thing here
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
I cannot check this email address until tonight.
Did the information about protected members/getters help?
0
 

Author Comment

by:mitchweb1
Comment Utility

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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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(employee); 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
0
 

Author Comment

by:mitchweb1
Comment Utility
int i = 0;
  float timewanted;
  cout<< "Enter a number:";
  cin >> timewanted;
 for (i=0; i<sizeof(e)/sizeof(employee); 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?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>> 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
0
 

Author Comment

by:mitchweb1
Comment Utility
i tried it but got quite a few other errors, so decided to try and go with the one with less to start with
0
 

Author Comment

by:mitchweb1
Comment Utility
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. :(
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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.
0
 

Author Comment

by:mitchweb1
Comment Utility
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)
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
You need to implement this as part of your employee class. Please see the the previous comments by Alex and myself about this method.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
Comment Utility
That code compiles and runs on my system:

class employee
{
protected:
    float depTime;
    char  name[32];
public:
    employee() : depTime(0.0) { name[0] = 0; }
    void  setData(float tim, const char* pszNam);
    float getDepTime();
    void print_out();
};

void  employee::setData(float tim, const char* pszNam)
{
    depTime = tim;
    strcpy(name, pszNam);
}
float employee::getDepTime()
{
    return depTime;
}
void employee::print_out()
{
    cout << depTime << "  " << name << endl;
}


int main() {
   
    employee e[4];
    e[0].setData(8.30F,  "Bob");
    e[1].setData(10.00F, "John");
    e[2].setData(12.10F, "Alice");
    e[3].setData(12.50F, "Jack");
    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 (int i=0; i<sizeof(e)/sizeof(employee); 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();
   
    return 0;
}

Hope, that helps

Alex

0
 

Author Comment

by:mitchweb1
Comment Utility
thanks guys, finally got it working using a combination of your help. Have increased the points and will split them. Thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now