Ok I'm back with another problem im having.. I have a vector of casual staffmembers.. Each time i create a casual staffmember they are added into a vector named thecasuals.. To return a casual staffmembers staffID a function is created:

staffmember::get_staffid();

I need to be able to enter a staffmembers ID number, and then search the vector for the correct staffmember and print the staff members details.. I have ATTEMPTED this as follows in a function in my 'company' class..:

company::staffIDsearch(int ID);
 
casstart = thecasuals.begin();
casend = thecasuals.end();
 
for (casstart; casstart < casend; casstart++)
 {
     if (thecasuals[casstart].get_staffid() == ID)
      {
            cout << thecasuals[cassstart].get_name(); << "    " << thecasuals[casstart].wage();
        }
  }
}

i am getting errors all over the place and am not sure what to do.. I have received errors saying i need to define 'casstart' and 'casend' but im not sure what to put them as, eg, int, casual...

Plus im sure there are syntax errors in there as i cannot find much helpful info on vectors and i'm lost..

Any Help would be appreciated!!

Thanks..

Jay Jones

Dani AI

Generated

Quick diagnosis based on 's snippet: the errors come from using iterators without declaring their type, treating an iterator like an index (thecasuals[casstart]), comparing iterators incorrectly, and a couple of typos. For vectors, either use an index, properly typed iterators, or (in modern C++) range-based loops or std::find_if. Iterators must be dereferenced (*it or it->member) and compared with != (or ==) in a loop header.

Example using an explicit iterator (works with older compilers):

#include <vector>
#include <iostream>

std::vector<StaffMember> casuals;
int ID = /* search id */;

for (std::vector<StaffMember>::const_iterator it = casuals.begin(); it != casuals.end(); ++it) {
    if (it->get_staffid() == ID) {
        std::cout << it->get_name() << " " << it->wage() << '\n';
        break; // if only one match is expected
    }
}

Cleaner, modern alternatives (C++11+): a range-based loop or std::find_if with a lambda.

for (const auto& s : casuals) {
    if (s.get_staffid() == ID) { std::cout << s.get_name() << '\n'; break; }
}

#include <algorithm>
auto it = std::find_if(casuals.begin(), casuals.end(),
                       [&](const StaffMember& s){ return s.get_staffid() == ID; });
if (it != casuals.end()) std::cout << it->get_name() << '\n';

If lookups by ID are frequent, keep a map from ID to index or to the object (std::unordered_map<int,size_t> or std::unordered_map<int,StaffMember>). That changes per-lookup cost from O(n) to average O(1). For reference on container/algorithm usage, see the standard library docs such as the std::vector reference and std::find_if. This approach lets thedeadline-friendly array solution mentioned by be revisited later with safer, clearer vector code.

Recommended Answers

All 2 Replies

Plus im sure there are syntax errors in there as i cannot find much helpful info on vectors and i'm lost..

See if you can see anything useful .

commented: Replyin quick.. Useful links.. Awesome.. Thanks mate!! +1

Thanks again for replying so quick wolf.. I realised my understanding of vectors was not going to reach it's requirements in time.. So i went back *a very long way* and worked it all out with arrays..

Now i have very minor issues, but i have completed enough to at least pass if worst comes to worst.. But i think i should work majority of it out.. Countdown begins.. I have:

20 Hoursish.. heh..

Thanks again.. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.