| 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?
|
Posted: Sun Jun 29, 2008 1:23 pm Post subject: Linked List problem |
|
|
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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Sun Jun 29, 2008 2:09 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jun 29, 2008 2:26 pm Post subject: |
|
|
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 |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Mon Jun 30, 2008 3:41 am Post subject: |
|
|
Try doing
temp = new node();
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Mon Jun 30, 2008 10:54 am Post subject: |
|
|
I think the problem comes from
| Code: | while(temp2->nxt != NULL) {
|
After you cin everything, set temp.nxt to NULL.
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Jun 30, 2008 1:47 pm Post subject: |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Jun 30, 2008 5:08 pm Post subject: |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Jun 30, 2008 5:15 pm Post subject: |
|
|
| oib111 wrote: | | That looks like a vectors, if I'm not mistaken. |
It's a linked list version of a vector.
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jul 01, 2008 8:49 am Post subject: |
|
|
| 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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Jul 01, 2008 12:30 pm Post subject: |
|
|
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 |
|
 |
|