本文介绍了这不行!我想在我的图书馆记录中添加新书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{	FILE *librecord;
	char another='Y';
	struct book
	{	
		char bname[40];
		int bid;
	};
	struct book b[100];
	librecord=fopen("librecord.txt","w");
	if(librecord==NULL)
	{	
		puts("Cannot open file!");
		exit(1);
	}
	while(another=='Y')
	{
		printf("\nEnter name and id of Book\n");
		scanf("%s %d\n",book.bname,&book.bid);
		fprintf(librecord,"%s %d\n","book.bname,book.bid");
		printf("Add Another Record(Y/N)");
		another=getche();
	}
	fclose(librecord);
	return 0;
}





我的尝试:



我已经尝试过很多次了。实际上我想创建一个向我的库添加书籍的功能,用户输入他们独特的ID。



What I have tried:

I have tried it many times. Actually i want to create a function of adding books to my library,with their unique ids entered by the user.

推荐答案

int onBook = 0;



你创建了一个名为b的书籍数组,现在你使用它(如果你不使用它,为什么要创建它)..这个行错了


You created an array of books called b now you use it (why create it if you dont use it) .. this line is wrong

scanf("%s %d\n",book.bname,&book.bid);



应该是


should be

scanf("%s %d\n",b[onBook].bname,&b[onBook].bid);



获取它onBook是数组中的书索引b你正在加载数据。



我会当你按下Y时,让它给你增加onBook,你可能需要确保它不会增加数组的长度b。


Get it onBook is the book index in array b your are loading the data into.

I will leave it to you to increment onBook when you press "Y" and you probably need to make sure it doesn't increment pas the length of the array b.


这篇关于这不行!我想在我的图书馆记录中添加新书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:43