Question

In: Computer Science

In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...

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.

Solutions

Expert Solution

Short Summary:

  • Created separate functions to handle each functionality and the program is menu driven.
  • If user enters invalid menu choice, it throws an error and ask user to enter input again until it finds valid input.
  • Created common function for loading text files to English and Spanish arrays
  • It is case insensitive when finding words in array.

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!


Related Solutions

Create a program that translates English into Pig Latin. The function will take the first letter...
Create a program that translates English into Pig Latin. The function will take the first letter of each word in the sentence only if it’s a not a vowel, and place it at the end of the word followed by “ay”. Your program must be case ​ins​ ensitive. You must write the functiontranslate() ​that takes in a single English word and ​returns​ its Pig Latin translation. Remembertranslatemusttake​onlyonewordasinput.​ ############################################################# # translate() takes a single english word translates it to #pig latin...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
In the C programming language, implement the translation from regular English to Euroglish. If the letters...
In the C programming language, implement the translation from regular English to Euroglish. If the letters were uppercase, keep them uppercase in the replacement. 1. Remove one“e”from the end of words that are more than three characters long, if they happen to end in “e”. 2. Change all double letters to a single letter (including double spaces). Do not remove double line spacing (i.e. “\n”). 3. When a word ends in “ed”, change that to just “d”. Text: The cat...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
Programing lanugaue is C++ Purpose of code: Plan and code a menu-driven modular program utilizing an...
Programing lanugaue is C++ Purpose of code: Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array...
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT