Code:
/************************************************************
* By: Aldis Berjoza <killasmurf86@gmail.com>
* Date: Sat 24 Oct 2009
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 5
struct s_book {
char title[100];
char author[100];
int year;
char publishing[100];
};
typedef struct s_book t_book;
int main (void) {
t_book *book;
int i,
from,
to;
char tmp[5];
if ((book = (t_book *) calloc(MAX_BOOKS, sizeof(struct s_book))) == NULL) {
perror(NULL);
exit(1);
}
printf("Input data!\n\n\n");
for (i=0; i<MAX_BOOKS; i++) {
printf("Author: ");
scanf("%100s", book[i].author);
// fgets(book[i].author, 100, stdin);
printf("Title: ");
scanf("%100s", book[i].title);
// fgets(book[i].title, 100, stdin);
printf("Publishing: ");
scanf("%100s", book[i].publishing);
// fgets(book[i].publishing, 100, stdin);
printf("Year: ");
scanf("%d", &(book[i].year));
// fgets(tmp, 5, stdin);
// book[i].year = atoi(tmp);
printf("ok\n=========================\n\n");
}
// clrscr();
printf("Search from year: ");
scanf("%d", &from);
printf("Search to year: ");
scanf("%d", &to);
printf("=================\n");
for (i=0; i<MAX_BOOKS; i++) {
if ((book[i].year >= from) && (book[i].year <= to)) {
printf("%s - \"%s\", %s, %d\n",
book[i].author,
book[i].title,
book[i].publishing,
book[i].year);
}
}
free(book);
return 0;
}
I don't understand why this stupid app sometimes skip asking some fields....
What did I code wrong?
Please enlighten me.