Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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;
}


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.
Posted
Updated 19-Jun-16 17:09pm
v2
Comments
Andreas Gieriet 19-Jun-16 21:40pm    
What exactly is the symptom?
- you have no variable book
- the fprintf call does have hard coded non-sense string arguments
- the b[100] is not used
- you do not check for errors from scanf

Cheers
Andi

1 solution

What Andreas Gierit said but I will expand a little as you are missing a variable
You need a number of the book you are on which starts at zero and increments each time you press "Y" so it starts like
int onBook = 0;

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);

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900