如何归还结构

时间:2014-03-17 11:40:56

标签: c++ function structure

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

例如

用户:admin 通:管理

然后我选择2.Insert Product之后我会选择1.display,之后我7.log out。现在我想再次以管理员身份登录,但我希望再次查看所有数据。请帮助这是我们的论文项目。

#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()
{

}

1 个答案:

答案 0 :(得分:0)

您的数据在内存中,直到您将其保存到硬盘,因此您需要实现某种机制来实现这一目标。

您可以选择多种选项,例如使用数据库,普通文件(XMLYAMLJSON等等...)和对象searialization 我认为这是你现在最好的选择。有几个库可以做到这一点,但是一开始我建议你使用这个tutorial。然后转到A practical guide to C++ serialization

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

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