c++ - 如何在我的数据库程序中添加“减少项目数量”功能?
问题描述
我正在为我的期末考试项目制作一个数据库库存程序。我对c ++真的很陌生,这是我的代码到目前为止:
#include <iostream>
#include <fstream>
#include <string>
#define ss 30
using namespace std;
struct store
{
char brand[30];
char name[30];
char pc[30];
int x;
float price;
}s[ss];
void AddNewItem(int &num)
{
cout<<"\n \t\t\t NEW DATA FOR AN ITEM\n\n\n";
cout<<"Product Code: ";
cin>>s[num].pc;
cout<<endl;
cout<<"Company Name: ";
cin>>s[num].brand;
cout<<endl;
cout<<"Product Name: ";
cin>>s[num].name;
cout<<endl;
cout<<"Product Price ";
cin>>s[num].price;
cout<<endl;
cout<<"Quantity: ";
cin>>s[num].x;
num++;
cout<<endl;
}
void ShowInventoryList(int &num)
{
if (num==0)
{
cout<<"\nThere are no items in the record right now. Please add record.\n\n";
}
else{
cout<<"\n \t\t\t LIST OF ALL ITEMS"<<endl;
cout<<"Product Code \t Company Name \t Product Name \t Product Price \t Quantity'\t" <<endl;
for(int i=0;i<num;i++)
{
cout<<s[i].pc <<"\t\t"
<<s[i].brand <<"\t\t"
<<s[i].name <<"\t\t"
<<s[i].price <<"\t\t"
<<s[i].x <<"\t\t"
<<endl
<<endl;
}
}
}
int main()
{
ifstream openFile("Inventory.txt");
string line;
int menu,number=0;
do
{
cout<<"\n==================== MENU ====================";
cout<<"\n ";
cout<<"\n 1. Add New Item ";
cout<<"\n 2. Show Inventory List ";
cout<<"\n 3. Search An item From Record ";
cout<<"\n 4. Reduce The Quantity of An Item; ";
cout<<"\n 5. Save Item Stock to Text File ";
cout<<"\n 6. Show Item Stock from Text File ";
cout<<"\n 7. Exit ";
cout<<"\n ";
cout<<"\n=================================================="<<endl;
cout<<"\nEnter your choice: ";
cin>>menu;
switch(menu)
{
case 1:{AddNewItem(number);
break;}
case 2:{ShowInventoryList(number);
break;}
}
}
while(menu!=7);
cout<<"\nThank you for using our program. Have a good day!";
}
我将项目的数量设为 int,因为我打算只询问用户他们想要从项目中减少的数量,然后从项目的原始数量中减去它。我被困在这里,非常感谢您的帮助!<3
解决方案
更新:感谢大家的帮助,我设法通过使用它来做到这一点:
void RemoveItemRecord(int&num)
{
int c;
long int pc, qnty;
cout<<"What do you want to delete from the record?\n Please enter product code of the item: ";
cin>>pc;
cout<<"Enter the amount you want to reduce from the quantity";
cin>>qnty;
if(num==0){
cout<<"\nThere is no data to be deleted.\n\n";}
else{
for(c=0;c<num;c++){
if(s[c].pc=pc){
s[c].qnty=s[c].qnty-qnty;
}
}
}
}
推荐阅读
- java - 父类中缺少嵌套的@ConfigurationProperties
- angular - 错误类型错误:无法读取未定义 Angular 的属性
- python - 删除非英语行 Pandas
- google-earth-engine - 过滤具有选定几何图形的图像集合
- javascript - 未捕获的语法错误:无法在模块外使用 import 语句(尝试使用 Shepard.js 库)
- c# - ASP.NET Core + PostgreSQL - 'System.OutOfMemoryException' 被抛出。我该如何解决?
- typescript - 替换单元测试的枚举
- swiftui - 使用 Slider 和 Button 在 SwiftUI 中实现 scrollTo 的问题
- javascript - 有没有办法在 js 或 css 中悬停时显示一种不透明度低的工具提示?
- java - 如何格式化检索 0 行的 SQL 查询的方法?