Januarydiam 2021-01-12 11:25 采纳率: 100%
浏览 4
已结题

添加函数的问题求大佬解答

#include <stdio.h>
#include <stdlib.h>

 struct lib
{
	char num[20];
	char name[20];
	char writer[20];
	char pub[20];
};

 typedef struct lib L;
 L book[200],books;

FILE *fp;

void add();
void list();
void search();

int main()
{
	int n;
	while(1)
	{
	system("cls");
	printf("Welcome!\n");
	printf("1.list.   \n2.add book.\n"); 
	printf("3.search book.\n4.delete book.\n");
	printf("5.exit.\n ");
	printf("Please input number:\n");
	scanf("%d",&n);
	if(n==1) 
		list();
	else if(n==2)
		add();
	else if(n==3)
		search();
//	else if(n==4)
	//	del();
	else if(n==5)
		exit(0);
	else
	{
		printf("wrong number!\n");
		system("pause");	
	}
	return 0;
}

void search() 
{
	int i=0;
	int m=0;
	char number[20];
	printf("Please input number to search :");
	scanf("%s",number);
	if ((fp=fopen ("12.txt","r"))==NULL)
	{
		printf("File open error!\n");
		exit (0);
	}
	while(!feof(fp))
	{
		
		fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
		if(book[i].num==number)
		{
			printf("------------------------------------------\n");
			printf("|  number   | book name | author | pulish |\n");
			printf("|  %4s     |%4s       |%5s   |%4s    |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
			printf("------------------------------------------\n");
			m=1;
			break;
		}
		i++;
	}
	if(m==0) 
		printf("No this book.");
	fclose(fp);
	system("pause");

}

void list()
{
	int i=0;
	if ((fp=fopen ("12.txt","r"))==NULL)
	{
		printf("File open error!\n");
		exit (0);
	}
	printf("------------------------------------------\n");
	printf("|  number   | book name | author | pulish |\n");
	while(!feof(fp))
	{
		
		fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
		printf("------------------------------------------\n");
		printf("|  %4s     |%4s       |%5s   |%4s    |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
		i++;
	}
	fclose(fp);
	printf("------------------------------------------\n");
	system("pause");

}



void add()
{	system("cls");
	
	if ((fp=fopen ("12.txt","a"))==NULL)
	{
		printf("File open error!\n");
		exit (0);
	}
	printf("Please input number:");
	scanf("%s",books.num);
	printf("Please input book name:");
	scanf("%s",books.name);
	printf("Please input author:");
	scanf("%s",books.writer);
	printf("Please input publish:");
	scanf("%s",books.pub);
	fprintf(fp,"%s\ %s\ %s\ %s\ ",books.num, books.name, books.writer, books.pub);
	fclose(fp);
}

添加 search函数之后(51) : error C2143: syntax error : missing ';' before 'type' 出现bao'cuo

  • 写回答

2条回答 默认 最新

  • 蒟蒻一枚 2021-01-12 12:11
    关注

    main函数里的while没写反大括号,修改后程序:

    	
    #include <stdio.h>
    #include <stdlib.h>
     
     struct lib
    {
    	char num[20];
    	char name[20];
    	char writer[20];
    	char pub[20];
    };
     
     typedef struct lib L;
     L book[200],books;
     
    FILE *fp;
     
    void add();
    void list();
    void search();
     
    int main()
    {
    	int n;
    	while(1)
    	{
    	system("cls");
    	printf("Welcome!\n");
    	printf("1.list.   \n2.add book.\n"); 
    	printf("3.search book.\n4.delete book.\n");
    	printf("5.exit.\n ");
    	printf("Please input number:\n");
    	scanf("%d",&n);
    	if(n==1) 
    		list();
    	else if(n==2)
    		add();
    	else if(n==3)
    		search();
    //	else if(n==4)
    	//	del();
    	else if(n==5)
    		exit(0);
    	else
    	{
    		printf("wrong number!\n");
    		system("pause");	
    	}
    	return 0;
    }
    }
    void search() 
    {
    	int i=0;
    	int m=0;
    	char number[20];
    	printf("Please input number to search :");
    	scanf("%s",number);
    	if ((fp=fopen ("12.txt","r"))==NULL)
    	{
    		printf("File open error!\n");
    		exit (0);
    	}
    	while(!feof(fp))
    	{
    		
    		fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
    		if(book[i].num==number)
    		{
    			printf("------------------------------------------\n");
    			printf("|  number   | book name | author | pulish |\n");
    			printf("|  %4s     |%4s       |%5s   |%4s    |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
    			printf("------------------------------------------\n");
    			m=1;
    			break;
    		}
    		i++;
    	}
    	if(m==0) 
    		printf("No this book.");
    	fclose(fp);
    	system("pause");
     
    }
     
    void list()
    {
    	int i=0;
    	if ((fp=fopen ("12.txt","r"))==NULL)
    	{
    		printf("File open error!\n");
    		exit (0);
    	}
    	printf("------------------------------------------\n");
    	printf("|  number   | book name | author | pulish |\n");
    	while(!feof(fp))
    	{
    		
    		fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
    		printf("------------------------------------------\n");
    		printf("|  %4s     |%4s       |%5s   |%4s    |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
    		i++;
    	}
    	fclose(fp);
    	printf("------------------------------------------\n");
    	system("pause");
     
    }
     
     
     
    void add()
    {	system("cls");
    	
    	if ((fp=fopen ("12.txt","a"))==NULL)
    	{
    		printf("File open error!\n");
    		exit (0);
    	}
    	printf("Please input number:");
    	scanf("%s",books.num);
    	printf("Please input book name:");
    	scanf("%s",books.name);
    	printf("Please input author:");
    	scanf("%s",books.writer);
    	printf("Please input publish:");
    	scanf("%s",books.pub);
    	fprintf(fp,"%s\ %s\ %s\ %s\ ",books.num, books.name, books.writer, books.pub);
    	fclose(fp);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c