Evan tho i use corecet traverse condition while (p != NULL)
the program does additinoal rotation printing nothing usefull,the issue is in case 4 of the switch in displayproduct function called from unicDispaly function, here is the cut down code:
#include <iostream>
using namespace std;
struct product {
char name[50];
int quantity;
};
struct productlist {
product x;
productlist* next;
};
struct drug {
int Unum;
char label[50];
float Uprice;
char category[50];
productlist* Lproduct;
};
typedef struct tree {
drug x; // Left child (pointer to tree)
tree* Lch;
tree* Rch;
} tree;
productlist* createproductlist(product x){
productlist*l=new productlist;
l->x=x;
l->next=NULL;
return l ;
}
tree* createdrugtree(drug x) {
tree* t = new tree; // Dynamically allocate memory for the tree
t->x = x; // Initialize the drug element
t->Lch = NULL; // Set left child as NULL
t->Rch = NULL; // Set right child as NULL
return t; // Return the tree
}
product createP(){
product x;
cout << "Enter product namen";
cin>>x.name;
cout << "Enter product quantityn";
cin>>x.quantity;
return x;
}
}
bool searchUnic(tree* &t,int unum){
bool found=false;
while(t!=NULL){
if (t->x.Unum == unum) {
found=true;
}
else if (unum < t->x.Unum) t=t->Lch;
else if(unum>=t->x.Unum)t=t->Rch;
if(found==true){
break;
}
}
if(found==false){
cout<<"drug not foundn";
return found;
}
return true;
}
}
void displayproduct(tree* t){
if (t->x.Lproduct == NULL) {
cout << "No products associated with this drug.n";
}
cout << "Product details:n";
productlist* p = t->x.Lproduct;
while (p != NULL) {
cout << "Name: " << p->x.name << ", Quantity: " << p->x.quantity << "n";
p = p->next;
}
}
void unicDispaly(tree* t,int unum){
tree* T=t;
searchUnic(T,unum);
cout<<"the lable:"<<T->x.label<<"n";
cout << "the drug unit Price:";
cout << T->x.Uprice<<"n";
cout << "the drug Category:";
cout <<T->x.category<<"n";
cout<<"the products:";
displayproduct(T);
}
int main() {
tree* t=NULL;
int choice;
do {
cout << "nPharmaceutical Management Systemn";
cout << "1. Add Drugn";
cout << "2. Add Products to Drugn";
cout << "3. Display All Drugsn";
cout << "4. Search for a Drugn";
cout << "0. Exitn";
cout << "Enter your choice: ";
cin >> choice;
int unum;
switch(choice){
case 1:
addTree(t,createD());
break;
case 2:
cout<<"enter unic number:";
cin>>unum;
addproduct(t,unum);
break;
case 3:
cout<<"the tree:";
display(t);
break;
case 4:
cout<<"enter unic number:";
cin>>unum;
unicDispaly(t,unum);
cout<<"the drug:";
}
}while(choice!=0);
return 0;
}
here is the console
Pharmaceutical Management System
1. Add Drug
2. Add Products to Drug
3. Display All Drugs
4. Search for a Drug
0. Exit
Enter your choice: 1
Enter drug unique number
1
Enter drug lable
1
Enter drug unit Price
1
Enter drug Category
1
Pharmaceutical Management System
1. Add Drug
2. Add Products to Drug
3. Display All Drugs
4. Search for a Drug
0. Exit
Enter your choice: 2
enter unic number:1
Enter product name
11
Enter product quantity
11
Pharmaceutical Management System
1. Add Drug
2. Add Products to Drug
3. Display All Drugs
4. Search for a Drug
0. Exit
Enter your choice: 4
enter unic number:1
the lable:1
the drug unit Price:1
the drug Category:1
the products:Product details:
Name: 11, Quantity: 11
Name: , Quantity: 0
Process returned -1073741819 (0xC0000005) execution time : 22.681 s
Press any key to continue.
so i am biasicly trying to do a pharmaceutical drug managing program for this assiment and the parts I’m having trouble with is displaying the elements of a linked list , so I set the exiting condition for the while loop to while (p != NULL),but it still run 1 time after p=null
17