Solved

Using strcmp and pointers

Posted on 1998-05-11
10
338 Views
Last Modified: 2010-04-10
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;

}
0
Comment
Question by:John500
  • 7
  • 2
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1175688
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175689
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.
0
 

Expert Comment

by:jbush
ID: 1175690
jbush,
    This probably is a homework question.  You should not be providing complete answers to schoolwork questions.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 22

Expert Comment

by:nietod
ID: 1175691
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
0
 

Author Comment

by:John500
ID: 1175692
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175693
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.  
0
 

Author Comment

by:John500
ID: 1175694
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.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1175695
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175696
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175697
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)

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

776 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