Question: I cannot get these functions to work with my code, so used different code. Here are the functions I have to use: 1) bool LoadMorseCodes(Code
I cannot get these functions to work with my code, so used different code.
Here are the functions I have to use:
1) bool LoadMorseCodes(Code arrCodes[], const int iTotalCount)
{
int i;
ifstream inFile("codes.dat");
if (!inFile.is_open())
return false;
string line;
for (i = 0; i
{
getline(inFile, line);
size_t pos;
pos = line.find(",");
arrCodes[i].symbols = line[0];
arrCodes[i].morse_code = line.substr(pos + 1);
}
return true;
}
2) bool LoadMorseCodes(Code arrCodes[], const int iTotalCount)
{
int i;
ifstream inFile("codes.dat");
if (!inFile.is_open())
return false;
string line;
for (i = 0; i
{
getline(inFile, line);
size_t pos;
pos = line.find(",");
arrCodes[i].symbols = line[0];
arrCodes[i].morse_code = line.substr(pos + 1);
}
return true;
}
3)string GetCode(char c, Code arrCodes[], const int iTotalCount) { int i; for (i = 0; i { if (c == arrCodes[i].symbol) return arrCodes[i].code; } return "Invalid"; }
My actual code I am using now, that needs to be updated:
#include "stdafx.h" #include #include #include #include #include "Windows.h"
using namespace std;
struct Code { char symbols; string morse_code; };
class MorseConvert { Code codes[100]; int count;
string ConvertCodeToDotDash(string sMorseCode) { string dashdot = ""; int len = sMorseCode.length(); for (int i = 0; i < len; i++) { if (sMorseCode[i] == '.') dashdot = dashdot + "dot " + " "; else if (sMorseCode[i] == '-') dashdot = dashdot + "dash " + " "; else dashdot = dashdot + sMorseCode[i]; } return dashdot; }
string GetCode(char c) { if (c == ' ') return " "; if (isalpha(c)) c = toupper(c); for (int i = 0; i < count; i++) { if (codes[i].symbols == c) return codes[i].morse_code; } return "Invalid"; } public: MorseConvert() { ifstream inFile("codes.dat"); if (!inFile.is_open()) { cout << "Invalid"; exit(1); } string line; count = 0; while (inFile >> line) { int index = line.find(","); char c = line[index - 1]; string morsecode = line.substr(index + 1); if (isalpha(c)) c = toupper(c); codes[count].symbols = c; codes[count].morse_code = ConvertCodeToDotDash(morsecode); count++; } inFile.close(); } string SymbolsToMorse(const string sText) { string morsecode = ""; int len = sText.length(); for (int i = 0; i < len; i++) morsecode = morsecode + GetCode(sText[i]); return morsecode; } };
int main() { string stext, mcode; MorseConvert converter; cout << "******************** Welcome to the Text to Morse Code Converter! ******************** ";
cout << "Enter your text and it will be converted into Morse Code:"; getline(cin, stext); mcode = converter.SymbolsToMorse(stext); cout << mcode << endl; system("pause"); }
Codes.dat file contents:
A,.- B,-... C,-.-. D,-.. E,. F,..-. G,--. H,.... I,.. J,.--- K,-.- L,._.. M,-- N,-. O,--- P,.--. Q,--.- R,.-. S,... T,- U,..- V,...- W,.-- X,-..- Y,-.-- Z,--.. 1,.---- 2,..--- 3,...-- 4,....- 5,..... 6,-.... 7,--... 8,---.. 9,----. 0,-----
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
