ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
ok im working on this project with linked lists for my C++ class and the point of the program is to load data from a text document and create a linked list from that data which resembles the train. The linked list is a chain of Node objects (a class i defined which contains a pointer to the next node in the linked list and a car class (which contains a type of data and the amount of data numbers)). Heres my dillemma: my schools CS website appears to be down so i cant access any of the examples or anything, im supposed to have data input from another text document so that the train stops at various "stations" and buys and sells some of its stuff. im starting on the Sell function and so far all im getting is a seg fault error. i have a good idea what to do next, but i want the seg fault to go away before i start doing anymore things. heres my code so far for the sell function:
void Train::SellItems(std::string typeitem, int numitems){
Node *prev, *cur;
prev=front;
cur=prev->link;
for (int i = 0; i < size-1; i++){
while (numitems!=0){
if (cur->car.GetType() == "typeitem"){ //****************
if (typeitem=="grain"){
}
//more empty if statements are here...
}
prev=prev->link;
cur=cur->link;
}
}
}
the error occurs where i have the commented *******, can anyone tell me why i cant do that? (GetType() is a public member function) thanks
It's hard to tell based on the few lines posted, but I do see one possible problem.
When you execute this statement
Code:
prev=front;
cur=prev->link;
I'm assuming that prev becomes the first node in the link and cur becomes the second. What if there is no second??? You should check to see if the link is null before trying access data at the next node, like below.
Code:
if (cur->car.GetType() == "typeitem"){ //****************
hmm ok, i think ill try to get through the list using a for loop, ill just have to rethink my logic a little. the way i had it before had too much annoying stuff.
It's been a while since I constructed a list. I just remember how to do it in C, and not C++ because I'm not familar with C++ file I/O, especially fsteams. Maybe this will help in some way.
I remember once that I had made a Standard C++ class with a private linked list. The self referential structure was a member of the implementation and the list methods contained the behavior for adding, sorting, and deleting nodes. I have a final exam next wednesday otherwise I'd try to figure out how I made that class. It was cool.
Try and post a better discription of the problem, along with struct/class definitions. It's kinda hard to help when you've only posted a small piece of the program.
Also, try and use some kind of common indentation scheme like gtkuser uses, it's sooooo much easier to read.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.