Hi Lilspree,
The user is supposed to enter the ID number of the employee he or she wants to search for. If the ID number exist it displays the employee name. I'm having trouble with that. Any suggestions? |
Some really lazy (on my part), but very very useful advice: Learn to use a debugger -
it will literally save you days of wondering what is wrong with the code. Step through the code 1 line at a time and keep an eye the values of your variables. Programs like valgrind can help too.
Also, what does the program do exactly (what is the output) - it might give a clue as to where to look.
Edit: removed wrong advice
Some other stuff that is good practice, and probably nothing to do with your problem:
Don't have
using namespace std;
- it brings in the entire std namespace which can cause naming conflicts, which is what namespaces were designed to avoid! put
std::
before each
std
thing - as in
std::cout
for example. Really you should put your own stuff in it's own namespace.
Trivial accessor functions can be inlined - for example:
inline int getEmpID() { return empID;}
inline
is just advice to the compiler: it will decide whether it will actually
inline
the function. Anything that is too long, recursive, contains loops or anything thing else it thinks will impact adversely won't be inlined.
Use initialiser lists in constructors:
1 2 3 4
|
EmployeeInfo::EmployeeInfo(int ID, string name) : // colon introduces init list
empID(ID),
empName(name);
{}
|
This is because variable are automatically initialised before any code in the constructor is executed, so doing this saves re-initialising them by assignment. One can initialise base classes from here too, if required.
Rather than using
new
&
delete
, try using smart pointers instead. The main problem with
delete
is early termination by exception or other means, so the destructor is never called.
Hope that helps