C语言 菜单类型程序



我有一个任务来写一个程序支持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("Menun");
        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 againn");
                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);
}

}

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

结构体大小错误的原因是 你试图在void addPainting()函数中创建struct painting的实例,当它在主函数中使用局部作用域创建时,因此对函数不可见。创建struct painting与全局作用域,如果你想这样使用它:

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

EDITED 修复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("Menun");
        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 againn");
                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: %sn", painting[0].code);
        fputs(stringToWrite, fp);
        // do others same way...
        //...
        fclose(fp);
    }
}

相关内容

最新更新