I am currently doing a C++ project for my uni assignments. I came across a bit difficulty to code this using linked list.
#include <iostream>
#include<stdlib.h>
#include <string.h>
#include <iomanip>
#include <conio.h>
#define MAX 100
using namespace std;
class User
{
public:
struct user_details
{
string data1;
user_details *next1;
};
struct user_date
{
string data2;
user_date *next2;
};
struct event
{
string data4;
event *next_event;
};
void push_user (string name, string date,string eventss)
{
user_details *newName;
user_date *newDate;
event *newEvent;
cout <<"\n\nList of Events services offered.\n1.Wedding\n2.Birthday\n3.Funeral\n4.Celebration event\n5.Open House"<<endl;
cin.sync();
cout<<"\nEnter name: ";
getline(cin, name);
cin.sync();
cout <<"Enter date: ";
getline(cin, date);
cin.sync();
cout<<"Write the events(e.g. Course.style-Wedding or Buffet.style-Wedding): "<< endl;
getline(cin, eventss);
if(top1 == NULL|| top2 == NULL||top_event==NULL) //checking for empty stack
{
top1 = new user_details;
top1-> data1= name;
top1->next1= NULL;
top2 = new user_date;
top2->data2=date;
top2->next2=NULL;
top_event = new event;
top_event->data4= eventss;
top_event->next_event= NULL;
}
else
{
newName= new user_details;
newName->data1 = name;
newName->next1 = top1;
top1 = newName;
newDate= new user_date;
newDate->data2 = date;
newDate->next2 =top2;
top2= newDate;
newEvent= new event;
newEvent->data4 = eventss;
newEvent->next_event= top_event;
top_event = newEvent;
cout<<"Added new user to stack."<<endl;
}
}
void pop_user (string name, string date, string eventss)
{
user_details *current1;
user_date *current2;
event *current23;
if(top1 == NULL)
{
cout<<"!!!Stack underflow" << endl;
}
if(top2 == NULL)
{
cout<<"!!!Stack underflow" << endl;
}
if(top_event == NULL)
{
cout <<"!!!Stack underflow"<<endl;
}
else
{
name = top1->data1;
current1 = top1;
top1 = top1->next1;
delete current1;
date = top2->data2;
current2=top2;
top2 = top2->next2;
delete current2;
eventss = top_event->data4;
current23 = top_event;
top_event = top_event->next_event;
delete current23;
cout << "Name: "<< name<<endl;
cout << "Date: " << date<<endl;
cout<<"Event" << eventss << " is removed " << endl;
}
}
void display_user ()
{
user_details *current1;
user_date *current2;
event *current23;
if(top1 == NULL)
{
cout<<"\nEmpty list" << endl;
// return;
}
else
{
current1 = top1;
current2 =top2;
current23=top_event;
while(current1 != NULL || current2 !=NULL||current23 != NULL)
{
cout<<"\nName: "<< current1->data1<<endl;
current1 = current1->next1;
cout <<"Date: "<<current2->data2<<endl;
current2 = current2->next2;
cout<<"Event: " << current23->data4<<endl;
current23 = current23->next_event;
}
}
}
void deleteStack_user()
{
user_details *current1;
user_date *current2;
event *current23;
if(top1 == NULL || top2 ==NULL||top_event==NULL)
return;
else
{
current1 = top1;
current2 = top2;
current23=top_event;
while(current1 != NULL || current2 != NULL||current23 != NULL)
{
top1 = top1->next1;
delete current1;
current1 = top1;
top2 = top2->next2;
delete current2;
current2 = top2;
top_event = top_event->next_event;
delete current23;
current23 = top_event;
}
}
}
void search(string user_name) //linear search
{
user_details *current1;
cout <<"Please enter the name you want to search: "<<endl;
cin.sync();
getline(cin,user_name);
if(top1==NULL)
{
cout<<"not found";
}
else
{
current1 = top1;
while(user_name==current1->data1||current1 != NULL)
{
cout<<"\nName: "<< current1->data1<<endl;
current1 = current1->next1;
cout <<"Found."<<endl;
}
}
}
user_details *top1=NULL;
user_date *top2=NULL;
event *top_event;
};
class reeves
{
public:
struct node
{
string data;
node *next;
};
void push(string menu)
{
node *newMenu;
cout<<"Enter the menu: ";
cin.sync();
getline(cin, menu);
if(top == NULL) //checking for empty stack
{
top = new node;
top->data= menu;
top->next= NULL;
cout<<"\nCreated new menu."<<endl;
}
else
{
newMenu= new node;
newMenu->data = menu;
newMenu->next = top;
top = newMenu;
cout<<"Added new menu to stack."<<endl;
}
}
void pop(string menu)
{
node *current;
if(top == NULL)
{
cout<<"!!!Stack underflow" << endl;
}
else
{
menu = top->data;
current = top;
top = top->next;
delete current;
cout<<"Menu " << menu << " is removed " << endl;
}
}
void display()
{
node *current;
if(top == NULL)
{
cout<<"\nEmpty menu" << endl;
// return;
}
else
{
current = top;
while(current != NULL)
{
cout<<current->data<<endl;
current = current->next;
}
}
}
void deleteStack()
{
node *current;
if(top == NULL)
return;
else
{
current = top;
while(current != NULL)
{
top = top->next;
delete current;
current = top;
}
}
}
node *top=NULL;
};
class summary_report_price
{
public:
//double wedding_menu=22.00; //per person
//double birthday_menu=17.00;
//double funeral_menu=16.00;
//double celebrationEvent_menu=30.00;
//double openHouse_menu=25.00;
summary_report (){}
~summary_report(){}
struct quantity
{
int Num;
quantity *next_quantity;
};
struct total_quantity
{
int total;
total_quantity *next_total;
};
void enter_quantity(int Quantity, int totalRevenue)
{ int choose;
int price;
quantity *newQuantity;
total_quantity *newTotal;
cout << "\n\nWhich event did you previously choose?\n1.Wedding\n2.Birthday\n3.Funeral\n4.Celebration Event\n5.Open House.\n"<<endl;
cin>>choose;
switch(choose)
{
case 1:
cout <<"Wedding..."<<endl;
cout <<"Enter how many people that will be attending: "<<endl;
cin >>Quantity;
totalRevenue=Quantity*22;
break;
case 2:
cout <<"Birthday..."<<endl;
cout <<"Enter how many people that will be attending: "<<endl;
cin >>Quantity;
totalRevenue=Quantity*17;
break;
case 3:
cout <<"Funeral..."<<endl;
cout <<"Enter how many people that will be attending: "<<endl;
cin>>Quantity;
totalRevenue=Quantity*16;
break;
case 4:
cout <<"Celebration Event..."<<endl;
cout <<"Enter how many people that will be attending: "<<endl;
cin >>Quantity;
totalRevenue=Quantity*30;
break;
case 5:
cout <<"Open House..."<<endl;
cout <<"Enter how many people that will be attending: "<<endl;
cin >>Quantity;
totalRevenue=Quantity*25;
break;
case 6:
exit(1);
break;
default:
cout<<"You can only press 1-6 only."<<endl;
}
if(top_quantity == NULL||top_total==NULL) //checking for empty stack
{
top_quantity = new quantity;
top_quantity->Num= Quantity;
top_quantity->next_quantity= NULL;
top_total= new total_quantity; //calc total
top_total->total=totalRevenue;
top_total->next_total=NULL;
}
else
{
newQuantity= new quantity;
newQuantity->Num= Quantity;
newQuantity->next_quantity= top_quantity;
top_quantity= newQuantity;
newTotal= new total_quantity;
newTotal->total=totalRevenue;
newTotal->next_total=top_total;
top_total= newTotal;
}
}
void pop_price(int Quantity, int totalRevenue)
{
quantity *current36;
total_quantity *current71;
if(top_quantity== NULL)
{
cout<<"!!!Stack underflow" << endl;
}
if(top_total== NULL)
{
cout<<"!!!Stack underflow" << endl;
}
else
{
Quantity = top_quantity->Num;
current36 = top_quantity;
top_quantity= top_quantity->next_quantity;
delete current36;
totalRevenue = top_total->total;
current71=top_total;
top_total = top_total->next_total;
delete current71;
cout << "Quantity: "<< Quantity <<endl;
cout << "Total: RM" << totalRevenue<< " is removed " << endl;
}
}
void display_totalRevenue()
{
quantity *current36;
total_quantity *current71;
if(top_quantity == NULL || top_total==NULL)
{
cout<<"\nEmpty revenue" << endl;
// return;
}
else
{
current36 = top_quantity;
current71=top_total;
while(current36 != NULL || current71 !=NULL)
{
cout<<"\nQuantity: "<<current36->Num<<endl;
current36 = current36->next_quantity;
cout<<"Total: RM"<<current71->total<<endl;
current71=current71->next_total;
}
}
}
quantity *top_quantity;
total_quantity *top_total;
};
So here's the problem I'm facing, supposedly I tried to access these structs from different classes of linked lists and make it print out into a summary report. That was the plan, as soon as I entered data it only shows me the list is empty.
Below is the function I'm having trouble with...is there another way to do this? Sorry for the mistakes. I am just a beginner to programming for C++
void QuantityReady(User::user_details *current1, User::user_date *current2, User::event *current23, summary_report_price::quantity *current36, summary_report_price::total_quantity *current71,int count)
{
//quantity
summary_report_price::quantity *temp3;
temp3=current36;
//totalRevenue
summary_report_price::total_quantity *temp5;
temp5=current71;
//struct user_details
User::user_details *temp;
temp=current1;
//struct user_date
User::user_date *temp1;
temp1=current2;
//struct event
User::event *temp2;
temp2=current23;
int i,j;
int HoldNum, tempCount= count, sizeM[tempCount], countM[tempCount];
int RevenueM[tempCount], HoldRevenue;
string NameM[tempCount], HoldName;
string DateM[tempCount], HoldDate;
string EventM[tempCount], HoldEvent;
string events1="Course style-Wedding ";
string events2="Course style-Birthday ";
string events3="Course style-Funeral ";
string events4="Course style-Celebration Event ";
string events5="Course style-Open House ";
string events6="Buffet style-Wedding ";
string events7="Buffet style-Birthday ";
string events8="Buffet style-Funeral ";
string events9="Buffet style-Celebration Event ";
string events10="Buffet style-Open House ";
system("cls");
if(current1==NULL||current2==NULL||current23==NULL||current36==NULL||current71==NULL)
{
cout<<"The list is empty..."<< endl;
cout<<"Press any key to continue ..."<<endl;
getch();
}
else
{ User::user_details *next1;
User::user_date *next2;
User::event *next_event;
summary_report::quantity *next_quantity;
summary_report::total_quantity *next_total;
i=0;
while (i<tempCount)
{
RevenueM[i]=temp5->total;
temp5=temp5->next_total;
sizeM[i]=temp3->Num; //quantity
temp3=temp3->next_quantity;
;
NameM[i]=temp->data1;//struct user details
temp=temp->next1;
DateM[i]=temp1->data2; // user date
temp1=temp1->next2;
EventM[i]=temp2->data4; //event
temp2=temp2->next_event;
i=i+1;
}
j=0;
for(i=0;i<tempCount; i++)
{
for(j=i+1; j<tempCount; j++)
{
if(sizeM[i]<sizeM[j]&&NameM[i]<NameM[j]&&DateM[i]<DateM[j]&&EventM[i]<EventM[j]&&RevenueM[i]<RevenueM[j]) //bubble sorting algorithm
{
HoldRevenue=RevenueM[i];
RevenueM[i]=RevenueM[j];
RevenueM[j]=HoldRevenue;
HoldNum=sizeM[i];
HoldName=NameM[i];
sizeM[i]=sizeM[j];
NameM[i]=NameM[j];
sizeM[j]=HoldNum;
NameM[j]=HoldName;
//date
HoldDate=DateM[i];
DateM[i]=DateM[j];
DateM[j]=HoldDate;
//event
HoldEvent=EventM[i];
EventM[i]=EventM[j];
EventM[j]=HoldEvent;
}
}
}
for(i=0;i<tempCount;i++)
{
countM[i]=0;
}
cout<<"\t\t\t\tReeves Cartering Summary Report"<<endl;
for(i=0;i<tempCount;i++)
{ temp1=current2;
temp2=current23;
temp3=current36;
temp5=current71;
temp=current1;
while (temp!=NULL||temp1!=NULL||temp2!=NULL||temp3!=NULL||temp5 !=NULL)
{
if(temp->data1==NameM[i]||temp1->data2==DateM[i]||temp2->data4==EventM[i]||temp3->Num==sizeM[i]||temp5->total==RevenueM[i])
{
if(temp2->data4.compare(events1)==0)
{
cout<<temp->data1<< " "<<temp2->data4<<temp1->data2 <<temp3->Num<<" "<<temp5->total; //quantity
}
if(temp2->data4==events2)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events3)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events4)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events5)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events6)
{
cout<<temp->data1<< " "<<temp2->data4<<temp1->data2 <<temp3->Num<<" "<<temp5->total; //quantity
}
if(temp2->data4==events7)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events8)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events9)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
if(temp2->data4==events10)
{
cout<<temp->data1<<" "<<temp2->data4<<temp1->data2<<temp3->Num<<" "<<temp5->total;
}
countM[i]=1;
}
temp3=temp3->next_quantity;
temp5=temp5->next_total;
temp1=temp1->next2;
temp2=temp2->next_event;
temp=temp->next1;
}
}
cout<<"Press any key to continue...";
getch();
}
}
```
Your mistake is on the following line:
if(current1==NULL||current2==NULL||current23==NULL||current36==NULL||current71==NULL)
It means if any of those are null than it will print out The list is empty, you never changed current23 71 or 36 from their default value, why do you think they will not be null??
summary_report::quantity *temp3;
temp3=current36;
//totalRevenue
summary_report::total_quantity *temp5;
temp5=current71;
//struct user_details
User::user_details *temp;
temp=current1;
//struct user_date
User::user_date *temp1;
temp1=current2;
//struct event
User::event *temp2;
temp2=current23;
At this point your current 1 through 10 are all null, after you intialize the current 23, 71 etc will null as well...