Need help for changing tc code to gcc

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

Make the following three changes

  • Remove #include<conio.h>
  • Replace clrscr() with system("clear")
  • Replace getch() with getchar()

This should get your code ready to properly compile for gcc.

3 Likes

Please select your whole code and mark it as code segment by clicking on 101010 image at the top of the post editor. Currently few character like * which are reserved for markdown are lost due to this and it makes the code unreadable. If that doesn’t help paste your code elsewhere like pastebin/ideone and post a link here, I usually recommend this for lengthy codes as that keeps the post length small.

1 Like

Simple getchar() donot pause the program.

Ideally It should since getchar() tells the program to take a single character input. If it doesn’t then it could be a result some gcc optimization, I am not sure though as I am no expert in gcc. Can you give me a small example where it doesn’t work.

Consider the same code with modifications

https://docs.google.com/open?id=0B3ASmlgtnloDd1lSYUYwTktRemk1UlU4dHk2cEtDUQ

In this code getchar() donot pause the program after display function and after search function.

no need of conio.h header file and also use:-
using namespace std;
statement straight after pre-processor directives