In: Computer Science
In C++ Language
English/Spanish Translation Program.
Create a menu driven program that translates English to Spanish and Spanish to English.
Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first word in the SPAN.txt file; the second word in the ENG.txt file corresponds to the second word in the SPAN.txt file, and so on).
Read each file into an array, one array for English words and one for Spanish words. The text files provided each have 100 words. Use a menu driven process.
A message should be displayed if the selected word is not in the dictionary and an error message is displayed if you select an incorrect menu option. Your program must be in a loop so that you can make multiple selections without having to restart your program each time.
Short Summary:
Source code:
//My Translator C++
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;
//prints menu options
void print_menu()
{
cout << "Welcome to My Translator" << endl;
cout << "***** Menu *****" << endl;
cout << "1. English to Spanish" << endl;
cout << "2. Spanish to English" << endl;
cout << "3. Quit" << endl;
}
//Getting user menu choice
int get_choice()
{
int choice;
//Loop until it finds a valid menu choice
do{
cout << "Enter your choice (1-3): ";
cin >> choice;
//if it is (1-3), then treat as valid input
if (choice > 0 && choice < 4)
{
break;
}
//print an error message for invalid choice
cout << "Invalid Choice!" << endl;
}while(true);
return choice;
}
//Loading text file lines into
array
void load_text_file(string language[], string filename)
{
ifstream file;
file.open(filename.c_str());
//if it falis to read
if(file.fail())
{
cout << "unable to open " << filename <<
endl;
}
else //valid file
{
int index = 0;
//Max 100 words or end of file
while(file >> language[index] && index <
100)
{
index++;
}
file.close();
}
}
//Get user search word
string get_SearchWord(string inputlabel)
{
string searchword;
cout << inputlabel;
cin >> searchword;
cin.ignore();
return searchword;
}
//Search the word in source array and
returns corresponding index
int search_Word(string word, string basedictionary[], int
size)
{
//Making it as case insensitive, when finding words.
//convert both dictionary word and word to be searched into lower
case
std::transform(word.begin(), word.end(), word.begin(),
[](unsigned char c){ return std::tolower(c); });
for ( int i = 0; i < size; i++ ) {
string dictword = basedictionary[i];
std::transform(dictword.begin(), dictword.end(),
dictword.begin(),
[](unsigned char c){ return std::tolower(c); });
//if it finds the owrd, return the Corresponding index
if (dictword == word) {
return i;
}
}
//nothing found, return -1
return -1;
}
int main()
{
//Load English array
string English[100];
load_text_file(English, "ENG.txt");
//load Spanish array
string Spanish[100];
load_text_file(Spanish, "SPAN.txt");
//varaibles to store user choice and word
int choice;
string searchword;
int searchindex;
do
{
//Print menu
print_menu();
//Get user menu choice
choice = get_choice();
switch(choice)
{
//1. English to Spanish
case 1:
{
//get search word
searchword = get_SearchWord("Enter a english word: ");
//Pass the searh word and get the index
searchindex = search_Word(searchword, English, 100);
//if it is '-1', print error message
if (searchindex == -1)
{
cout << "Unable to find the word: " << searchword
<< endl;
}
else
{
//Print the Corresponding word from spanish array
cout << "Corresponding Spanish word: " <<
Spanish[searchindex] << endl;
}
break;
}
//2. Spanish to English
case 2:
{
//get search word
searchword = get_SearchWord("Enter a spanish word: ");
//Pass the searh word and get the index
searchindex = search_Word(searchword, Spanish, 100);
//if it is '-1', print error message
if (searchindex == -1)
{
cout << "Unable to find the word: " << searchword
<< endl;
}
else
{
//Print the Corresponding word from English array
cout << "Corresponding English word: " <<
English[searchindex] << endl;
}
break;
}
//3. Quit
case 3:
{
cout << "Good Bye! See you soon!";
break;
}
}
cout << "\n";
} while(choice != 3);
return 0;
}
Refer the following screenshots for code indentation:
Sample text files:
Sample output:
Feel free to rate the answer. Let me know, if you have any question through comment section. I am happy to answer.
Happy Studying!