Error Comparing Strings

Employee is a class with 2 data members fname and IDIn.  In main, I’ve declared an array of strings, and also declare two character arrays for user entered data.  These declarations are as follows:

   const MAX_PERSONS = 100;
   Employee *emp1 = new Employee[MAX_PERSONS];
   char fname[15], IDIn[10];

I then declare an Employee object and print to screen like:

   int i = 0;    
   emp1[i] = {“John”, “j39846xva”} // assign emp1 parameters : ‘Name’, and ‘IDIn’
   cout << emp1[i].getID( );      // using Employee class member function getID( )

All is fine so far, it seems to work as I expect.   Where I seem to be having difficulty is when I try and perform a boolean comparison of 2 strings as shown in the if statement below.

   char IDtemp[10];  // declare temporary variable which user enters from keyboard
   cin  >> IDtemp;
   if (emp1[i].getID( ) == IDtemp)
       cout << "Employee found";

While this problem doesn't crash my compiler, it doesn't even seem to be processed during runtime either...even if I know for sure that IDtemp has been entered exactly the same as IDIn.  "Employee found" is never output to the screen.

Can someone please tell me if they think this method of comparing the 2 strings is wrong, it think it must be since it doesn't seem to work.  If so then can you explain how to make such a comparison work properly.  Thanks in advance for any help you can offer.
InvariantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eranborovikCommented:
You are trying to compare to char array. What the compiler
is doing is just comparing two pointers, and these will
never be the same. To make it work, you must replace the
character array with a string object. This object know
how to compare itself with others, and it should work.
You can also use it like you used your char array with
cin and cout.

The things you must do to make it work is:
1.#include <string>
2.using namespace std;
3.Everywhere you are using char * or char array, replace
  with string.
0
InvariantAuthor Commented:
Thanks alot for your response eranborovik.

I also tried changing my code as follows:

////////////////////////////////////////////////////////

 char ArrayID[10];  // declared a new char array
 char *IDptr;      // instead of char IDtemp[10];
 IDptr = &ArrayID; // direct pointer to address of ArrayID

 cin >> *IDptr;    // enter a value and store it in ArrayID

 if (emp1[i].getID( ) == IDptr)  //dereference pointer IDtemp to access its value
      cout << "Employee found";

///////////////////////////////////////////////////////

but this didn't work and came back with 1 error related to the line of code where I say:

 IDptr = &ArrayID; // direct pointer to address of ArrayID

The actual error said:

"cannot convert from 'char (*)[10]' to 'char *'
 Types pointed to are unrelated; conversion requires
 reinterpret_cast, C-style cast or function-style cast"

So (to my untrained eye) it seems the error message is telling me that there is a way to make my method work (through casting methods..which I don't really understand).  Is it possible to show me how casting methods can work using my simple example or is it too complicated to present here.

Using the strings header file does work (and I can fall back to using it if I must) but in the interest of my own education I would like to understand the idea of type casting and how to apply it in this situation.

Thanks to eranborovik and to anyone else who might feel they can enlighten me.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
An array name is a pointer and a constant.  

change IDptr = &ArrayID; // direct pointer to address of ArrayID

TO

IDptr = ArrayID; // NO &

when you say

IDptr = &ArrayID;

you're telling the pointer to get the address of the pointer on the right.  

IE:
single level pointer vs a double lever pointer.
- - - --  - -- - - -- - - - - - - - - - - - - - - - - -

As for casting methods . . . their easy.  You cast when you have one thing, but need it to look like something else:
say you have a char array[10];

but in a function you need a string:

you can send down my_func( (string) array);

and it will try to convert it to a string for you.

note: they're not all by directional.  

You can make a char array a string, but you can't do it in reverse.  Likewise, an integer casted to a float should give you the int and .0 at the end of it.  Try to go back and I believe you end up rounding it.  (Haven't done it in a while, so I'm not sure.)

Hope that helps.


0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

InvariantAuthor Commented:
Thanks ged325.  That makes sense.

So, on the casting issue...from what you've said, in order to cast one variable type as another variable type, just use the syntax as follows:

   (NewVariableTypeNeeded) VariableNameToConvert

Is it just that simple?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Yup.  You can do that with functions as well
buf[256];
string [x];

x = (string) convertchartostring(buf);

char* convertchartostring(buf[]);
 return buf;

If I remember correctly that should convert it to a string, my passing or return may be off, been a while since I've used it.  Hope that helps.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InvariantAuthor Commented:
Great stuff.  Thanks alot ged325, I'll practice that.  I wish I could give points to both people who responded to my original question.  Unfortunately I must choose.  Even though eranborovik offered a suggestion that did resolve my original question, I think ged25 has helped expand my horizons the most and offered a way for me to use my original code with fewer changes....{:-).  Thanks to you both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.