This is a code written by me for a dictionary in Turbo C. I want to change the code so that it will be supported by gcc. I need help mainly with getch() function. After display it should wait for a key before clearing the screen. What can I do for that?
#include``
#include``
#include``
#include``
struct node
{
char word[15];
char mean[40];
struct node* link;
}*start,*temp;
void insert(char word[15])
{
struct node *new=(struct node *)malloc(sizeof(struct node));
printf("Enter the meaning of word\n");
fflush(stdin);
scanf("%[^\n]",new->mean);
strcpy(new->word,word);
if(start==NULL)
{
start=new;
new->link=NULL;
}
else if(start->link==NULL)
{
if(strcmp(word,start->word)>0)
{
new->link=NULL;
start->link=new;
}
else
{
new->link=start;
start->link=NULL;
start=new;
}
}
else
{
temp=start;
while(temp->link!=NULL&&strcmp(word,temp->link->word)>0)
{
temp=temp->link;
}
new->link=temp->link;
temp->link=new;
}
printf("Insertion success");
getch();
}
void search(char word[15])
{
// int c;
temp=start;
while(temp!=NULL)
{
if(strcmp(word,temp->word)==0)
{
printf("%s : %s",temp->word,temp->mean);
break;
}
temp=temp->link;
}
if(temp==NULL)
{
char resp;
printf("Word not found\nDo you want to insert it now?(y/n)");
fflush(stdin);
scanf("%c",&resp);
if(resp=='y'||resp=='Y')
{
insert(word);
}
}
//fflush(stdin);
getch(); // scanf("%d",&c);
}
void read()
{
FILE *ptr;
char word[15],mean[40];
ptr=fopen("dict.txt","r");
while(fscanf(ptr,"%s %[^\n]",word,mean)!=EOF)
{
struct node *new=(struct node *)malloc(sizeof(struct node));
strcpy(new->word,word);
strcpy(new->mean,mean);
if(start==NULL)
{
start=new;
new->link=NULL;
}
else
{
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=new;
new->link=NULL;
}
fflush(stdin);
}
}
void display()
{
//int c;
temp=start;
if(start==NULL)
{
printf("Nothing to display");
}
else{
while(temp!=NULL)
{
printf("%s : %s\n",temp->word,temp->mean);
temp=temp->link;
}}
//fflush(stdin);
getch();//scanf("%d",&c);
//sleep(5);
}
void write()
{
FILE *ptr;
ptr=fopen("dict.txt","w");
temp=start;
while(temp!=NULL)
{
fprintf(ptr,"%s %s\n",temp->word,temp->mean);
temp=temp->link;
}
}
void modify(char word[15])
{
temp=start;
while(temp!=NULL)
{
if(strcmp(word,temp->word)==0)
{
printf("Present meaning of %s is %s\nEnter new meaning\n",temp->word,temp->mean);
fflush(stdin);
scanf("%[^\n]",temp->mean);
printf("Word modified successfully");
break;
}
temp=temp->link;
}
if(temp==NULL)
{
printf("Word not found\n");
}
getch();
}
int main()
{
int opt;
char word[15];
start=NULL;
read();
while(1)
{
fflush(stdin);
clrscr();
printf("\t\t\tMENU\n");
printf("\t\t\t____\n");
printf("\t\t 1.Search for a word\n");
printf("\t\t 2.Display All\n");
printf("\t\t 3.Modify a word\n");
printf("\t\t 4.Exit\n");
printf("Enter your option to proceed\n");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:printf("Enter word to search\n");
fflush(stdin);
scanf("%s",word);
search(word);
break;
case 2:display();
break;
case 3: printf("Enter word to modify\n");
fflush(stdin);
scanf("%s",word);
modify(word);
break;
case 4:write();exit(EXIT_SUCCESS);
}
}
//return EXIT_SUCCESS;
}