C 中的库存程序。需要有关如何从库存中删除项目的帮助

2024-01-16

这是一个保存库存的程序。该程序显示一个选项菜单。除了删除条目功能之外,其他一切都很完美。我不知道如何让它删除一个功能。我放置了一个变量来查找位置,但我真的不知道如何。我输入要删除的项目名称,然后输入显示条目,它会陷入无限混乱。有人帮助我如何让它选择一个项目并将其从库存中删除。 它在 Visual Studios 2010 中使用,代码如下

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 25
#define MAX 100

  typedef struct {
    char item[SIZE]; //item for the company
    char company[SIZE]; //company that sells the item
    int intStock; //how many of the item are instock
    char lastShipDate[SIZE]; //last ship date of that item
    double cost; //cost to make the item
    double price; //price of the item 
    } gaming;

//prompts the user to get a selection
int Menu(void);

//display the options to the user
void DisplayOptions(void);

//display some entries for inventory
void HardCodeEntries(gaming entry[],int *size);

//function to add a new entry to the inventory
void AddNewEntry(gaming entry[], int *size);

//function to delete a selected entry from inventory
void Delete(gaming entry[], int *size, int location);

//display the current inventory to screen
void Display(gaming entry[], int size);

//save the current inventory to file
void SaveInventory(gaming br[], int *size);

//clears out the entire inventory
void Clear(int *size);

//load the inventory from the file that is being saved
void LoadSalesRecords(gaming br[], int *size);

//find location of entry that is going to be edited or deleted
int FindLocation(gaming entry[], int size);

//display the options for editing an entry
int EditMenuOptions(void);

//function to edit selected entry
void Edit(gaming entry[], int location);

int main()
{
    int selection;
    gaming entry [150];
    int size=0;
    char trash;
    int choice;
    int location;

    //display size of inventory before and after
    printf("\nSIZE before: %d", size);
    HardCodeEntries(entry, &size);
    printf("\nSIZE after: %d\n\n", size);

    DisplayOptions();

    selection= Menu();

    while(selection != 8)
    {
          if (selection==1)
          { 
              printf("ADD ENTRIES\n\n");
              //display size of inventory before adding entries
              printf("\nSIZE BEFORE ADDING: %d\n", size);

              AddNewEntry(entry, &size);

              //display size of inventory after adding entries
              printf("\nSIZE AFTER ADDING: %d\n", size);
          }
          else if(selection==2)
          {
              printf("DELETE\n\n");
              //get location of entry to be deleted

              location = FindLocation(entry,size);
              printf("\nLOCATION: %d\n", location);

              Delete(entry, &size, location);
          }
          else if(selection==3)
          {
              printf("EDIT\n\n");

              //get the location of entry to be edited
              location = FindLocation(entry, size);

              printf("\nLOCATION: %d\n", location);
              Edit(entry, location);
          }
          else if(selection==4)
          {
              printf("DISPLAY OF INVENTORY\n\n");
              Display(entry, size);
          }
          else if(selection==5)
          {
              printf("SAVING CURRENT INVENTORY TO FILE\n\n");
              SaveInventory(entry, &size);
              //saves to file
          }
          else if(selection==6)
          {
              printf("CLEARING ENTIRE DATA\n\n");
              Clear(&size);
          }
          else if(selection==7)
          {
              printf("LOADING SALES RECORDS FROM FILE\n\n");
              LoadSalesRecords(entry, &size);
          }
          else
          {
              printf("COMMAND NOT RECOGNIZED\n\n");
          }

          printf("\n\n");
          DisplayOptions();

          //clears buffer
          scanf("%c", &trash);

          selection = Menu();
    }

    if (selection==8)
    {
        printf("\nHAVE A NICE DAY!!!\n\n");
    }
    return 0;
}

//display the options to the user
void DisplayOptions(void)
{
    printf("HELLO, WELCOME TO THE INVENTORY PROGRAM.\n");
    printf("THIS PROGRAM HOLDS INVENORY FOR ELECTRONICS COMPANIES ALONG WITH THEIR ITEMS, COST TO PRODUCE, PRICE,\n");
    printf("LAST SHIP DATE AND NUMBER IN STOCK\n");
    printf("THERE ARE COMPANIES IN THE INVENTORY ALREADY\n");
    printf("*********************************************\n\n");
    printf("HERE ARE YOUR OPTIONS\n");
    printf("1--ADD A NEW ENTRY\n");
    printf("2--DELETE AN ENTRY\n");
    printf("3--EDIT AN ENTRY\n");
    printf("4--DISPLAY THE INVENTORY\n");
    printf("5--SAVE CURRENT INVENTORY TO FILE\n");
    printf("6--CLEAR THE ENTIRE INVENTORY\n");
    printf("7--LOAD SALES RECORD FROM FILE\n");
    printf("8--QUIT\n");
}

//prompts the user to get a selection
int Menu(void)
{
    int selection;
    printf("\nENTER SELECTION: ");
    scanf("%d", &selection);

    return selection;
}

//display some entries for inventory
void HardCodeEntries(gaming entry[], int *size)
{
    strcpy(entry[0].item,"Xbox360");
    strcpy(entry[0].lastShipDate,"04/10/12");
    entry[0].cost=200.00;
    strcpy(entry[0].company,"Microsoft");
    entry[0].price=300.0;
    entry[0].intStock=150;

    *size = *size + 1;

    strcpy(entry[1].item,"PlayStation3");
    strcpy(entry[1].lastShipDate,"04/10/12");
    entry[1].cost=200.00;
    strcpy(entry[1].company,"Sony");
    entry[1].price=450.00;
    entry[1].intStock=100;

    *size = *size + 1;

    strcpy(entry[2].item,"XboxController");
    strcpy(entry[2].lastShipDate,"04/10/12");
    entry[2].cost=25.00;
    strcpy(entry[2].company,"Microsoft");
    entry[2].price=50.00;
    entry[2].intStock=50;

    *size = *size + 1;

    strcpy(entry[3].item,"PS3Controller");
    strcpy(entry[3].lastShipDate,"04/10/12");
    entry[3].cost=30.00;
    strcpy(entry[3].company,"Sony");
    entry[3].price=45.00;
    entry[3].intStock=50;

    *size = *size + 1;

    strcpy(entry[4].item,"Wii");
    strcpy(entry[4].lastShipDate,"04/10/12");
    entry[4].cost=100.00;
    strcpy(entry[4].company,"Nintendo");
    entry[4].price=200.00;
    entry[4].intStock=150;

    *size = *size + 1;

}

//function to add a new enty to the inventory
void AddNewEntry(gaming entry[], int *size)
{
    printf("\nENTER ITEM:\t\t");
    scanf("%s", entry[*size].item);

    printf("ENTER SHIP DATE:\t");
    scanf("%s", entry[*size].lastShipDate);

    printf("ENTER COST TO MAKE:\t\t");
    scanf("%lf", &entry[*size].cost);

    printf("ENTER COMPANY:\t\t");
    scanf("%s", entry[*size].company);

    printf("ENTER PRICE:\t\t");
    scanf("%lf", &entry[*size].price);

    printf("ENTER NUMBER IN STOCK:\t");
    scanf("%d", &entry[*size].intStock);

    *size = *size + 1;
}

//function to delete a selected entry from inventory
void Delete(gaming entry[], int *size, int location)
{
    entry[location] = entry[*size - 1];
    *size = *size - 1;
}

//display the current inventory onto the screen
void Display(gaming entry[], int size)
{
    int i;
    for(i=0; i<size; i++)
    {
        printf("\n");
        printf("ITEM:\t\t %s\n", entry[i].item);
        printf("LAST SHIP DATE:\t %s\n", entry[i].lastShipDate);
        printf("COST:\t\t %4.2f\n",entry[i].cost);
        printf("COMPANY:\t %s\n", entry[i].company);
        printf("PRICE:\t\t %4.2f\n", entry[i].price);
        printf("IN STOCK:\t %d\n", entry[i].intStock);
    }
}

//save the current inventory to a file
void SaveInventory(gaming br[], int *size)
{
    FILE *outp;

    if((outp = fopen("output.txt","w"))==0)
    {
        printf("\nCANNOT OPEN FILE\n");
        return;
    }

    //connect to the file
    outp= fopen("output.txt","w");

    fprintf(outp,"%d", *size);
    fwrite(br, sizeof(gaming),*size,outp);

    //close file pointer
    fclose(outp);
}

//clears out the entire inventory
void Clear(int *size)
{
    *size = 0;
}

//load the inventory from the file that is being saved
void LoadSalesRecords(gaming br[], int *size)
{
    FILE * inp;

    if((inp = fopen("output.txt","r"))==0)
    {
        printf("CANNOT OPEN FILE\n");
        return;
    }
    //connect to file
    inp = fopen("output.txt","r");

    fscanf(inp,"%d",size);
    fread(br, sizeof(gaming),*size,inp);

    //close file pointer
    fclose(inp);
}

//find location of entry that is going to be edited or deleted
int FindLocation(gaming entry[], int size)
{
    int i;
    int j;
    int length;
    char userItem[SIZE];

    //enter the item to delete
    printf("ENTER ITEM TO EDIT OR DELETE: ");
    scanf("%s", userItem);
    length = strlen(userItem);

    for(i=0; i<length; i++)
    {
        userItem[i] = toupper(userItem[i]);
    }
    printf("\nYOUR BRAND IN UPPER CASE IS:  %s\n", userItem);

    for(j=0; j<size; j++)
    {
        if(strcmp(entry[j].item,userItem)==0)
        {
            return j;
        }
    }
    return -1;
}

//function to edit a selected entry
void Edit(gaming entry[], int location)
{
    int choice;

    choice = EditMenuOptions();

    if(choice==1)
    {
        printf("\nENTER NEW ITEM: ");
        scanf("%s", &entry[location].item);
    }
    else if(choice==2)
    {
        printf("\nENTER SHIP DATE: ");
        scanf("%s", &entry[location].lastShipDate);
    }
    else if(choice==3)
    {
        printf("\nENTER COST TO MAKE: ");
        scanf("%lf", &entry[location].cost);
    }
    else if(choice==4)
    {
        printf("\nENTER COMPANY: ");
        scanf("%s", &entry[location].company);
    }
    else if (choice==5)
    {
        printf("\nENTER PRICE: ");
        scanf("%lf", &entry[location].price);
    }
    else if (choice==6)
    {
        printf("\nENTER HOW MANY IN STOCK: ");
        scanf("%d", &entry[location].intStock);
    }
    else if (choice==7)
    {

        printf("\nENTER NEW ITEM: ");
        scanf("%s", &entry[location].item);
        printf("\nENTER SHIP DATE: ");
        scanf("%s", &entry[location].lastShipDate);
        printf("\nENTER COST TO MAKE: ");
        scanf("%lf", &entry[location].cost);
        printf("\nENTER COMPANY: ");
        scanf("%s", &entry[location].company);
        printf("\nENTER PRICE: ");
        scanf("%lf", &entry[location].price);
        printf("\nENTER HOW MANY IN STOCK: ");
        scanf("%d", &entry[location].intStock);
    }
    else
    {
        printf("Invalid Entry");
    }
}

//display the options for edditing an entry
int EditMenuOptions(void)
{
    int choice;

    printf("\nENTER 1 TO EDIT ITEM\n");
    printf("ENTER 2 TO EDIT DATE\n");
    printf("ENTER 3 TO EDIT COST\n");
    printf("ENTER 4 TO EDIT COMPANY\n");
    printf("ENTER 5 TO EDIT PRICE\n");
    printf("ENTER 6 TO EDIT NUMBERS IN STOCK\n");
    printf("ENTER 7 TO EDIT ALL DATA\n");
    printf("\nENTER SELECTION: ");
    scanf("%d", &choice);
    return choice;
}

