Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Error Comparing Strings

Posted on 2003-02-23
6
Medium Priority
?
211 Views
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.
0
Comment
Question by:Invariant
  • 3
  • 2
6 Comments
 

Expert Comment

by:eranborovik
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.
0
 

Author Comment

by:Invariant
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.
0
 
LVL 41

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

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
Industry Leaders: 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!

 

Author Comment

by:Invariant
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?
0
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 150 total points
ID: 8009988
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
 

Author Comment

by:Invariant
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.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

577 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