Link to home
Start Free TrialLog in
Avatar of Invariant
Invariant

asked on

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.
Avatar of eranborovik
eranborovik

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.
Avatar of Invariant

ASKER

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.
Avatar of Kyle Abrahams, PMP
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.


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?
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.