Does anyone know why I\'m not getting the correct output? c++ I shouldn\'t be ge
ID: 3855361 • Letter: D
Question
Does anyone know why I'm not getting the correct output? c++
I shouldn't be getting a new line before the results of the code.
Below is the code.
#include
#include
#include
#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 size; //the number of elements in the codes array;
//convert a string such as .-. to dot dash dot
string convertCode(string morsecode)
{
string dashdot = "";
int len = morsecode.length();
for(int i = 0; i < len; i++)
{
if(morsecode[i] == '.')
dashdot = dashdot + "dot ";
else if(morsecode[i] == '-')
dashdot = dashdot + "dash ";
}
return dashdot;
}
//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 < size; i++)
{
if(codes[i].character == ch)
return codes[i].morsecode;
}
return "NOCODE " ; //if character is not found
}
public:
//loads the file containing morse code into the array and returns the count
CMorseConvert(string filename = "codes.dat")
{
ifstream file(filename.c_str());
if(!file.is_open())
{
cout << "Could not open file " << filename << endl;
exit(1);
}
string line;
size = 0;
while(file >> line)
{
int idx = line.find(",");
char ch = line[idx-1]; //get the character before comma
string morsecode = line.substr(idx+1); //get the morse code after comma
if(isalpha(ch)) //convert alphabet to uppercase
ch = toupper(ch);
codes[size].character = ch;
codes[size].morsecode = convertCode(morsecode); //convert the morse code into a string of words dash and dot
//cout << ch << " " << morsecode << " " << codes[size].morsecode << endl;
size ++;
}
}
string Text2Morse(const string sText )
{
string code = "";
int len = sText.length();
for(int i = 0 ; i < len; i++)
code = code + getCode(sText[i]);
return code;
}
};
int main()
{
string filename;
string text , code;
cout << "Enter the filename containing morse codes:";
getline(cin, filename);
CMorseConvert converter(filename);
cout << "Enter the text to be converted to morse code:";
getline(cin, text);
code = converter.Text2Morse(text);
//cout << " The morse code for the give text is :" << endl;
cout << code << endl;
}
//////////////////////////////////////////////////////////////////////////////////////////Below are the tests I am failing
zy 822. Lab Activity. Text texG chegg Study | Guided S/X O Topic: Week #7 Q&A; G String (Java Platform SE × c Secure ! https://learn.zybooks.com/zybook/MACOMBITCS2530 er2017/chapter/8/section/22 zyBooks Library ITCS 2530 home> 8.22 Lab Activity- Text to Morse Code (Kind of) @ Help/ FAQ .veronica Dean 1: Unit Test 0/10 Test 1 Teeting "Texc2Moree ("abo)" expecting "áot dash dash dct dot dot dash Iest 1Eailed Test feedback Expected "dot dash dash dot dot dot dash dot dash dot" got" dot dash d..h dot dot d dash dot d h d t " 2: Unit Test A 0/10 Test 2 Testing "Text2Morse ("s")" expecting "dot dot dot" Test feedback Expected "dot dot dot" got" dot dot dot". 3: Unit Test 0/10 Test 3 Testing "Texc2Morae (")" expecting "dot dash dash daah dash". . Test 1 -Failed Expected "dot dash dash dash dash" got dot dash dash dash dash " Test feedback 4: Unit Test A 0/10 Test 4 Testing "Text2Morse ("12345") "expecting "dot dash dash dash dash dot Test i- Failed 0 Type here to search 12:14 AM 7/10/2017Explanation / Answer
the code is correct and modified
#include<stdio.h>
#include<conio.h>
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 size; //the number of elements in the codes array;
//convert a string such as .-. to dot dash dot
string convertCode(string morsecode)
{
string dashdot = "";
int len = morsecode.length();
for(int i = 0; i < len; i++)
{
if(morsecode[i] == '.')
dashdot = dashdot + "dot ";
else if(morsecode[i] == '-')
dashdot = dashdot + "dash ";
}
return dashdot;
}
//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 < size; i++)
{
if(codes[i].character == ch)
return codes[i].morsecode;
}
return "NOCODE " ; //if character is not found
}
public:
//loads the file containing morse code into the array and returns the count
CMorseConvert(string filename = "codes.dat")
{
ifstream file(filename.c_str());
if(!file.is_open())
{
cout << "Could not open file " << filename << endl;
exit(1);
}
string line;
size = 0;
while(file >> line)
{
int idx = line.find(",");
char ch = line[idx-1]; //get the character before comma
string morsecode = line.substr(idx+1); //get the morse code after comma
if(isalpha(ch)) //convert alphabet to uppercase
ch = toupper(ch);
codes[size].character = ch;
codes[size].morsecode = convertCode(morsecode); //convert the morse code into a string of words dash and dot
//cout << ch << " " << morsecode << " " << codes[size].morsecode << endl;
size ++;
}
}
string Text2Morse(const string sText )
{
string code = "";
int len = sText.length();
for(int i = 0 ; i < len; i++)
code = code + getCode(sText[i]);
return code;
}
};
int main()
{
string filename;
string text , code;
cout << "Enter the filename containing morse codes:";
getline(cin, filename);
CMorseConvert converter(filename);
cout << "Enter the text to be converted to morse code:";
getline(cin, text);
code = converter.Text2Morse(text);
//cout << " The morse code for the give text is :" << endl;
cout << code << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.