如何返回一个结构
How to Return a structure
当我选择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&¤t!=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等…)则需要更复杂的代码。
玩得开心! !在你读完所有这些东西并(试着把它付诸实践)之后,如果你仍然不能解决你的问题,回到这里,我们将很乐意帮助你。
相关文章:
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 静态结构和一个定义规则
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 在学习数据结构之前对STL有一个了解是好的吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 使用完数据结构后清空数据结构是一个好习惯吗?
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何将数据从一个结构链接到另一个结构
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- std::transform 将一个结构数组复制到另一个结构数组
- C++:如何做一个链表,结构作为参数传递?
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 两个附带的类层次结构-一个好的设计模式