使用链表的摘要报告代码中的错误 我目前正在为我的UNI任务做C++项目。我在使用链表编写代码时遇到了一些困难 #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; };

使用链表的摘要报告代码中的错误 我目前正在为我的UNI任务做C++项目。我在使用链表编写代码时遇到了一些困难 #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; };,c++,count,int,computer-science,C++,Count,Int,Computer Science,您的错误在以下行: if(current1==NULL||current2==NULL||current23==NULL||current36==NULL||current71==NULL) 这意味着如果其中任何一个是空的,那么打印出来的列表是空的,您从未更改过current23 71或36的默认值,为什么您认为它们不会是空的 summary_report::quantity *temp3; temp3=current36; //totalRevenue

您的错误在以下行:

if(current1==NULL||current2==NULL||current23==NULL||current36==NULL||current71==NULL)
这意味着如果其中任何一个是空的,那么打印出来的列表是空的,您从未更改过current23 71或36的默认值,为什么您认为它们不会是空的

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;
此时,您当前的1到10都为空,初始化当前的23、71等后也将为空

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;