C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 10th, 2005, 05:04 AM
Shmo Shmo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 Shmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem with pointers

Can't get my pointers working. I have head = tmp only when head = NULL, which loads the first piece of data from the file. But when the while loop finishes, head->key now contains the last piece of data in the file. How is the contents of head changing?

URL

Reply With Quote
  #2  
Old January 10th, 2005, 06:10 AM
Shmo Shmo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 Shmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
void updateindex() {
int ordNo;
ordNo = 0;
ifstream inputPathway;
inputPathway.open("orderfile.txt", ios::in);
if (!inputPathway.is_open() ) {
cout << "No orderfile present. A new one will be constructed.\n\n"; // checks if file exists to
return; // prevent infinite loop
} //

Link tmp;
tmp = new index;
inputPathway.read(reinterpret_cast<char*> (&order1), sizeof(order));
while (!inputPathway.eof()) { // build binary tree index here
strcpy(tmp->key,order1.name);
tmp->recordnum = order1.ordernum;
tmp->left = NULL;
tmp->right = NULL;
if (head == NULL) { // first node in a new index is placed at head
head = tmp;cout << head->key << " has been put at head"; }
inputPathway.read(reinterpret_cast<char*> (&order1), sizeof(order));
ordNo++;
ordertotal = order1.ordernum;
}
cout << "\nThere are " << ordNo << " orders on record\n";
inputPathway.close();
cout << head->key << " is now at head";

Reply With Quote
  #3  
Old January 10th, 2005, 06:27 AM
Shmo Shmo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 Shmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Got it.

Someone pointed out that I had left the creation of tmp outside of the while loop, so head was just pointing to the first instance.

Disaster averted!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Problem with pointers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT