From: alaric AT oasis DOT novia DOT net (Alaric Dailey) Newsgroups: comp.os.msdos.djgpp Subject: djgpp bug? Date: 10 Mar 1997 10:27:24 GMT Organization: Novia Internetworking <> 28.8kbps dialup; 402/390-2NET Lines: 231 Message-ID: <5g0nmc$k4k@nntp.novia.net> References: <5g0mss$ivc AT nntp DOT novia DOT net> NNTP-Posting-Host: oasis.novia.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp the following file compiles and runs perfectly under linux gcc and Turbo C for dos however when you compile it under djgpp it skips it skips entering the keyword. This is an example program from a book that I have modified because it didn't work correctly under gcc. Imagine my surprise when I found out that it still doesn't work under djgpp. I think that perhaps fflush() isn't working properly. maybe I am wrong could someone tell me what is going on with the following code? /////////////////first file//////////////////// /* tiny_dict.h */ #define SENTENCE_MAX 3 #define ENTRY_MAX 50 /* the following typedefs are just used to simplify how we refer to the structure. Just think of typedefs as aliases. The first typedef replaces the declaration struct dict_entry with dict_entry. The second makes it simpler to refer to a dict_entry pointer */ typedef struct dict_entry dict_entry; typedef dict_entry *dict_entryp; struct dict_entry { char keyword[80]; char sentence[SENTENCE_MAX][80]; } dictionary[ENTRY_MAX]; int entry_count=0; /////////////////////////Next file////////////////////////// /* tiny_dict.c - a fixed length dictionary */ #include #include #include #include #include "tiny_dct.h" /************************************************************** FUNCTION NAME: get_definition PURPOSE: uses gets to receive input of a definition from the user. INPUT: none. OUTPUT: a dict_entry structure AUTHOR: MCD **************************************************************/ dict_entry get_definition(void) { int i; dict_entry out_entry; dict_entry *out_entryp = &out_entry; printf("\n Enter keyword: "); /* since out_entry is a localstructure variable and not a structure pointer we access its members using the dot operator. */ fgets(out_entry.keyword,80,stdin); /* scanf("%s",out_entryp->keyword);*/ fflush(stdin); /*this is so the \n gets flushed out of the buffer */ printf("\n Enter definition of up to %d lines.", SENTENCE_MAX); for(i=0; i < SENTENCE_MAX; i++) { printf("\n Line %d: ",i+1); /*scanf("%s",out_entryp->sentence[i]);*/ fgets(out_entry.sentence[i],80,stdin); } return(out_entry); } /************************************************************** FUNCTION NAME: display_entry PURPOSE: prints to stdout the members of the dictionary structure. INPUT: display_rec - a pointer to a dict_entry structure. OUTPUT: none. AUTHOR: MCD **************************************************************/ /* display entry NOTICE that display_rex is a structure pointer. This will change how we access the members of the structure. */ void display_entry(dict_entryp display_rec) { int i; /* since dispay_rec is a structure pointer we use the arrow operator to access its members. */ printf("Keyword: %s\n",display_rec->keyword); for(i=0; i < SENTENCE_MAX; i++) { printf("%s\n",display_rec->sentence[i]); } } /************************************************************** FUNCTION NAME: list_entries PURPOSE: to print all the dictionaries entries currently in the dictionary. INPUT: none. OUTPUT: none. AUTHOR: MCD **************************************************************/ /* list_entries */ void list_entries(void) { int i; printf("Dictionary Entries\n"); /* dictionary is a global array of structures so we access it with the dot operator. */ for(i=0; i < entry_count; i++) { printf("%s\n",dictionary[i].keyword); } printf("****** End of Entries ******\n"); } /************************************************************** FUNCTION NAME: find_entry PURPOSE: locates an entry in the dictionary array of structures that matches the keyword passed in. INPUT: keyword - a character string OUTPUT: an integer which is the index of the matching entry in the array of dictionary structures. AUTHOR: MCD **************************************************************/ int find_entry(char *keyword) { int i; for(i = 0; i < entry_count; i++) { if(!(strcmp(keyword,dictionary[i].keyword))) { return(i); } } return(-1); } /************************************************************** FUNCTION NAME: main for tiny_dict.c PURPOSE: present a menu of the available functions, retrieve the response from the user and call the appropriate function. INPUT: none OUTPUT: returns an int to the OS AUTHOR: MCD **************************************************************/ int main(void) { int done=0; int choice=0, idx=0; char display_kw[80]; char user_input[5]; char *cleanup; while(!done) { printf("<<<< Your Webster >>>>\n"); printf("1. enter a definition.\n"); printf("2. list all entries.\n"); printf("3. display all entries.\n"); printf("4. exit.\n"); printf("choice: "); scanf("%d",&choice);/* fgets(user_input,2,stdin); choice=atoi(user_input);*/ fflush(stdin); /* this is so the \n gets flushed * out of the buffer */ switch (choice) { case 1: if(entry_count < ENTRY_MAX) { dictionary[entry_count] = get_definition(); entry_count++; } else { printf("\n%d is the Maximum number of entries!\n",ENTRY_MAX); } break; case 2: list_entries(); break; case 3: printf("\n Enter keyword to display: "); fflush(stdin); fgets(display_kw,80,stdin); /*scanf("%s",display_kw);*/ if((idx = find_entry(display_kw)) >= 0) { display_entry(&(dictionary[idx])); } else { printf("\n %s not found.\n",display_kw); } break; case 4: done = 1; break; default: printf("\nInvalid choice - try again.\n"); } /* switch */: } /* while * return 0; } /* main */ -- --Alaric Dailey (alaric AT novia DOT net) :-> ############################################################################# Failing Tao, man resorts to Virtue. Failing Virtue, man resorts to humanity. Failing humanity, man resorts to morality. Failing morality, man resorts to ceremony. Now, ceremony is the merest husk of faith and loyalty; It is the beginning of all confusion and disorder. -Lao Tzu