Re: Problem with a function
Please provide enough that we can compile and test - hunting an error out by reading your code is an interesting mental exercise, but I'm not that awake yet today.
Re: Problem with a function
Quote:
Can someone tell me what I am doing wrong here?
Two things....
1) you are not using the debugger to analyze what is going on.
2) Yoiu are posting incomplete information which makes it impossible for anyone but a mind reader to help you...
Re: Problem with a function
Alright, I have edited the post. I hope it made the problem more clear. If not, let me know I'll add more details. Thanks
Re: Problem with a function
You should learn how to use the debugger. I would put a breakpoint in the code (right after the cin >>) then step my way through the code using F10/F11 until the application fails. Then analyze the variable etc. trying to determine what went wrong.
Remember to clean up your resources. For every new (or new[]) there should be a delete (or delete []).
- petter
Re: Problem with a function
I don't really know how to debug. That's why I was hoping someone could tell me what's wrong.
Re: Problem with a function
Quote:
Originally Posted by Colox10
I don't really know how to debug. That's why I was hoping someone could tell me what's wrong.
As the old saying goes "give a man a fish, and you feed him for a day. Teach a man to fish, and he is fed for a lifetime".
The debugger is very easy to use. Just hit F10 and single step through the program. It is a requirement for any programmer to know how to debug a program. Without this knowledge, how will you be able to write any programs and know what's wrong if something doesn't work correctly?
You won't be able to post problem after problem without a few us getting frustrated that you have not debugged the program yourself to figure out what's wrong. That's why you need to learn to fish (debug), and not have us give you the fish.
See the homework FAQ here, and read the second entry:
http://www.codeguru.com/forum/showthread.php?t=366302
Regards,
Paul McKenzie
Re: Problem with a function
Well the debugging really isn't that bad, just like you said. However I still don't know what the problem is.
Re: Problem with a function
Quote:
Originally Posted by Colox10
Well the debugging really isn't that bad, just like you said. However I still don't know what the problem is.
1)You never stated what the error is.
2)What line of code causes the error to occur? Did you single step through the program using F10 and F11 (step into a function)? I highly doubt that inputing into a string causes an error.
3) You didn't provide a main() function that shows us how, when, and where you're using your classes.
Regards,
Paul McKenzie
Re: Problem with a function
First of all I want to thank you Paul for answering my posts. This is my first time ever posting on a forum, so I am not quite sure how to do it most effectively.
Also, I added the main function.
I did go through every line, and I don't think inputin into the string is the problem.
The problem is that when I run it and follow the program, after inputing the name and hitting enter, I get a pop up of Internet Explorer, saying the program had to close. I have no idea what that means.
I hope this information makes it clearer.
Re: Problem with a function
Code:
if (head == NULL)
{
head = ptr->next; // ptr->next isn't initialized. head now points to garbage
}
current = head; // assign head (which is garbage) to current
previous = head; // assign head (which is garbage) to previous
if(ptr->person < current->person) // crash because current is garbage
- petter
Re: Problem with a function
Hi
How did u even compile it
I see that Add is not a method of the class JGOp
but u define it as JGOp::Add
have you missed it ?
Regards
Re: Problem with a function
yes i think you fail to initialise the head struct in the constructor !!!
which leads to current and previous being junk
Code:
JGOp::JGOp()
{
size = 0;
head = NULL;
}
.....
if (head == NULL)
{
head = ptr->next;
}
current = head;
previous = head;
......
may be you meant
Code:
if (ptr == NULL)
{
head = ptr->next;
}else
{
head = ptr;
}
current = head;
previous = head;
even then your code will crash at
Code:
while(current != NULL)
{
current = current->next;
previous = previous->next;
}
Re: Problem with a function
Quote:
Originally Posted by Colox10
First of all I want to thank you Paul for answering my posts. This is my first time ever posting on a forum, so I am not quite sure how to do it most effectively.
Also, I added the main function.
I did go through every line, and I don't think inputin into the string is the problem.
So again, what function was called when the error occurred? If you really debugged line-by-line, you will see that the problem is in the function Add(). If you put a breakpoint in that function and single-stepped into it, you will see that things are not right, as others have pointed out.
Quote:
The problem is that when I run it and follow the program, after inputing the name and hitting enter,
This is proof you are not going through the code line by line. The next function that is called after you input a value is Add(). Did you debug this function?
Regards,
Paul McKenzie
Re: Problem with a function
Re: Problem with a function
Quote:
Why not use std::list?
I guess it is a school assignment. Hopefully this is a data structures class, where the student must understand linked lists and usually are assigned to code their own.
Otherwise, I would agree to use std::list, as the assignment seems to be to write an airline passenger list program, not a linked list program.
Regards,
Paul McKenzie