Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

Using strcmp and pointers

Greetings,

Below is a function a can't debug.  The program crashes each time it leaves the for loop which is intended to traverse the doubly linked list.  This search func receives a pointer (which is an array holding the desired name) from the driver program.  "Name" is a private member of the Student class and is dynamically created in the Student constructor.  The private declarations for the Student class are:
private:
            long _id;
            char *_name;
            int _age;
            float _cum;
            char *_born;
            char *_note;
            Student *_prev;
            Student *_next;

Below is the function troubling me.  Thanks much for your help!!


Student *Database::searchStudent(char *name)
{
        // If nothing passed
      if(!name) {cout<<"Bad entry!"; return 0;}            
        //  If no list
      if(!_head)                                                                  
            { cout << "Empty list!";
              return 0;
            }


    for (Student *p = _head; p !=0 && strcmp(p->_name,name)!= 0; p= p -> _next);
            
                // If second condition applies
            if (strcmp(p->_name,name)==0)
                  {
                        cout <<"Name found: ";
                        print(p);
                  }
                // If first condition applies
            else
                  if(p)
                  {
                        cout << "Name not found!\n";

                  } return 0;

}
Avatar of nietod
nietod

answer coming.
First of all, since this is a homework assignment I will give you advice, but I won't give you the answer, but it doesn't look like you much help anyways.

Let me ask you a question.  There are two ways to end the for loop (either side of the and can fail).  What happens in each case?  (Trace the code in your head after the loop ends.)

If you want more help, I can give it, but I don't think you need much more.
jbush,
    This probably is a homework question.  You should not be providing complete answers to schoolwork questions.
jBush's answer helped because it gave me something to consider.  He provided an alternative should I choose not to discover why the original problem is occuring.

Thanks
Avatar of John500

ASKER

If you don't figure out why the original problem occured now, you will see it again.  I can almost garauntee it.  It is one of the most common problems.

As for the for loop issue JBush is right, sort of, you code would have worked fine, but it is a generaly accepted practice to use for loops only in loops that have numerical counters controling them.  It would be a little confussing to other programers to see it that way.  However, it would work fine.
As was said by nietod, it would be good to learn what the answer is to my original guestion but I can only stair at a compilers answer so long.  nietod also puts himself in a position where he feels he knows how much time I have put into this problem evidenced by his decision to make me wait longer before I can have the next morsel.

nietod's answer states that the most common problem is occuring but feels it will be a sin to point it out.

Rather than write a book in my first question to assure the expert I had covered all avenues known to myself, I decided to save the reader alot of reading and get straight to the point.  Otherwise I could have said that I have a duplicate function that finds each student by id and which works fine.  The only difference is that a pointer is not passed as a parameter and the strcmp is not used.

Also, removing the semicolon from the end of the for loop does not eleminate the problem in case this was assumed.  
Avatar of John500

ASKER

Alright, I give up.

The for loop

 for (Student *p = _head; p !=0 && strcmp(p->_name,name)!= 0; p= p -> _next);

can end in two ways.  The first way is if you reach the end of the list.  In that case p will be NULL (0).  Then you execute the if statement

 if (strcmp(p->_name,name)==0)

but if p is NULL then this p->_name will cause a crash.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

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
Sorry about the double answer.  EE developed a bug last week.  You should be able to see one of the two answers I posted.

How to fix this?  jbush's answer was pretty good, but I prefer.

Student *p = _head;

while (p)
{
   if (strcmp(p->_name,name) !=  0)
      break;
   p = p -> _next);
}

if (p)
{
    cout << "Name found: ";
    print(p);
}
else
{
    cout << "Name not found!\n";
}
return 0;

Any questions?
John found a bug in my last code sample.  The if statement inside the while loop should read

if (strcmp(p->_name,name) ==  0)

not

if (strcmp(p->_name,name) !=  0)