?
Solved

display one record from an array

Posted on 2004-04-12
19
Medium Priority
?
219 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 150 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 150 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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