Hey, I'm just wondering if it's possible to run an if statement through a class? The thing is, I wanna store users details and then when you search for a user, i.e "Phill" it returns all the information on that user. I'm using something like:

c1.GetName();

So something like:

for(int i=0;(i<100); i++)
{

   if(c[i].GetName() = "Phil")
   {
     // do this
    // do that
   }else{
   // do this
  // don't do that
  }

Is it possible or are there any other ways? Thank you.

Yes, but change if(c[i].GetName() = "Phil") to if(c[i].GetName() == "Phil") . That should solve your compile-error :)

[edit]
And C has to be an array (or vector ) of classes for thiss to work!

Edited 7 Years Ago by Nick Evan: n/a

Hey, I don't understand how I can make C into an array when C is defined as:

Customer c1, c2, c3;

Any suggestions? I'm new to OO

Backing up what niek_e said :

You have used c[i].GetName() in your for loop, you can use c.<function name> only if the classes are declared as Customer c[3] or something else you should use the functions as
c1.<function>();
c2.<function>();
c3.<function>();
separately as c1.function(); is completely different from c[1].function();

Ok and now for comparison of two strings you can use "==" as niek_e suggested (your mistake is using "=" instead of "==" )as it has been overloaded in the standard namespace.I hope now you understand.

Edited 7 Years Ago by csurfer: n/a

Yes, but change if(c[i].GetName() = "Phil") to if(c[i].GetName() == "Phil") . That should solve your compile-error :)

[edit]
And C has to be an array (or vector ) of classes for thiss to work!

Be sure use strcpy when comparing cStyle String,

if( strcmp( ArrayOfClasses[i].getName(), "phil") == 0 ) { cout <<" match\n"; }

Be sure use strcpy when comparing cStyle String,

if( strcmp( ArrayOfClasses[i].getName(), "phil") == 0 ) { cout <<" match\n"; }

You're right. But this is the C++ forum and I strongly recommend people to use std::strings instead of char-arrays in C++. There's a good reason why they were invented ;)

This article has been dead for over six months. Start a new discussion instead.