Hi ive made a code where a user makes a list and then displays it. he may also delete objects one by one.
suppose i wished to clear all in one go. How would i go about doing this? thank you in advance. my code is below. all works except the clear all option does nothing.
#include <iostream>
using namespace std;
struct node {
string pro[10];
string product;
node *link;
};
node* buildListForward()
{
node *first,*newNode, *last;
string num;
cout<<"\nEnter Products\nEnter 'quit' when done\n";
cin >>num;
first=NULL;
while(num!="quit")
{
newNode = new node;
newNode->product =num;
newNode->link = NULL;
if(first==NULL)
{
first = newNode;
last= newNode;
}
else
{
last->link = newNode;
last = newNode;
}
cin>>num;
}
return first;
}
node* deleteNode(node *head,string delItem)
{
node *current,*prev;
bool found=false;
current = head;
prev = head;
while(current!=NULL && !found) {
if(current->product==delItem) found = true;
else {
prev = current;
current = current->link;
}
}
if(found) {
if (current==head) head = current->link;
else
prev->link = current->link;
delete current;
}
return head;
}
int main() {
node *head, *current;
string num;
int pos;
int menu;
bool quit=false;
do{
cout<<"1.....Build List"<<endl;
cout<<"2.....Delete Item"<<endl;
cout<<"3.....Display"<<endl;
cout<<"4.....Logout"<<endl;
cout<<"5.....Quit"<<endl;
cout << "Enter choice: ";
cin >> menu;
switch(menu){
case 1:cout<< "\nBuild List"<<endl;
head = buildListForward();
system("cls");
break;
case 2:cout<< "\nDelete Item"<<endl;
cout<<"Enter number to delete\n";
cin>>num;
head = deleteNode(head,num);
system("cls");
break;
case 3:cout<< "\nDisplay Item"<<endl;
current = head;
while(current!=NULL) {
cout<<current->product<<endl;
current = current->link;
}
system("pause");
system("cls");
break;
case 4:cout<<"Clear All";
system("cls");
break;
case 5:quit=true;
break;
default:cout<<"Invalid entry must be 1..5"<<endl;
}
}while(!quit);
system("pause");
return 0;
}