Solved

display one record from an array

Posted on 2004-04-12
19
183 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
ID: 10804544
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
ID: 10804554
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
ID: 10804587
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
ID: 10804592
... got my logic backwards. You need this:

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

Author Comment

by:mitchweb1
ID: 10804641
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
ID: 10804670
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
ID: 10804789
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
ID: 10804867
I cannot check this email address until tonight.
Did the information about protected members/getters help?
0
 

Author Comment

by:mitchweb1
ID: 10804900

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10805268
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
ID: 10805452
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
ID: 10805498
>> 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
ID: 10805507
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
ID: 10805577
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
ID: 10805863
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
ID: 10805899
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
ID: 10805935
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
ID: 10806002
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
ID: 10813804
thanks guys, finally got it working using a combination of your help. Have increased the points and will split them. Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 35
returning a dereferenced pts in C++ 10 141
PDF library for Delphi 2 105
max float value 3 26
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

920 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