C++ 如何返回结构

C++ 如何返回结构,c++,function,structure,C++,Function,Structure,当我选择7时,如何返回所有结构。从“管理”菜单插入记录后注销,如果返回“管理”菜单,我将再次看到该记录 比如说 用户:管理员 通行证:管理员 然后我选择2.插入产品之后我会选择1.显示,然后我7.注销。现在我想再次以管理员身份登录,但我想再次查看所有数据。请帮助这是我们的论文项目 #include <iostream> #include <string> #include <iomanip> #include <conio.h> using na

当我选择7时,如何返回所有结构。从“管理”菜单插入记录后注销,如果返回“管理”菜单,我将再次看到该记录

比如说

用户:管理员 通行证:管理员

然后我选择2.插入产品之后我会选择1.显示,然后我7.注销。现在我想再次以管理员身份登录,但我想再次查看所有数据。请帮助这是我们的论文项目

#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>

using namespace std;

struct product{

    string pname,pid;
    int qnty;
    double price;

    product *ptr;
};

//Function Declaration
void Menu(string username);
void InsertProduct();
void DeleteProduct();
void DisplayProduct();
void SearchProduct();
void PurchaseProduct();
//

product *head;

int main() //Account Login
{
    int pw=0,n;
    char pwc=' ';
    string username,password;

    cout<<"\n ==============================================================================="<<endl<<endl;
    cout<<"\t\t\t      LICA'S GROCERY STORE"<<endl<<endl;
    cout<<" ==============================================================================="<<endl<<endl;

    cout<<"\t\t\t    Username: ";
    getline(cin, username);

    cout<<"\n\t\t\t    Password: ";

    do
    {
        pwc=getch();
        if(pwc==13||pwc==' ')
        {
            break;
        }

        cout<<"*";
        password+=pwc;
        pw++;
    }
    while(pwc!=13||pwc!=' ');

    n=username.length();

    for(int a=0; a!=n; a++)
    {
        if(isupper(username.at(a)))
        {
        username.at(a) = tolower(username.at(a));

        }
    }



    if(username=="admin" && password=="Admin")
        {
            system("CLS");
            cout<<"\n ==============================================================================="<<endl<<endl;
            cout<<"\t\t\t\t  WELCOME ADMIN!"<<endl<<endl;
            cout<<" ==============================================================================="<<endl<<endl;
                Menu(username);
                return main();

        }
        else if(username=="cashier" && password=="Cashier")
        {
            system("CLS");

            cout<<"\n ==============================================================================="<<endl<<endl;
            cout<<"\t\t\t\t  WELCOME CASHIER!"<<endl<<endl;
            cout<<" ==============================================================================="<<endl<<endl;
                Menu(username);
        }
        else
        {
            system("CLS");
            cout
                <<"\n\t\t Invalid Username or Password. Please Try Again"<<endl;
                return main();
        }

    system("pause");
    return 0;
}
//
//
//
//
//
void Menu(string username)
{
    head=NULL;

    int menu;
    if(username=="admin")
    {
    do
    {

    cout
        <<"\t MENU: "
        <<"\n\n\t\t\t A. FILE MAINTENANCE" <<endl
            <<"\n\t\t\t\t 1. Display All Products"<<endl
            <<"\t\t\t\t 2. Insert New Product"<<endl
            <<"\t\t\t\t 3. Delete A Product" <<endl
            <<"\t\t\t\t 4. Search A Product"<<endl
        <<"\n\t\t\t B. SALES MODULE"<<endl
            <<"\n\t\t\t\t 5. Cashier"<<endl
            <<"\t\t\t\t 6. Print Receipt."<<endl
        <<"\n\t\t\t C. EXIT"<<endl
            <<"\n\t\t\t\t 7. Log Out"<<endl;

        cout<<"\n\t Choose an option (1 to 7 only): ";
        cin>>menu;
        cin.ignore();

        switch(menu)
        {
            case 1:
                cout<<"\n ============================================================================="<<endl<<endl;
                cout<<"\t\t\t   DISPLAY ALL PRODUCTS"<<endl<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                DisplayProduct();
                system("pause");
                system("CLS");
                break;
            case 2:
                cout<<"\n ============================================================================="<<endl<<endl;
                cout<<"\t\t\t   INSERT NEW PRODUCT"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                InsertProduct();

                break;
            case 3:
                cout<<"\n ============================================================================="<<endl<<endl;
                cout<<"\t\t\t    DELETE A PRODUCT"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                DeleteProduct();
                break;
            case 4:
                cout<<"\n ============================================================================="<<endl<<endl;
                cout<<"\t\t\t SEARCH A PRODUCT" <<endl;
                cout<<" ============================================================================="<<endl<<endl;
                SearchProduct();
                break;
            case 5:
                cout<<"\n ============================================================================="<<endl<<endl;
                cout<<"\t\t\t PURCHASE PRODUCTS"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                PurchaseProduct();
            case 7:
                cout<<"\n\n THANK YOU ADMIN!"<<endl<<endl;
                break;
            default:
                cout<<"\n\n INVALID INPUT!"<<endl<<endl;

        }
    }while(menu!=7);
    }
    else
    {
    cout
        <<"Choose an Option"
        <<"\n1.Sales Module"
        <<"\n2.Exit"<<endl;
        cin>>menu;
        switch(menu)
        {
            case 1:
                cout<<"Sales Module";
                break;
            case 2:
                cout<<"Exit!";
                break;
            default:
                cout<<"Invalid Input!";

        }
    }
}

