Thanks Salem. Just finished reading the tutorial on this. It looks like a good option. Your suggestion about implementing map in C - I'm interested in this too, so I started a brief exploration and came up with 'hash tables' - is this the right direction?
I think I had reasonable success with the struct idea (i.e. it worked). I post the header and some of the function definitions here.
If anyone has the time, I'd be interested in tips to making the code leaner and criticisms on style. I'm especially interested in how I could trim down the 'dictAddWord' function. I thought it would be the simplest to write, but it just goes on and on and on...
Code:
#include <stdio.h>
#include <string.h>
typedef struct {
char french[20];
char english[20];
}wordpair;
typedef struct {
int nFilesLoaded;
wordpair word[100];
int length;
int randomIndexList[100];
wordpair randomWord;
int randomCurrentIndex;// keeps track of current random index
}dictionary;
void dictNew(dictionary *d); // initialise dictionary
void dictPrint(dictionary *d);
void dictSaveToFile(dictionary *d, char *filename);
void dictLoadFromFile(dictionary *d, char *filename);
void setRandomList(dictionary *d);
void setRandomWord(dictionary *d);
void dictAddWord(dictionary *d);
void dictEdit(dictionary *d);
wordpair getRandomWord(dictionary *d);
and some of the function definitions from the source file
Code:
void dictSaveToFile(dictionary *d, char *filename)
{
FILE *fp;
fp = fopen(filename, "w");
int length = d->length;
for (int i=0; i <length; i++)
{
fputs (d->word[i].english, fp);
fputs(",", fp);
fputs (d->word[i].french, fp);
fputs("\n", fp);
}
fclose(fp);
}
void setRandomList(dictionary *d)
{
printf("\n\n --- Generating random list ---");
int index, isDifferent, isInRange;
d->randomIndexList[0] = rand()%10;
int count = 1;
while (count < d->length)
{
isDifferent = 1;
isInRange = 0;
index = rand()%1000 * d->length/100;
if (index < d->length) { isInRange = 1; }
for (int i = 0; i < count; i++)
{
if(index == d->randomIndexList[i])
{
isDifferent = 0;
break;
}
}
if (isDifferent == 1 && isInRange == 1)
{
d->randomIndexList[count] = index;
count++;
}
}
for (int i = 0; i <count; i++) { printf("\nIndex %d = Random Number %d", i, d->randomIndexList[i]); }
d->randomCurrentIndex = 0; // resets random index to 0
}
This is the rambling dictAddWordFunction
Code:
void dictAddWord(dictionary *d)
{
if (d->length >49)
printf("\nDictionary is half full");
if (d->length <99)
{
char english[20] = {'\0'};
char french[20] = {'\0'};
printf("\nAdd word to dictionary");
printf("\n\n Type in English Word: ");
char letter;
int i;
letter=fgetc(stdin);
for (i = 0;i<20; i++)
{
if (letter == '\0' || letter == '\n')
break;
else {
english[i] = letter;
letter = fgetc(stdin);
}
}
printf("\n Type in French Word for %s: ", english);
letter=fgetc(stdin);
for (i = 0;i<19; i++)
{
if (letter == '\0' || letter == '\n')
break;
else
{
french[i] = letter;
letter = fgetc(stdin);
}
}
strlcpy(d->word[d->length].english, english, 20);
strlcpy(d->word[d->length].french, french, 20);
d->length += 1;
}
else if (d->length >98)
{
printf("\n Not enough room in dictionary, make a new one");
}
printf("\nResetting random list");
setRandomList(d); // Updates random list, sets currentRandomIndex to 0
}