Error Comparing Strings

Posted on 2003-02-23
Medium Priority
Last Modified: 2010-04-01
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.
Question by:Invariant
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 8002270
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.

Author Comment

ID: 8003898
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.
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8004363
An array name is a pointer and a constant.  

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


IDptr = ArrayID; // NO &

when you say

IDptr = &ArrayID;

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

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.


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


Author Comment

ID: 8006368
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?
LVL 40

Accepted Solution

Kyle Abrahams earned 150 total points
ID: 8009988
Yup.  You can do that with functions as well
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.


Author Comment

ID: 8011013
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 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