Pointers problem

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Bit of a C++ prob not sure if it's something the compiler is doing or something in my code.

Code:
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";

Basically my while loop is reading the file structure data and saving it in tmp->key and tmp->recordnum. I haven't finished the binary tree structure that I want it to insert the data into, but so far it puts the first entry that it reads into head->key and head->recordnum. The problem is that once it has finished reading the data, the head now points to the last structure that it read instead of the first. Somehow it's getting tmp and head confused.

It's a real mind fuck, hope someone can help.
 

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Pic of code in case it helps.

pointers.jpg
 

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Tried moving the building of the binary tree into a new function and passing the index data through a dummy variable but doesn't make any difference.

Weird thing is, the first time I enter the data into the file and run the function it works fine, the head contains the correct data. But when I close the program and execute it again the head just returns what is left in tmp.
 

sibanac

Fledgling Freddie
Joined
Dec 19, 2003
Messages
824
Cask,
first off all my c++ is rusty as hell, so this might be crap, but :

Where is head declared ?
 

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Sorry should have posted the whole program. head is a global variable of type index. index is a node structure for my binary tree (that I haven't made yet).

Here's the whole prog. Should compile ok.

Code:
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>

using namespace std;

// declare structure of nodes in database
struct index {
		char key[20];
		int recordnum;
		index *left;
		index *right;
	};

typedef struct {
				 int ordernum;				 
				 char name[20];
				 int orderdate;
				 char productcode[7];
				 char description[20];
				 char deliverydate[9];
				 int quantity;
				} order;
				

order order1;


// sets variables of type Link as pointers to index entries
typedef index *Link;

//declare functions
void addorder();
void buildtree(index *tmp);
void search();
void displayindex();
void updateindex();
void inorder(index *p);

Link head = NULL;

int ordertotal = 0;

void main() {
	updateindex();
	int option;
	cout << "\n<<< Main Menu >>>\n\n1.Add a new record to the database\n2.Search database\n3.Display Index\n4,Quit\n\nSelect: ";
	cin >> option;
	cin.get();
	while (option != 4) {
		switch(option) {
			case 1 : addorder();
			break;
			case 2 : search();
			break;
			case 3 : displayindex();
			break;
			default: cout << "\nInvalid choice.  Please try again\n";
			break;
		}
	cout << "\n<<< Main Menu >>>\n\n1.Add a new record to the database\n2.Search database\n3.Display Index\n4,Quit\n\nSelect: ";
	cin >> option;
	cin.get();
	}
}

void addorder() {
	ofstream dataFile; 
	time_t systime;
	struct tm *p;
	string name,code,description;
	int d,m,y;
	time(&systime);					//
	p = localtime(&systime);		//	gets system date
	y = p->tm_year + 1900;			//  and loads it into
	d = p->tm_mon * 1000000;		//	variables
	m = p->tm_mday * 10000;			//
	
    dataFile.open("orderfile.txt", ios::app);
	order1.ordernum = ordertotal + 1;
	cout << "\nEnter Client Name: ";
	cin.getline(order1.name,20);
	order1.orderdate = d + m + y;
	cout << "\nEnter Product Code: ";
	cin.getline(order1.productcode,7);
	cout << "\nEnter Order Description: ";
	cin.getline(order1.description,20);
	cout << "\nEnter Delivery Date (ddmmyyyy): ";
	cin.getline(order1.deliverydate,9);
	cout << "\nEnter Quantity: ";
	cin >> order1.quantity;
	cin.clear();
	
	cin.ignore(999999,'\n');

	dataFile.write(reinterpret_cast<const char*> (&order1), sizeof(order));
	dataFile.close();

	updateindex();
}

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;
		buildtree(tmp);
		inputPathway.read(reinterpret_cast<char*> (&order1), sizeof(order));
		ordNo++;
		ordertotal = order1.ordernum;
	}
	cout << "\nThere are " << ordNo << " orders on record\n";
	inputPathway.close();
}

void buildtree(index *tmp) {
	if (head == NULL)	// first node in a new index
		head = tmp;		//	is placed at head
	/*else {
		if ((head->right == NULL) && (strcmp(head->key,tmp->key)<=0))
			head->right = tmp;
		if ((head->left == NULL) && (strcmp(head->key,tmp->key)>0))
			head->left = tmp;
	}*/
}

void displayindex() {
	inorder(head);
	cout << "\n\n";
}

void inorder(index *p) {
	if (p != NULL) {
		inorder(p->left);
        cout << "\n" << p->key << "\t" << p->recordnum;
        inorder(p->right);
	}
}


void search() {

}
 

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Ahhhhhhh,

I posted this on about 5 forums and a guy pointed out that I had left the creation of tmp outside of the while loop so head was just pointing to the first instance.

Was all ready to scrap my entire program and start over, whew.
 

sibanac

Fledgling Freddie
Joined
Dec 19, 2003
Messages
824
Cask said:
Tried moving the building of the binary tree into a new function and passing the index data through a dummy variable but doesn't make any difference.

Weird thing is, the first time I enter the data into the file and run the function it works fine, the head contains the correct data. But when I close the program and execute it again the head just returns what is left in tmp.

sounds like the init of head is screwed up
 

Wij

I am a FH squatter
Joined
Dec 23, 2003
Messages
18,404
Where did you declare head ? Can't see it.

Where do you change tmp to point to another element ?
 

Cask

Fledgling Freddie
Joined
Dec 27, 2003
Messages
653
Sok mateys it's all working now. Head is declared up at the top as type Link which is a pointer to my index structure.

This guy on this programming forum told me to move the creation of tmp into the body of the while loop, that seemed to work so I finished writing the rest of the binary tree. Then every man and his dog started ripping into every little cosmetic problem with my code which was depressing.

Thanks tho :)
 

Danya

Fledgling Freddie
Joined
Dec 23, 2003
Messages
2,466
Cask said:
Then every man and his dog started ripping into every little cosmetic problem with my code which was depressing.
LOL, yeah that happens. ;)

TBH it doesn't look that bad to me - I don't much care for your {} style, but that's about it. Also it's usual to use endl rather than \n with cout because it guarantees a flush.
 

Users who are viewing this thread

Top Bottom