• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ Deleting from linked list problem

Joined
May 19, 2007
Messages
8
I wrote a function called Delete(), which is supposed to take in a name of a person, and delete it from the list. For some reason it doesn't work. When I try debugging it, I get that there is a problem here:

Code:
return (compare(0, _Mysize, _Right._Myptr(), _Right.size()));

First of all, I don't know what that means. Second of all, I don't know what I'm doing wrong.

Here's the structure:
Code:
struct Air
{
	string person;
	Air *next;
};
---------------------------------------------------------
Here's my function:

Code:
void JGOp::Delete()//Doesn't work
{
	Air *previous;
	Air *current;
	string input;
	
	cout << "Select person to delete: ";
	cin >> input;

	current = head;
	previous = head;
	
	current = previous->next;
	while(current->person != input)
	{
		current = current->next;
		previous = previous->next;
	}
	
	current->next=NULL;
	previous->next = current->next;
	delete current;

}//Deletepassenger

Can anyone see what is wrong with my function?

Any help would be appreciated.
 
Colossusx10 said:
When I try debugging it, I get that there is a problem here:
I hope you'll excuse my blunt feedback and trust that it will be taken in the spirit that it's intended.

I know you're just starting out, but you need to very rapidly develop two skills. First, you need to learn how to ask for help. "I get that there is a problem here" doesn't tell us anything. Are you debugging your project, or building it? Do you get a specific error message? Which version of which tools are you using? On which version of which operating system?

I think that you're getting a seg fault or a GPF on that line of code. Your debugger should tell you this much. You need to learn how to use your debugger. It can help you step through your program and let you compare the way the program is actually working with the way you expect that the program works. This will teach you what's really happening in short order. It will also reveal to you why your assumptions might be wrong. In either case, once you've got even a small amount of skill with using the debugger, you'll be able to make progress far faster than asking anyone for help.

In this specific case, you mention the compare() function. Doubutlessly, this is where your problem manifests itself -- but it's also drawn a blind guess from someone who doesn't know what they're doing, and that might waste some of your time as you go off and investigate it, further delaying you from sucess.

Here's my function:

Your code has two obvious problems.

First, the loop invariant lets you walk off the end of your list. From other code you've posted, I'm sure you know what marks the end of a linked list. But you're never testing for the end of the linked list here.

Next, you're using "current" and "previous", and it's confusing you. You initialize them equally. Then, you adjust current to be the next item. What if there are no items in the list? What if there is only one? How does this code affect the loop invariant?

I hope that helps. As usual, we can't just give you the answer -- you'd learn nothing. Even as it stands, with the help I've given you, you're not learning the most important skill of all: debugging your own code.
 
Here's something I'm seeing (bear with me I haven't done C++ in like 7 years)

Code:
current->next=NULL;
previous->next = current->next;
delete current;

I won't point it out explicitly, but there's something wrong here...
(I hope you can see it, if not i'll be glad to detail it.)

Of course, this is all an aside to what mikeblas already touched on.

Also, and this just might be my style of coding... but why are you doing input / output inside your function?
Personally I'd handle that outside, then call the function passing in my user input as a parameter.
 
Back
Top