Solved

Using strcmp and pointers

Posted on 1998-05-11
10
332 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now