c++ - 使用链表的摘要报告代码中的错误

标签 c++ count int computer-science

我目前正在为我的大学作业做一个 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++ 编程的初学者

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();
            
        }
        
        
    }

    

   



    
    ```

最佳答案

您的错误位于以下行:

if(current1==NULL||current2==NULL||current23==NULL||current36==NULL||current71==NULL)

这意味着如果其中任何一个为空,那么它将打印出 The list isempty, you never change current23 71 or 36 from their default value, Why do you think those won't 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;

此时你当前的 1 到 10 都为 null,初始化后当前的 23、71 等也将为 null...

关于c++ - 使用链表的摘要报告代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62736376/

相关文章:

c++ - 当 Visual C++ 拒绝此基于模板 -"dependent"的枚举时,它是否正确?

c++ - 线性颜色渐变不起作用

R - 按中断切割并按组计算出现次数

c++ - 任何在 64 位机器上使用 8 个字节作为 int 数据类型大小的 C/C++ 编译器

java - java格式化int方法

java 。限制日期的值

c++ - C++ 中从非引用类型自动推导引用模板参数

c++ - 如何从数据库解析日期时间格式?

php - count() 的未定义偏移量

php - mysql加入: get parts with count for each type