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...
(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.
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT