【问题标题】:Menu type program菜单类型程序
【发布时间】:2015-08-01 19:09:30
【问题描述】:

我有一个任务是编写一个用 C 语言支持艺术画廊的程序。它必须是使用列表的基于菜单的程序。我写了菜单的第一个功能,我需要一些帮助来写其他三个。所以我有一个唯一的绘画代码结构,作者姓名,绘画名称,价格,绘画年份。我必须创建一个函数,使用唯一代码删除一幅画,打印出每幅画的所有信息,并使用所述代码再次修改一幅画。数据必须是使用链表的动态类型结构。这是迄今为止的程序。

#include <stdio.h>

void addPainting(void);
void erasePainting(void);
void printData(void);
void modifyData(void);

int main()
{

    struct paintings
    {
        char code[20];
        char name[50];
        char paintingName[50];
        double price;
        int year;

    }painting[100];
    int choice;
    do
    {
        printf("Menu\n");
        printf("To add a painting press 1.\n");
        printf("To erase a painting press 2.\n");
        printf("To print data for all paintings by authors alphabetically press 3.\n");
        printf ("To modify data for a painting press 4.\n");
        printf("To exit the program press 5.\n");
        scanf("%d",&choice);

        switch(choice)
        {
           case 1:
            {
                addPainting();
                break;
            }
            case 2:
            {
                erasePainting();
                break;
            }
            case 3:
            {
                printData();
                break;
            }
            case 4:
            {
                modifyData();
                break;
            }

            default: printf ("Wrong choice. Try again\n");
                break;
        }
    }while (choice !=5);



void addPainting()
{
    FILE *fp;


    struct paintings painting;
    printf("Enter code:");
    scanf("%s", &painting.code);
    printf("Enter the name of the author:");
    scanf("%s", &painting.name);
    printf("Enter the name of the painting:");
    scanf("%s", &painting.paintingName);
    printf("Enter price:");
    scanf("%lf", &painting.price);
    printf("Enter the year of creation:");
    scanf("%d", &painting.year);
      if ((fp=fopen("paintings","wb"))==NULL)
        exit(1);
    if ((fwrite (&painting,sizeof(painting),1,fp)!=1))
        exit(2);
    fclose(fp);

}



}

【问题讨论】:

  • 函数addPainting()好像嵌套在main()
  • 请不要在其他函数中定义函数/结构...
  • 欢迎您!如果您告诉我们您的问题到底是什么,我们会为您提供帮助。仅仅“它不起作用”对我们来说是不够的。
  • 我必须写我提到的三个函数,我不知道如何以及从哪里开始。这就是问题所在。
  • 您的代码不会编译。在继续其他问题之前,您必须先了解这一点,但是,有很多关于如何在 C 中创建和使用链表的教程。Here is one。 Google C 中的链接列表 了解更多信息。

标签: c function menu linked-list structure


【解决方案1】:

第一个问题:您缺少 main() 函数的右大括号 (})。 (但我相信你知道)

结构体大小错误的原因是您试图在void addPainting() 函数中创建struct painting 的实例,而它是使用本地创建的范围在主函数中,因此对函数不可见。如果您想以这种方式使用它,请创建具有全局范围的 struct painting

这将构建(并运行),但仅针对已定义的函数,其他函数被注释掉。您还需要解决或询问其他问题。

已编辑 修复 scanf() 语句,显示 fopen()/fclose() 的使用,使用 sprintf()/fputs() 创建和写入字符串。 .

void addPainting(void);
//void erasePainting(void);
//void printData(void);
//void modifyData(void);

typedef    struct //created with global scope, visible to all functions
{
    char code[20];
    char name[50];
    char paintingName[50];
    double price;
    int year;

}PAINTING;

PAINTING painting[100];//array of instance of PAINTING

#define PATH "C:\\play\\painting.txt"  //edit to your need


int main()
{

    int choice;
    do
    {
        printf("Menu\n");
        printf("To add a painting press 1.\n");
        printf("To erase a painting press 2.\n");
        printf("To print data for all paintings by authors alphabetically press 3.\n");
        printf ("To modify data for a painting press 4.\n");
        printf("To exit the program press 5.\n");
        scanf("%d",&choice);

        switch(choice)
        {
           case 1:
            {
                addPainting();
                break;
            }
            case 2:
            {
                //erasePainting();
                break;
            }
            case 3:
            {
                //printData();
                break;
            }
            case 4:
            {
                //modifyData();
                break;
            }

            default: printf ("Wrong choice. Try again\n");
                break;
        }
    }while (choice !=5);
}

void addPainting(void)
{
    FILE *fp;
    char stringToWrite[80];
    //Note: this function could be prototyped with an int argument
    //        to be used as an index for the array arguments of your
    //        structure.  Currently, the indexes are hard-coded to 0, 


    printf("Enter code:");
    //scanf("%s", &painting[0].code); 
    scanf("%s", painting[0].code); //& not needed for char array (char *) et. al.
    printf("Enter the name of the author:");
    scanf("%s", painting[0].name);
    printf("Enter the name of the painting:");
    scanf("%s", painting[0].paintingName);
    printf("Enter price:");
    scanf("%lf", &painting[0].price);
    printf("Enter the year of creation:");
    scanf("%d", &painting[0].year);
    fp = fopen (PATH, "a");//open for create/append text file (not write binary, "wb")

    if(fp)
    {
        sprintf(stringToWrite, "Painting Code is: %s\n", painting[0].code);
        fputs(stringToWrite, fp);
        // do others same way...
        //...
        fclose(fp);
    }
}

【讨论】:

  • 最重要的是,每次调用addPainting 都会清除绘画数据库。
  • @szczurcio - 还有很多其他问题。但是OP必须问。我回答的问题是迄今为止唯一提出的问题(结构大小错误)
  • 那么我如何让它在调用 addPainting 时向现有的绘画添加绘画,而不是删除数据库?
  • @TsvetomirPavlov 问题出在您的fopen 电话上。阅读手册:cplusplus.com/reference/cstdio/fopen
  • @TsvetomirPavlov - 我已经对结构定义(使用 typedef )和编辑函数进行了编辑。使用 fopen() 创建文件后,您可以使用多种方法编写字符串。我将再次编辑以说明创建字符串并使用 fputs() 将其写入文件。 (给我一分钟)
猜你喜欢
  • 2011-09-06
  • 1970-01-01
  • 2010-10-29
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 2016-05-22
  • 2017-11-11
相关资源
最近更新 更多