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

x
?
Solved

Comparing char's

Posted on 2005-03-27
6
Medium Priority
?
199 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:JayTreDoe357
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

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

                    if (strcmp(sartist, artist) == 0)
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 13641785
if u r searching for matching partial strings... look at

strstr()...


0
 
LVL 3

Accepted Solution

by:
r_a_j_e_s_h earned 500 total points
ID: 13641788
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 (strstr(sartist, artist) != NULL)
                    {
                         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);
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:JayTreDoe357
ID: 13643551
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
0
 
LVL 15

Expert Comment

by:efn
ID: 13644398
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?
0
 

Author Comment

by:JayTreDoe357
ID: 13644458
artist is already declaired in the beginining of the program, i am just setting it to nothing at that point.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

571 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