遵循您的编码风格,尽管它应该改进:

void remove_item(gaming entry[], int *size, int item) {
    memcpy(&entry[item], &entry[*size], sizeof(entry[0]));
    *size = *size - 1;
}

此代码将覆盖所需的条目(在索引item)并减少最后一个索引(size) 减一。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C 中的库存程序。需要有关如何从库存中删除项目的帮助 的相关文章

  • fopen_s 怎么会比 fopen 更安全呢?

    我正在处理遗留代码Windows平台 当我编译代码时VS2013 它给出以下警告 错误 C4996 fopen 该函数或变量可能不安全 考虑使用fopen s反而 要禁用弃用 请使用 CRT SECURE NO WARNINGS 详情请参见
  • Caliburn.Micro - ShowDialog() 如何关闭对话框?

    EDIT 新信息 刚刚设法让记录器工作 老实说 我不知道 cm 有一个 并且在尝试使用时收到此消息TryClose TryClose requires a parent IConductor or a view with a Close m
  • ASP.NET Web 应用程序中的身份验证遇到问题

    我正在尝试对从登录页面登录我的 Web 应用程序的用户进行身份验证 我正在使用本教程 http support microsoft com kb 301240作为指南 它几乎准确地解释了我希望做什么 但是当我输入用户名和密码时 验证不起作用
  • 将字符串作为 PChar 从 CSharp 传递到 Delphi DLL

    我正在尝试将字符串从 C 传递到 Delphi 构建的 DLL Delphi DLL 需要 PChar 这是Delphi导出 procedure DLL Message Location PChar AIntValue integer st
  • 最新 .Net MongoDb.Driver 的连接问题

    我创建了一个 MongoLab 沙箱数据库 我与 MongoChef 连接 效果很好 我通过 Nuget 安装了 MongoDB Driver 2 2 2 我编写了一些简单的 C 演示代码 但就是无法使其工作 连接字符串是直接从 Mongo
  • 使用 C# 将多个音频样本混合到单个文件中

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个能够创建音频文件 mp3 或 wav 的库 NAudio http www codeple
  • 为什么子函数不销毁GtkWindow?

    这是我的代码 void window first void enter window2 GtkWidget w gpointer data void quit GtkWidget w gpointer data void quit int
  • 嵌入资源文件的路径

    我的资源文件中有一个图标 我想引用它 这是需要图标文件路径的代码 IWshRuntimeLibrary IWshShortcut MyShortcut MyShortcut IWshRuntimeLibrary IWshShortcut W
  • ASP.NET - 在 RenderContent 调用中将事件处理程序添加到 Repeater 内的 LinkBut​​ton

    我有一个加载自定义用户控件的 Sharepoint WebPart 用户控件包含一个 Repeater 而 Repeater 又包含多个 LinkBut ton 在 Web 部件的 RenderContent 调用中 我有一些用于添加事件处
  • 如何自定义 Google 测试失败消息?

    我编写了一个如下所示的 Google 测试 它将一些计算值与 CSV 文件中预期存储的值进行比较 class SampleTest public testing Test public void setupFile const std st
  • 在c#中获取没有时间的日期

    我的表上有一列 缺勤日期时间 日期 当我想要获取包含日期的行时 它返回 0 行 这是我的 C 代码 DateTime ClassDate DateTime Parse lblDate Content ToString var Abs dbs
  • Gremlin.net 文本包含等效项

    我正在使用 Gremlin net 库连接到 janus 图形服务器 我使用 cassandra 和弹性搜索进行数据存储和索引 在我使用的 gremlin 语言和 gremlin 控制台中文本包含在属性的文本中进行搜索 我正在使用混合索引
  • 如何同步nosql db(ravendb)中的更改

    我已经开始在 RavenDB 的示例上学习 NoSQL 我从一个最简单的模型开始 假设我们有由用户创建的主题 public class Topic public string Id get protected set public stri
  • 连接到没有元数据的网络服务

    我想连接到此网络服务 https training api temando com schema 2009 06 server wsdl https training api temando com schema 2009 06 serve
  • 如何将System.Windows dll添加到Visual Studio 2010 Express?

    我正在开发一个小型应用程序C and VS2010 as IDE with NET框架4 我想用CaptureSource类以便从笔记本电脑的网络摄像头捕获视频 为此我需要添加一个命名空间System Windows DependencyO
  • “1个未解决的外部”C++

    我已经检查了所有文件之间的连接以及类和函数定义 但每次我尝试运行我的程序时 它都会阻止我并告诉我它有 1 个未解析的外部 该程序应该打开多个文件 一个 学生 文件和一个 成绩 文件 从中读取数据 然后使用 查询文件 来查找数据 找到查询中要
  • 卸载程序

    我正在尝试使用此代码卸载程序 但它似乎不起作用 我尝试过其他答案 但似乎也不起作用 有人可以帮助我吗 我正在尝试按给定名称 displayName 卸载该程序 例如 我给出 displayName Appname 那么此代码应该从我的计算机
  • 具有四个 && 的 LINQ Where 子句

    我正在尝试在Where 子句中创建一个带有4 个参数的LINQ 查询 这是一个 Windows 8 应用程序项目 我正在使用 SQLite 数据库 SQLite 实现 https github com praeclarum sqlite n
  • 将一个 IEnumerable 拆分为多个 IEnumerable

    我是 linq 新手 我需要根据指示器将 Couple string text bool Indicator 类型的 IEnumerable 拆分为多个 IEnumerable 我尝试使用skipWhile 和 TakeWhile 但没有找
  • 如何使用 C# 为 azure devops 变量赋值

    我有 selenium C 测试脚本 可以从浏览器获取令牌 我有两个 azure devops 任务 一个用于执行 selenium 测试 另一个用于执行 API 测试 我想将 selenium 测试获取的令牌传递给 API 测试执行任务

