#include<stdio.h>
#include<string.h>

#define count 2000000

void add(char word[],char meaning[])
{
	FILE *fp;
	fp = fopen("words.txt","a");
	fprintf(fp,"%s		=	%s\n",word,meaning);
}

void processing(void)
{

	int i = 0,delay;
	printf("Processing....\n\n");		
	for(delay=0;delay<count;delay++)
			;
	printf("\n");
}
	

void getwords(void)
{

	FILE *fp;
	fp = fopen("words.txt","r");
	if(fp!=NULL)
	{
		printf("\n/**************************** DICITIONARY **********************************/\n\n\n");
		char words;
		while((words = fgetc(fp)) != EOF)
		{
			putchar(words);
		};
		fclose(fp);
	}
	if(fp=NULL)
	{
		printf("Unable to open the file!\n");
	}
		
}
	
int main()
{
	int selection;
	char word[10];
	char meaning[40];
	printf("$$$$ Welcome to Aneesh's Dicitionary $$$$\n\n");
	printf("1. Show the dicitionary .\n");
	printf("2. Add words to dicitionary .\n3. Exit\n");
	printf("\tYour selection (1 , 2 or 3 [to quit]  ) : ");		
	selection=fgetc(stdin);
	if( selection ==  '1')
	{
		processing();		
		getwords();
	
	}
	else if( selection == '2')
	{
		processing();		
		printf("Enter word to add: ");	
		fflush(stdin);			
		fgets(word,9,stdin);
		fflush(stdin);			
		printf("Meaning : ");
		fflush(stdin);
		fgets(word,9,stdin);	
		add(word,meaning);
	}
		
}

In the above program in the main() function:-

in the line no 67 and 71.

what i m trying is to input is two character arrays :-
1. word
2. meaning

but i cannot use scanf();
as it only inputs till a space but i want to input till newline..

problems :-

1. the program only inputs the meaning and not the word.

2. the output when we reach that code is" Enter word to add: Meaning:"
and it will only input the meaning.

Please help with the code so that it works as expected and inputs both the arrays correctly.

Help!

Recommended Answers

All 2 Replies

Do not use fflush(stdin) . Here's why.

Reading the meaning, you are only reading 8 characters. Isn't the meaning longer?

And you did not explain well enough what the problem is. What is the result of the code?

printf("Enter word to add: ");	
		fflush(stdin);			
		fgets(word,9,stdin);
		fflush(stdin);			
		printf("Meaning : ");
		fflush(stdin);
		fgets(word,9,stdin);	
		add(word,meaning);

what i m trying is to input is two character arrays :-
1. word
2. meaning

1. the program only inputs the meaning and not the word.

You are reading into the same array twice. See highlighted.

2. the output when we reach that code is" Enter word to add: Meaning:"
and it will only input the meaning.

You need to fflush(stdout) , not stdin .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.