?
Solved

Comparing char's

Posted on 2005-03-27
6
Medium Priority
?
196 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
[X]
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
  • 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
Independent Software Vendors: 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: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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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

777 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