Solved

Using strcmp and pointers

Posted on 1998-05-11
10
342 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
[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
  • 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
Industry Leaders: 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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

733 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