Hello everyone! I have to make a program - french-english dictionary controled by a menu with three functions: search, add and remove words. I have a problem with the add function but i don't now what is the problem. I'm posting the entire code here just in case:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char words[][2][40]={
{"adorer","adore"},
{"gras","greasy"},
{"outil","tool"},
{"radis","radish"},
{"tracer","trace"},
{"",""}
};
void Search(void);
int menu(void);
void Save(void);
void Load(void);
void Display(void);
void Add(void);
int main(void){
int choice;
//Load();
do{
choice = menu();
switch(choice){
case 1: Load();
break;
case 2: Save();
break;
case 3: Search();
break;
case 4: Display();
break;
case 5: Add();
break;
}
}while(choice!=6);
return 0;
}
int menu(void){
char str[80];
int i;
printf("1. Load\n");
printf("2. Save\n");
printf("3. Search\n");
printf("4. Display\n");
printf("5. Add new words\n");
do{
printf("Enter your choice:(1-5) \n");
gets(str);
i=atoi(str);
printf("\n");
}while(i<1 || i>5);
return i;
}
void Search(void){
char word_fr[40];
int i, size;
size=sizeof(words)/sizeof(words[0]);
printf("Enter french word: ");
gets(word_fr);
for(i=0; i < size; i++)
if(!strcmp( word_fr, words[i][0]))
printf("%s %s\n", words[i][0], words[i][1]);
}
void Save(void){
FILE *fp;
int i,size;
size=sizeof(words)/sizeof(words[0]);
if(( fp=fopen( "diction.txt", "w" )) == NULL) {
printf("Save says: Cannot open file.\n");
exit(1);
}
for(i=0; i < size; i++) {
fprintf( fp, "%s %s ", words[i][0], words[i][1]);
}
fclose(fp);
}
void Load(void)
{
FILE *fp;
int i,size;
size=sizeof(words)/sizeof(words[0]);
if(( fp=fopen( "diction.txt", "w+" )) == NULL ) {
printf("Load says: Cannot open file.\n");
exit(1);
}
for(i=0; i < size; i++) {
fscanf( fp, "%s %s", words[i][0], words[i][1]);
}
fclose(fp);
printf("The file was loaded successfully.\n");
}
void Display(void)
{
int i,size;
size=sizeof(words)/sizeof(words[0]);
for(i=0; i < size; i++) {
printf("%s %s\n", words[i][0], words[i][1]);
}
}
void Add(void)
{
char str[40];
int size;
size=sizeof(words)/sizeof(words[0]);
printf("Enter french word:\n ");
gets(str);
strcpy(words[size][0], str);
printf("Enter english word:\n ");
gets(str);
strcpy(words[size][1], str);
Save();
}
I'm using Turbo C/C++ because i'm obliged to present my project on this compiler. I'll be thankful if someone helps me.