Return to the BobbyGriggs.com C++ Nook


Chapter 8 - English and French Translation
© 1998 by Bobby Griggs.



// Program name:  Translate

// Purpose:  To translate english to french and french to english.



// Win32 console application



/* Words can be added to the dictionary using the following format.

one

un

two

deux

three

trois

*/



#pragma hdrstop



#include < iostream.h >

#include < fstream.h >

#include < assert.h >

#include < string.h >

#include < stdlib.h >

#include "myprocs.h"



#define FILENAME "translate.dat"

#define MAX 5000

#define SIZE 20



void Load_words(int &c,char e[MAX][SIZE],char f[MAX][SIZE]);

void Input_word(char w[SIZE]);

void Display_translation(int c,char w[SIZE],char e[MAX][SIZE],char f[MAX][SIZE]);



void main()

{

	int count = 0;

	char word[SIZE],english[MAX][SIZE],french[MAX][SIZE];

	bool doagain = true;



	Load_words(count,english,french);

	do

	{

		Input_word(word);

		Display_translation(count,word,english,french);

	}

	while (doagain = Again());

	return;

}



void Load_words(int &c,char e[MAX][SIZE],char f[MAX][SIZE])

{

	ifstream dictionary_file;

	char english_word[SIZE],french_word[SIZE];

	int i = 0;



	strcpy(e[0]," ");

	strcpy(f[0]," ");

	dictionary_file.open(FILENAME);

	assert(!dictionary_file.fail());

	while (!dictionary_file.eof())

	{

		dictionary_file.getline(english_word,sizeof(english_word));

		dictionary_file.getline(french_word,sizeof(french_word));

		i++;

		strcpy(e[i],english_word);

		strcpy(f[i],french_word);

	}

	dictionary_file.close();

	if (i == 0)

		exit(0);

	c = i;

	return;

}



void Input_word(char w[SIZE])

{

	cout << "Enter word to translate ... " << endl;

	gets(w);

	cout << endl << endl;

	return;

}



void Display_translation(int c,char w[SIZE],char e[MAX][SIZE],char f[MAX][SIZE])

{

	bool found = false;

	int i;



	for (i = 1;i <= c;i++)

	{

		if (strcmp(w,e[i]) == 0)

		{

			found = true;

			cout << f[i] << endl << endl;

			return;

		}

		else if (strcmp(w,f[i]) == 0)

		{

			found = true;

			cout << e[i] << endl << endl;

			return;

		}

	}

	if (!found)

		cout << "Not in dictionary!" << endl << endl;

	return;

}