mMorse.txt
/* Morse Code Converter - Morse code is a code where each letter of the
English alphabet, each digit, and various punctuation characters are
represented by a series of dots and dashes. This program asks the user
to enter a string, and then converts that string to Morse code. */
#include "Utility.h"
/* Asks the user if he or she wants to repeat the process,
returns the decision */
char tryAgain();
/* Reads in a file containing letters a-z, the numbers 0-9, and a set
of punctuation characters,
stores the contents in a char vector */
int getAlpha(vector<char> &);
/* Reads in a file containing the Morse code equivalents of letters
a-z, the numbers 0-9, and a set of punctuation characters,
stores the contents in a string vector */
int getMorse(vector<string> &);
/* Converts a sentence from English to Morse Code,
returns the sentence */
string toMorse(const string, const vector<char>, const vector<string>);
int main()
{
vector<char> alpha;
vector<string> morse;
int fOpenAlpha = 0,
fOpenMorse = 0;
char again = ' ';
string english,
morseCode;
cout << "\n\tMORSE CODE TRANSLATOR\n";
fOpenAlpha = getAlpha(alpha);
fOpenMorse = getMorse(morse);
if (fOpenAlpha != -1 && fOpenMorse != -1)
{
do
{
cout << "\n\tEnter a sentence in English, and I will translate "
"it to Morse code for you:\n\t";
getline(cin, english);
cout << "\n\tHere is your sentence in Morse code:\n\n"
<< (morseCode = toMorse(english, alpha, morse)) << "\n";
again = tryAgain();
if (again == 'N')
{
cout << "\n\tHave a nice day!\n\n";
}
} while (again != 'N');
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: tryAgain
This function asks the user if he or she wishes to try
again. This decision is returned.
********************************************************** */
char tryAgain()
{
char again = ' ';
cout << "\n\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
/* Input validation */
while (toupper(again) != 'Y' && toupper(again) != 'N')
{
cout << "\n\tDo you wish to try this again? ";
cin >> again;
cin.ignore();
}
return toupper(again);
}
/* **********************************************************
Definition: getAlpha
This function reads in a file called 'mAlpha.txt', which
contains the letters a-z, numbers 1 through 9, and a set
of punctuation characters. The contents of this file is
stored in a string vector. If the file cannot be openend,
the user is informed by a message, and the function will
exit with code -1.
********************************************************** */
int getAlpha(vector<char> &alpha)
{
ifstream alphabet;
char mAlpha;
alphabet.open("mAlpha.txt");
if (alphabet && !alphabet.eof())
{
while (alphabet >> mAlpha)
{
alpha.push_back(mAlpha);
}
}
else
{
cout << "\n\tFile open error: The file 'mAlpha.txt' could not be\n"
<< "\topened or processed. Make sure that the filename is\n"
<< "\tcorrect and the file is not damaged or has been moved\n"
<< "\tfrom the program folder.\n\n"
<< "\tPress enter to exit this program ...";
return -1;
}
alphabet.close();
return 0;
}
/* **********************************************************
Definition: getMorse
This function reads in a file called 'mMorse.txt', which
contains the Morse code equivalents of the letters a-z,
numbers 1 through 9, and a set of punctuation characters.
The contents of this file is stored in a string vector.
If the file cannot be openend, the user is informed by a
message, and the function will exit with code -1.
********************************************************** */
int getMorse(vector<string> &morse)
{
ifstream morseAlpha;
string mMorse;
morseAlpha.open("mMorse.txt");
if (morseAlpha && !morseAlpha.eof())
{
while (getline(morseAlpha, mMorse))
{
morse.push_back(mMorse);
}
}
else
{
cout << "\n\tFile open error: The file 'mMorse.txt' could not be\n"
<< "\topened or processed. Make sure that the filename is\n"
<< "\tcorrect and the file is not damaged or has been moved\n"
<< "\tfrom the program folder.\n\n"
<< "\tPress enter to exit this program ...";
return -1;
}
morseAlpha.close();
return 0;
}
/* **********************************************************
Definition: toMorse
This function accepts a string object and two vectors as
arguments. It translates a sentence into Morse code. The
string object containing the translation is returned.
********************************************************** */
string toMorse(const string input, const vector<char> alpha,
const vector<string> morse)
{
unsigned int startScan = 0,
index = 0;
string morseCode;
for (startScan = index; startScan < input.length(); startScan++)
{
for (index = 0; index < morse.size(); index++)
{
if (tolower(input.at(startScan)) == alpha.at(index))
{
morseCode.append(morse.at(index) + " ");
}
}
if (isspace(input.at(startScan)))
{
morseCode.insert(morseCode.length(), " ");
}
}
return morseCode;
}
No comments:
Post a Comment