随机推荐

  • 在javascript中将小数转换为六十进制(以六十为基数)

    将十进制数 以十为基数 转换为以 0 9 A Z 和 a x 作为数字的字符串表示的六十进制 以六十为基数 的最佳方法是什么 我计划用 javascript 对其进行编码 但感谢您的帮助 使用示例 gt gt decToSex 60 10
  • RStudio read.xl工作目录错误

    大家好 我在将 xlsx 加载到 RStudio 时遇到困难 我不确定为什么 RStudio 无法看到该文件 我指定的 read excel 路径是否错误 有任何想法吗 R 的新人 Thanks Windows 10 64 位 版本 0 9
  • OpenCL 中的矩阵求逆

    我正在尝试使用 OpenCL 加速一些计算 算法的一部分包括反转矩阵 是否有任何开源库或免费可用的代码来计算用 OpenCL 或 CUDA 编写的矩阵的 lu 分解 lapack dgetrf 和 dgetri 或一般求逆 该矩阵是实数且为
  • 检索 C++ 程序中的所有函数 [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 假设我们有一个定义了函数的 C 程序 我们称之为input cpp 现在 我有另一个 C 程序retrieve cpp必须 检索 中的所有功
  • 为什么 NGEN 应该挂起并且永远不会完成特定程序集的任何原因?

    我有一个使用 Visual Studio 2008 构建的 NET 3 5 类库项目 如果我尝试 NGEN 此解决方案文件中的核心程序集 NGEN 永远不会完成 或者至少在我费心让它运行的时间内 比如过夜 不会完成 还有其他人经历过吗 如果
  • “合并”多个模型。创建“最近活动”框

    如何合并模型 以便我可以按顺序显示最后 10 个帖子 提要条目和私人消息 帖子存储在 Post 模型中并按 created at 排序 Feed 条目存储在 Planet 中并在 published at 上排序 私人消息存储在 消息 中
  • 使用 PowerShell 和 System.Data.SQLite 打开 Firefox 文件“places.sqlite”

    我想运行以下代码 dll System Reflection Assembly LoadWithPartialName System Data SQLite System Reflection Assembly LoadFrom C Pro
  • 替换 AuthenticationHandler 进行集成测试

    我有一个 web 应用程序 它使用浏览器客户端的表单身份验证以及对 odata 源的 api 访问的基本身份验证 这在生产中有效 但现在我正在努力使其可测试 我使用 WebApplicationFactory 方法 还成功实现了测试身份验证
  • vue中如何调整图片大小

    我将图像从nodejs上传到vue 并将图像放入v卡中 但图像被截断了 如何在不剪切的情况下调整图像大小 在 v img 中使用 包含 属性
  • android:我想在 WebViewb 中使用我的自定义字体

    我想在 WebViewb 中使用我的自定义字体我的 html 文件已加载到 webView 中 但仍然没有字体我的字体有 Unicode 字符我在 android 2 2 上工作 mWebView loadUrl file android
  • Laravel Blade 模板在尝试获取非对象的属性时如何返回 null 而不是 ErrorException

    我正在编写一些 Laravel Blade 模板 并且我的模型可能包含空对象 我非常想尝试获取对象属性 如果出现错误 则返回 null 所以不必这样写 if model gt child object that may be null mo
  • 如何链接相同或不同文件夹中的html页面?

    如果 html 页面位于相同或不同的文件夹中 而无需编写完整路径 如何链接到它们 在同一文件夹中 只需使用文件名 a href thefile html my link a 在父文件夹的目录中 a href thefile html my
  • python 中的迭代

    您好 我想创建一些代码来打印一个如下所示的框 代码应该使用循环来打印一行框 使用 for i in range 5 不应该使用 IF 语句来解决这个问题 只使用一个框 如下所示 我尝试使用下面的代码 但没有产生所需的输出 请帮忙 for i
  • Firebase:自动创建/更新多个子节点

    假设我有一个带有 用户 节点和 宠物 节点的项目 当用户获得宠物时 我想将宠物的密钥添加到用户的 pets 节点 并将用户 ID 添加到宠物的 owner 节点 Example users user1 pets pet1 true pet3
  • 在 Xcode 11 中将分支合并到 master 中?

    我一定在这里遗漏了一些非常简单的东西 在早期版本的 Xcode 上 我从未遇到过将分支合并到 master 中的问题 但在使用 Xcode 11 时 我在任何项目上都没有该选项 我应该如何合并到master 谢谢 这是一个令人沮丧的 Xco
  • 使用 C# 从 Parquet 文件中读取前 100 行

    我有这些巨大的镶木地板文件 存储在一个 blob 中 有超过 60 万行 我想检索前 100 个 以便我可以将它们发送到我的客户端应用程序 这是我现在用于此功能的代码 private async Task lt Table gt getPa
  • 如何查找 Ruby 哈希的“nil”值并将其替换为“None”或 0?

    我试图深入到数组嵌套哈希迭代中的每个值并替换所有值nil值带有 无 或 0 之类的值 请参阅我的代码 它显然不起作用 在将其传递到 Rails 中的视图进行迭代和渲染之前 我需要修复此问题 我的控制器 def show results Re
  • SECURITY_ERR:调用 Canvas 的 toDataURL 方法时出现 DOM 异常 18

    当我尝试从在 Internet Explorer 和 Safari 浏览器上绘制 SVG 图像的画布检索数据 URL 时 出现以下错误 而其他浏览器都正常工作 此外 SVG 图像还包含一些
  • 使用sequelize的种子数据的不同目录

    我希望在开发和生产之间有不同的种子数据 我如何在配置中指定它 我知道在 sequelizerc我可以加载动态配置文件并指定seeders path sequelizerc const path require path module exp
  • C 中的库存程序。需要有关如何从库存中删除项目的帮助

    这是一个保存库存的程序 该程序显示一个选项菜单 除了删除条目功能之外 其他一切都很完美 我不知道如何让它删除一个功能 我放置了一个变量来查找位置 但我真的不知道如何 我输入要删除的项目名称 然后输入显示条目 它会陷入无限混乱 有人帮助我如何