gpt4 book ai didi

c - 菜单式程序

转载 作者:行者123 更新时间:2023-11-30 17:14:25 25 4
gpt4 key购买 nike

我有一个任务是用 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);

}



}

最佳答案

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

结构大小错误的原因是您尝试在 void addPainting 中创建 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);
}
}

关于c - 菜单式程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351326/

25 4 0