Sunday 15 January 2012

c - Adding more data to a structure -



c - Adding more data to a structure -

i have created function add together books construction when seek add together more books, previous book deleted. i'm doing wrong?

void addbooks(void) { char chchoice; int cnt = 0; printf("\n\t\t~~add new book\n"); printf("enter isbn: "); scanf("%d", &b[cnt].nid); printf("enter books title: "); scanf("%s", b[cnt].chtitle); printf("the record saved successfully!"); printf("save more books? (y / n) "); scanf("%s", &chchoice); if (chchoice == 'y') { addbooks(); cnt = cnt + 1; } else mainmenu(); }

void addbooks(int cnt) //parameter { char chchoice; // int cnt = 0; printf("\n\t\t~~add new book\n"); printf("enter isbn: "); scanf("%d", &b[cnt].nid); printf("enter books title: "); scanf("%s", b[cnt].chtitle); printf("the record saved successfully!"); printf("save more books? (y / n) "); scanf(" %c", &chchoice); if (chchoice == 'y') { addbooks(cnt+1); } else mainmenu(); }

you need cnt parameter else cnt local,you reset value 0 itself. declare cnt static , if so,don't forget increment cnt before calling function. also,use %c character , not %s.

c

No comments:

Post a Comment