Link to home
Start Free TrialLog in
Avatar of JayTreDoe357
JayTreDoe357

asked on

Comparing char's

I was wondering if there was a way to compare char arrays. For example if I have...

...
artist[25]
...


void search (void)
{
      cout << "What is the name of the artist to search for? ";
      cin >> sartist;
      matches = 0;
      artist = "";
      FILE *cfptr;

      cfptr = fopen(fName, "r");
            
            do
                  {
                        if (strcmp(sartist, artist) = 0)
                        {
                              cout << "Match Found for " << sartist << ": " << album << " " << year << "\n";
                              matches++;
                        }
                        fscanf ( cfptr, "%s%s%s", album, artist, year);
                  }while ( !feof ( cfptr ) );
            
            cout << matches << " found\n";
            fclose(cfptr);
}


is there any what I can run a search to see if "artist" is equal to "sartist", they are not const so I don't know of any way.

Thanks in advance,

Jason
Avatar of efn
efn

strcmp should work.  But you need two equals signs for comparison rather than assignment:

                    if (strcmp(sartist, artist) == 0)
if u r searching for matching partial strings... look at

strstr()...


ASKER CERTIFIED SOLUTION
Avatar of r_a_j_e_s_h
r_a_j_e_s_h

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
Avatar of JayTreDoe357

ASKER

c:\Documents and Settings\EliTe\My Documents\Visual Studio Projects\Project3\Project3.cpp(144) : error C2440: '=' : cannot convert from 'const char [1]' to 'char [25]'

This is what I get as an error when I do that
That error message looks like it might be from the line

artist = "";

where artist is declared

char artist[25];

You can't assign to an array like that.  To set it to an empty string, you can store a null characterin the first position, like this:

artist[0] = 0;

or

artist[0] = '\0';

Did you try the correction I suggested before?
artist is already declaired in the beginining of the program, i am just setting it to nothing at that point.