如何返回一个结构

How to Return a structure

本文关键字:结构 一个 返回 何返回      更新时间:2023-10-16

当我选择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<<"ttt      LICA'S GROCERY STORE"<<endl<<endl;
    cout<<" ==============================================================================="<<endl<<endl;
    cout<<"ttt    Username: ";
    getline(cin, username);
    cout<<"nttt    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<<"tttt  WELCOME ADMIN!"<<endl<<endl;
            cout<<" ==============================================================================="<<endl<<endl;
                Menu(username);
                return main();
        }
        else if(username=="cashier" && password=="Cashier")
        {
            system("CLS");
            cout<<"n ==============================================================================="<<endl<<endl;
            cout<<"tttt  WELCOME CASHIER!"<<endl<<endl;
            cout<<" ==============================================================================="<<endl<<endl;
                Menu(username);
        }
        else
        {
            system("CLS");
            cout
                <<"ntt 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: "
        <<"nnttt A. FILE MAINTENANCE" <<endl
            <<"ntttt 1. Display All Products"<<endl
            <<"tttt 2. Insert New Product"<<endl
            <<"tttt 3. Delete A Product" <<endl
            <<"tttt 4. Search A Product"<<endl
        <<"nttt B. SALES MODULE"<<endl
            <<"ntttt 5. Cashier"<<endl
            <<"tttt 6. Print Receipt."<<endl
        <<"nttt C. EXIT"<<endl
            <<"ntttt 7. Log Out"<<endl;
        cout<<"nt Choose an option (1 to 7 only): ";
        cin>>menu;
        cin.ignore();
        switch(menu)
        {
            case 1:
                cout<<"n ============================================================================="<<endl<<endl;
                cout<<"ttt   DISPLAY ALL PRODUCTS"<<endl<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                DisplayProduct();
                system("pause");
                system("CLS");
                break;
            case 2:
                cout<<"n ============================================================================="<<endl<<endl;
                cout<<"ttt   INSERT NEW PRODUCT"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                InsertProduct();
                break;
            case 3:
                cout<<"n ============================================================================="<<endl<<endl;
                cout<<"ttt    DELETE A PRODUCT"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                DeleteProduct();
                break;
            case 4:
                cout<<"n ============================================================================="<<endl<<endl;
                cout<<"ttt SEARCH A PRODUCT" <<endl;
                cout<<" ============================================================================="<<endl<<endl;
                SearchProduct();
                break;
            case 5:
                cout<<"n ============================================================================="<<endl<<endl;
                cout<<"ttt PURCHASE PRODUCTS"<<endl;
                cout<<" ============================================================================="<<endl<<endl;
                PurchaseProduct();
            case 7:
                cout<<"nn THANK YOU ADMIN!"<<endl<<endl;
                break;
            default:
                cout<<"nn 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<<"nntEnter Product Name:";getline(cin,newproduct->pname);
    cout<<"ntEnter Product Quantity:";cin>>newproduct->qnty;
    cout<<"ntEnter Product ID:";cin>>newproduct->pid;
    cout<<"ntEnter 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<<"ntProduct is inserted"<<endl;
system("pause");
system("CLS");
}
void DeleteProduct()
{
    DisplayProduct();
    product *current,*previous;
    current=head;
    int x;
    if (current==NULL)
    {
        cout<<"nttNo Products Found.";
    }
    else
    {
        cout<<"nttEnter Product No. to delete:";
        cin>>x;
        int i=1;
        if(x==1)
        {
            head=current->ptr;
            delete current;
            cout<<"nttProduct is deleted";
            DisplayProduct();
        }
        else
        {
            while(i<x&&current!=NULL)
            {
                previous=current;
                current=current->ptr;
                i=i++;
            }
            if(current==NULL)
            {
                cout<<"nttProduct does not Exist";
            }
            else
            {
                previous->ptr=current->ptr;
                delete current;
                cout<<"nttProduct is deleted";
                DisplayProduct();
            }
        }
    }
    system("pause");
    system("CLS");
}
void DisplayProduct()
{
    product *current;
    current=head;
    int i=1;
    cout<<setiosflags(ios::left);
    while(current!=NULL)
    {
        cout<<"ntt"<<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<<"nttNo Products Found";
    else
    {
            cout<<"nttEnter 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<<"ntt"<<current->pid<<" is Product No."<<i;
            else
                cout<<"ntt"<<sid<<" is not in the list.";
            DisplayProduct();
            cout<<endl;
    }
    system("pause");
    system ("CLS");
}   
void PurchaseProduct()
{
}

你的数据在你保存到硬盘之前是在内存中,所以你需要实现一些机制来做到这一点。

你可以选择几个选项,如使用数据库,普通文件(XML, YAML, JSON等…)和对象序列化,我认为这是你最好的选择。有几个库可以做到这一点,但作为开始,我建议您使用本教程。然后转到c++序列化实用指南。

我推荐序列化,因为我意识到您刚刚开始使用c++。而其他选项,如数据库和格式化文件(xml, Yaml等…)则需要更复杂的代码。

玩得开心! !在你读完所有这些东西并(试着把它付诸实践)之后,如果你仍然不能解决你的问题,回到这里,我们将很乐意帮助你。