Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Linked List problem

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jun 29, 2008 1:23 pm    Post subject: Linked List problem Reply with quote

I'm making a program to manage item information for a company but the problem is on the third time it adds something it gets a runtime error and I can't figure it out. Here's my code.

Code:

void AddNodeEnd() {
   node *temp, *temp2;
   temp = new node;
   cout<<"\nName: ";
   cin.getline(temp->name, 255, "\n");
   cout<<"\nPrice: ";
   cin>>temp->price;
   cout<<"\nNumber in stock: ";
   cin>>temp->stock;
   cout<<"\nNumber sold: ";
   cin>>temp->sold;
   if(start == NULL) {
      start = temp;
      current = start;
      temp->nxt = NULL;
   }
   else {
      temp2 = start;
      while(temp2->nxt != NULL) {
         temp2 = temp2->nxt;
      }
      temp2->nxt = temp;
   }
   cout<<"\n\n";
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sun Jun 29, 2008 2:09 pm    Post subject: Reply with quote

When you add a new node, would the nxt be null or just garbage?
Try adding a last/end node pointer instead of just start.
Other than that, linked lists confuse the hell out of me.

_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Jun 29, 2008 2:26 pm    Post subject: Reply with quote

I'm not quite sure what you're saying, but I think I understand the first part:

HalfPrime wrote:

When you add a new node, would the nxt be null or just garbage?


I think what it is is I assign temp2->nxt to equald temp, but I don't set temp->nxt to equal NULL. So it does just point to garbage.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Mon Jun 30, 2008 3:41 am    Post subject: Reply with quote

Try doing

temp = new node();
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Mon Jun 30, 2008 10:54 am    Post subject: Reply with quote

I think the problem comes from
Code:
while(temp2->nxt != NULL) {

After you cin everything, set temp.nxt to NULL.

_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Mon Jun 30, 2008 1:47 pm    Post subject: Reply with quote

Ok,well I have a new problem. It skips the chance to accept user input for the name part. And then after the price part it skips two lines instead of one.


_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Jun 30, 2008 3:01 pm    Post subject: Reply with quote

Since you are using C++, there are already classes for linked lists so you don't have to reinvent the wheel.

http://www.cppreference.com/cpplist/index.html
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Mon Jun 30, 2008 5:08 pm    Post subject: Reply with quote

That looks like a vectors, if I'm not mistaken.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Jun 30, 2008 5:15 pm    Post subject: Reply with quote

oib111 wrote:
That looks like a vectors, if I'm not mistaken.


It's a linked list version of a vector.
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Tue Jul 01, 2008 8:49 am    Post subject: Reply with quote

oib111 wrote:
Ok,well I have a new problem. It skips the chance to accept user input for the name part. And then after the price part it skips two lines instead of one.



Are we supposed to figure out the error from this?

Code:
void AddNodeEnd() {
   node *temp, *temp2;
   temp = new node;
   cout<<"\nName: ";
   cin.getline(temp->name, 255, "\n");
   cout<<"\nPrice: ";
   cin>>temp->price;
   cout<<"\nNumber in stock: ";
   cin>>temp->stock;
   cout<<"\nNumber sold: ";
   cin>>temp->sold;
   if(start == NULL) {
      start = temp;
      current = start;
      temp->nxt = NULL;
   }
   else {
      temp2 = start;
      while(temp2->nxt != NULL) {
         temp2 = temp2->nxt;
      }
      temp2->nxt = temp;
   }
   cout<<"\n\n";
}
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Jul 01, 2008 12:30 pm    Post subject: Reply with quote

Yea. That's the code that's being executed and when I go through it it's perfectly fine, so I'm not sure if I'm missing something or if VC++ is being stupid.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites