?
Solved

Using strcmp and pointers

Posted on 1998-05-11
10
Medium Priority
?
347 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

839 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