Hey everyone! I want to make a program that acts something like a dictionary. Meaning: i have a file dictionary.txt with words and their definitions each on one line like this:
Dog
A domestic animal bla bla bla.
Bedroom
A room in which you sleep.
and so on... and i want my program to do the following:
1.When i give a word i want it to return the definition of that word, or if the word is not in dictionary.txt i want it to display a message like "the word is not there".
2.I want to add words to the dictionary with their meanings so that if i want to search for this new added word later with function 1. it will display the meaning.
The code i got so far is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
FILE *f,*g;
char cuv1[30][200],cuv2[30][200],def[30][300], c[30],o[30][200],p[30],i,n,j,z;
int aux=0;
char cuva[30],defa[300];
void citire() //"citire" means read.. with this function i read from the txt
{i=0;
while(!feof(f))
{
fgets(cuv1[i],500,f);
fgets(def[i],500,f);
i+=1;
}
n=i;
for(i=0;i<=n;i++)
{
for(j=0;j<strlen(cuv1[i])-1;j++)
cuv2[i][j]=cuv1[i][j];
}
}
void cauta() //"cauta" means search..with this one i search for the word
{ // and if it is there i print its meaning
printf("%s%","Introduceti un cuvant: ");
scanf("%s",c);
for(j=0;j<=strlen(c);j++)
p[j]=tolower(c[j]);
for(i=0;i<=n;i++)
{
for(j=0;j<=strlen(cuv2[i]);j++)
{
o[i][j]=tolower(cuv2[i][j]);
}
}
for(i=0;i<=n;i++)
{
if(strcmp(p,o[i])==0)
{
printf("%s",def[i]); aux=1;
}
}
if(!aux)
printf("%s","Cuvantul nu exista in baza de date.\n");
}
void adauga() //"adauga" means add.. i use it to add new words with their
{ // meaning in the .txt file
printf("%s","Introduceti cuvantul ce doriti sa fie adaugat: ");
gets(cuva);
for(j=0;j<=strlen(cuva);j++)
{cuva[j]=tolower(cuva[j]);}
cuva[0]=toupper(cuva[0]);
printf("%s","Introduceti o definitie pentru cuvantul adaugat: ");
gets(defa);
for(j=0;j<=strlen(defa);j++)
{defa[j]=tolower(defa[j]);}
defa[0]=toupper(defa[0]);
fprintf(f,"\n%s\n%s%s",cuva,defa,".");
}
int main()
{f=fopen("dictionar.txt","a+");
citire();
cauta();
adauga();
citire();
cauta();
getchar();
fclose(f);
return 0;}
|
The problems i have so far are: (and i know that maybe the code i got is a little messed up and unorganized with a lot of memory allocated for nothing but anyways)
1. If i call the functions in the order that i called them in main like citire() then cauta() then adauga() after i search for the given word and the program prints its meaning, i get "Introduceti cuvantul ce doriti sa fie adaugat: Introduceti o definitie pentru cuvantul adaugat: ". I mean the program is not waiting for me to type the word and then print "introduceti o definitie" and then type the definition. It just prints both lines and read only one word that i give.
2.If i call the functions in other order like citire() adauga() cauta() the program works just fine. But i want it, after every added word and its meaning from adauga() to read the hole .txt file again so that i can search for the words later. So i want to call them like this citire() adauga() citire() cauta() but after i add the word and the defintion the program just stops and in my dictionary.txt i get a hole copy of what is already written in there plus alot of blank spaces and new lines.
3.I also wanted to make a menu for the program like :
1. Search for a word
2. Add a word
3. Show dictionary
4. Exit
Choose the number:
and i did it something like this:
int z;
citire();
printf("1. Search for a word");
.
.
.
scanf("%d",z);
if(z==1)
cauta();
.
.
the problem here was that when i pressed 1 and hit enter it just gave me a "Program has stopped working". And when i tried to debug it told me something like Access violation error(Segmentation fault) or something like that.
If anyone can tell me what did i do wrong please do.
Looking forward to your replies. Thanks in advance.