Question: C++ Programming: Need help with this section: Develop a function that has the following signature: void PlayMorseCode(string sDotDashCode) The job of this function is to
C++ Programming:
Need help with this section:
Develop a function that has the following signature:
void PlayMorseCode(string sDotDashCode)
The job of this function is to play an audio tone (beep) that corresponds to the "dot"s and "dash"es in the string "sDotDashCode" which contain "dot"s and "dash"es and not "-" or ".".
Your function must use std::string functions to parse the "sDotDashCode" string. I recommend functions like find(), length(), substr(), and erase().
To play a "beep (Links to an external site.)Links to an external site." on the Windows computer, use the function "
Beep(1750, 500);
To play "dot" and
Beep(750, 1000);
To play "dash".
Here is my code:
// Quiz 6.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include #include #include #include "Windows.h" #include
using namespace std;
typedef struct { char character; //the character which is to be encoded string morsecode; // the morse code sequence containing words dash dot }Code; class CMorseConvert { Code codes[100]; int count; //the number of elements in the codes array; //convert a string such as .-. to dot dash dot string convertCode(string morsecode) { string dashdotString = ""; int len = morsecode.length(); for (int i = 0; i < len; i++) { if (morsecode[i] == '.') dashdotString = dashdotString + "dot " + " "; else if (morsecode[i] == '-') dashdotString = dashdotString + "dash " + " "; else dashdotString = dashdotString + morsecode[i]; } return dashdotString; } //search the list of n codes for the character ch and return its morse code (dash dot sequence) string getCode(char ch) { if (ch == ' ') return " "; //convert alphabet to uppercase if (isalpha(ch)) ch = toupper(ch); for (int i = 0; i < count; i++) { if (codes[i].character == ch) return codes[i].morsecode; } return "NOCODE"; //if character is not found then return it } public: //loads the file containing morse code into the array and returns the count CMorseConvert() { ifstream morseFile("codes.dat"); if (!morseFile.is_open()) { cout << "Could not open morseFile "; exit(1); } string line; count = 0; while (morseFile >> line) { int index = line.find(","); char ch = line[index - 1]; //get the character before comma string morsecode = line.substr(index + 1); //get the morse code after comma if (isalpha(ch)) //convert alphabet to uppercase ch = toupper(ch); codes[count].character = ch; //convert the morse code into a string of words dash and dot codes[count].morsecode = convertCode(morsecode); count++; } morseFile.close(); } string Text2Morse(const string sText) { string morseCode = ""; int len = sText.length(); for (int i = 0; i < len; i++) morseCode = morseCode + getCode(sText[i]);//get morsecode of character return morseCode; //return morse code } }; int main() { string stext, mcode; CMorseConvert converter; cout << "Enter the text to be converted to morse code:"; getline(cin, stext); mcode = converter.Text2Morse(stext); cout << mcode << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
