hi all.
I have a question need your all help.
1.what is the problem for my delete function,I can't delete the list that i select(but no error in that case)
2.how to build the search function with that prototype?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

struct ProductNode{
	char code[5];
	char description[30];
	double price;
	ProductNode *Next;

typedef ProductNode *Productptr;

void Display(Productptr );
Productptr Search(Productptr, char []);
void Delete(Productptr *, char []);
void Insert(Productptr *, ProductNode);

	Productptr PhoneList,temp;
	ProductNode p;
	int sel;
	char id[5];

	PhoneList = NULL;
	while (sel != 5){

		cout<<"\t1. Add Item."<<endl;
		cout<<"\t2. Add Delete."<<endl;
		cout<<"\t3. Display Item."<<endl;
		cout<<"\t4. Search."<<endl;
		cout<<"\t5. Exit"<<endl;
		cout<<"\t? : ";

		switch (sel){
			case 1: cout<<"Item Code :";
				    cin>>p.code ;
					cout<<"Description : ";
					cout<<"Price : RM ";
				    Insert(&PhoneList, p);

			case 2: cout<<"Item Code :";
				    cin>>id ;
					Delete(&PhoneList, id);

			case 3: Display(PhoneList);

			case 4: cout<<"Enter item's Code : ";

					temp =  Search(PhoneList, id);
					if (temp == NULL)
						cout<<"Error : Item not Found."<<endl;
			//			cout<<"====\t===========\t=====\n";
			//			cout<<temp->code<<"\t"
			//				<<temp->description<<"\t\t"
			//				<<temp->price<<endl;
			//			}
	return 0;

void Insert(Productptr *h, ProductNode p){
	Productptr newNode, pre, cur;
	bool found = 0;
	newNode = new ProductNode;

	if (newNode == NULL){
		cout<<"Error : Insertion Failed.";
	}else {
		newNode->Next = NULL;
			*h = newNode;
			while(cur!=NULL) {
				if (strcmp(cur->description, p.description )<0) {
					break;	}

				newNode->Next = *h;
				*h = newNode;
				newNode->Next = cur;
				pre->Next = newNode;

void Delete(Productptr *r, char id[5])
	Productptr *temp;
	Productptr *temp2;
	bool found;
		cout<<"Cannot delete from and empty list."<<endl;
		found= false;
		while(temp!=NULL && !found)
			if (strcmp((*temp)->code,id)<=0)
				cout<<"Item to be deleted is not in the list."<<endl;
						delete temp;
							delete temp;
						cout<<"Item to be deleted not in list."<<endl;
	void Display(Productptr t)
	while(t!= NULL)
		cout<<t->code<<" "<<t->description<<" "<<t->price<<endl;
	cout<<"End of the list"<<endl;

You are having problems dealing with pointers.

I know this sounds stupid, but you need to get out a piece of paper and draw yourself a linked list, with little arrows pointing from element to element.

Something else that might help you is to use references instead of pointers when you can. void Insert( Productptr &head, ProductNode p ) Call it with: Insert( PhoneList, p ); And inside use it normally: if (head == NULL) head = newNode etc.

Yes, a reference is really a pointer under the covers, but if you use it you won't have to worry about two levels of indirection (which is what is causing you to mess up in Delete()).

Secondly, you are using cin >> to get strings from the user. Some professors continue to teach that but it is wrong (because at some point the user will have a space in his input --particularly for something like "description"). Also, your strings are char arrays. What happens if the user inputs a code longer than 4 characters? Or less? How will you know? How will you keep it from messing up the input for description?

The most robust fix is to use a std::string for all user input. Say:

#include <sstream>

// The temp string to use for all our input
string s;


// Get the 4-digit code
cout << "Item code : ";
getline( cin, s );
// Validate it
if ((s.length() < 1)
or  (s.length() > (sizeof( p.code ) /sizeof( char )))) {
  cout << "Invalid item code. Must be one to four characters." << endl;
// Copy it to our temp input node
memset( p.code, '\0', sizeof( p.code ) );
s.copy( p.code, (sizeof( p.code ) /sizeof( char )) -1 );

// Get the description
cout << "Description : ";
getline( cin, s );
// Copy it to our temp input node
memset( p.description, '\0', sizeof( p.description ) );
s.copy( p.description, (sizeof( p.description ) /sizeof( char )) -1 );

// Get the price
cout << "Price : ";
getline( cin, s );
if (!(stringstream( s ) >> p.price)) {
  cout << "A price must be a number like 12.99." << endl;

// Append the new node to the list
Insert( PhoneList, p );

The sizeof() /sizeof() trick is just so your code will work no matter if you change the length of the char arrays in ProductNode. The other alternative is just to use std::strings in ProductNode instead of char arrays.

I know this seems like a lot, but when you get into the habit then there is nothing your user can type that will cause your program to malfunction. (It seems to me that getting all that input from the user would also make a good function. You would only need to return the new node, which the switch statement in main could then Insert() into the linked list.)

Finally, you might think I have ignored your requests for help. I haven't. Draw some stuff and see if you can't fix it yourself. Post again with what you get after going through that exercise.

Here's a hint: Write your Search() function first, then use it in Delete() to find the node to remove.

Hope this helps.

The delete function someone help me check.Thx for your comment about my program.
But the delete function i can't delete that item i want delete.Please tell me about my problem . Thx.

How about you first make an effort to solve your problem with the suggestions I gave you? If you do the exercise I suggested and work in the order I suggested then you'll learn something useful instead of my simply giving you code. At the very least you'll be able to pass your exams.

This article has been dead for over six months. Start a new discussion instead.