void InsertProduct()
{

    product *newproduct;
    newproduct=new product;
    cout<<"\n\n\tEnter Product Name:";getline(cin,newproduct->pname);
    cout<<"\n\tEnter Product Quantity:";cin>>newproduct->qnty;
    cout<<"\n\tEnter Product ID:";cin>>newproduct->pid;
    cout<<"\n\tEnter Product Price:";cin>>newproduct->price;
    cin.ignore();


    newproduct->ptr=NULL;
    if(head==NULL)
        head=newproduct;
    else
    {
        product *lastproduct;
        lastproduct=head;
        while(lastproduct->ptr!=NULL)
            lastproduct=lastproduct->ptr;
        lastproduct->ptr=newproduct;
    }
    cout<<"\n\tProduct is inserted"<<endl;

system("pause");
system("CLS");
}

void DeleteProduct()
{
    DisplayProduct();
    product *current,*previous;
    current=head;
    int x;
    if (current==NULL)
    {
        cout<<"\n\t\tNo Products Found.";
    }
    else
    {

        cout<<"\n\t\tEnter Product No. to delete:";
        cin>>x;
        int i=1;
        if(x==1)
        {
            head=current->ptr;
            delete current;
            cout<<"\n\t\tProduct is deleted";
            DisplayProduct();
        }
        else
        {
            while(i<x&&current!=NULL)
            {
                previous=current;
                current=current->ptr;
                i=i++;
            }
            if(current==NULL)
            {
                cout<<"\n\t\tProduct does not Exist";
            }
            else

            {
                previous->ptr=current->ptr;
                delete current;
                cout<<"\n\t\tProduct is deleted";
                DisplayProduct();
            }

        }
    }
    system("pause");
    system("CLS");
}

void DisplayProduct()
{
    product *current;
    current=head;
    int i=1;
    cout<<setiosflags(ios::left);
    while(current!=NULL)
    {
        cout<<"\n\t\t"<<setw(4)<<i;
        cout<<setw(25)<<current->pname<<setw(3)<<current->qnty <<setw(3)<<current->pid <<setw(3)<<current->price;
        current=current->ptr;
        i++;
    }

}

void SearchProduct()
{
    string sid;//Name to search
    product *current;
    current=head;
    if (current==NULL)
            cout<<"\n\t\tNo Products Found";
    else
    {
            cout<<"\n\t\tEnter product to search: ";
            cin>>sid;
            int i=1;

            while(sid.compare(current->pid))    
            {

                current=current->ptr;
                if (current==NULL)
                    break;
                i++;

            }

            if(current!=NULL)
                cout<<"\n\t\t"<<current->pid<<" is Product No."<<i;
            else
                cout<<"\n\t\t"<<sid<<" is not in the list.";
            DisplayProduct();
            cout<<endl;

    }
    system("pause");
    system ("CLS");
}   

void PurchaseProduct()
{

}

在您将数据保存到硬盘之前,您的数据一直在内存中,因此您需要实现一些机制来实现这一点

您可以在几个选项中进行选择,例如使用数据库、普通文件等。。。对象序列化,我认为这是你现在最好的选择。有几个库可以实现这一点,但对于start,我建议您使用这个库。然后转到

我建议序列化,因为我意识到你刚从C++开始。还有其他选项,如数据库和格式化文件XML、Yaml等。。。需要更复杂的代码


玩得开心!!!在您阅读了所有这些内容并尝试将其付诸实践后,如果您仍然无法解决问题,请回到这里,我们将很乐意为您提供帮助。

您不允许在程序中使用main,并且您将遇到std::getline似乎被跳过的问题,因为有很多关于它的问题,所以很容易搜索。@Rairulyle你已经知道如何循环了。再做一次。我已经更换了回水总管;现在我可以再次登录到菜单,但仍然看不到结构中的数据。