I didn't want to ask for help on this, but I am really lost on how to fix this error I am getting when trying to compile my code. I have already created a program that takes Morse Code and turns it into English. Now I have to do the opposite. Much of the first program was already in a skeleton form and we filled in a few parts. Is it because I am using a vector<char> does that not work with push_back somehow? Or maybe it has to do with how I have to take a vector<char> and need it to output a vector<string>?
I am really not sure if this is my only problem but the compiler is only complaining about part of my int main() at line 97.
92 vector<char> codemessage
93 string temp;
94 cin>>temp;
95 while(cin.good())
96 {
97 codemessage.push_back(temp);
98 cin>>temp;
99 }
The error is this:
Code2Morse.cpp: In function ‘int main()’:
Code2Morse.cpp:97: error: no matching function for call to ‘std::vector<char, std::allocator<char> >::push_back(std::string&)’
/usr/include/c++/4.4/bits/stl_vector.h:733: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = char, _Alloc = std::allocator<char>]
My full code is this so it makes a little more sense. Some of the comments could be labeled a little off since most of it was copied over from my Morse to English program.
#include <string>
#include <vector>
#include <iostream>
usingnamespace std;
class Code
{
public:
Code(); // Default constructor - loads and uses morse code
string decode(vector<char> message); // decodes a message
private:
vector<string> codewords; // this is a codeword vector parallel to A-Z
vector<char> alpha; // this is the vector for A-Z
vector<char> alphacode(); // function returns vector - A B C etc
vector<string> morsecode(); // function returns vector containing morse code
string decode(char c); //returns the character for the codeword c
};
Code::Code()
{
alpha=alphacode();
codewords=morsecode();
}
string Code::decode(vector<char> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp+=decode(message[i]); //where the magic happens
}
return temp; //finished encoding and returns
}
string Code::decode(char c) //where the magic happens //char c= message[i]
{
for (int i=0;i<codewords.size();i++) //codewords.size() does that refer to the longest a single codeword can be, or the 28 characters?
{
if (c==alpha[i]) //checks if char c is equal to alpha[i] OR alphacode[i] since alpha=alphacode()
{
return codewords[i]; //it then takes what is equal to alphacode[i] and sends i to codewords OR morsecode() to find corresponding symbols
}
}
}
vector<char> Code::alphacode()
{ // This returns a vector containing the alphabet A-Z and " " and "."
vector<char> temp;
for (char c='A'; c<='Z'; c++) //returns the letters
temp.push_back(c);
temp.push_back(' '); //returns the space space
temp.push_back('.'); //returns the period.
return temp; //this would be the same as typing something like temp[0]='A';temp[1]='B'; ... temp[25]='Z';temp[26]=' ';temp[27]='.'
}
vector<string> Code::morsecode()
{ // This function returns a vector containing the morse code
vector<string> temp(28); //compares c
temp[0] =" .-";
temp[1] =" -...";
temp[2] =" -.-.";
temp[3] =" -..";
temp[4] =" . ";
temp[5] =" ..-.";
temp[6] =" --.";
temp[7] =" ....";
temp[8] =" ..";
temp[9] =" .---";
temp[10] =" -.-";
temp[11] =" .-..";
temp[12] =" --";
temp[13] =" -.";
temp[14] =" ---";
temp[15] =" .--.";
temp[16] =" --.--";
temp[17] =" .-.";
temp[18] =" ...";
temp[19] =" -";
temp[20] =" ..-";
temp[21] =" ...-";
temp[22] =" .--";
temp[23] =" -..-";
temp[24] =" -.--";
temp[25] =" --..";
temp[26] =" .......";
temp[27] =" x";
return temp;
}
int main()
{
vector<char> codemessage; //the message vector
string temp; //temp string for storing each code letter
cin>>temp; //get input of morsecode
while(cin.good()) //while file still has a message left
{
codemessage.push_back(temp); //take each piece of char and put into vector for message. //pieces being a letter
cin>>temp; //reloads the rest of temp to continue taking piece by piece and putting into vector
}
Code C;
cout << C.decode(codemessage) << endl;
}
The problem is that you're trying to add a string to a vector that holds chars, not strings.
So you need to add each character of temp individually, for example: codemessage.insert(codemessage.end(),temp.begin(),temp.end());
Oh shoot, all I needed was to changed the string temp to a char temp. Looks like it works fine now. I'll double check that its decoding properly later, already missed a class from sleeping in. lol
Thanks for the help.
So I can't seem to figure out why it doesn't want to produce the morse code part ' .......' when there is a space ' ' in the text file. Everything else works just fine. The parts of code my teacher gave us didn't work with turning the '.......' into a space and I corrected that, but I can't seem to do the latter. Is the space contained as a char when I have it grab a string of chars?
I think the problem was that cin>>temp was not grabbing the spaces in my text file. Instead I used a getline() to make sure everything was grabbed and from there put each char in the vector.
I fixed it using this
int main()
{
vector<char> codemessage; //the message vector
string temp1; //temp for storing each code letter
getline( cin, temp1); //get input of morsecode
for (int i=0; i <temp1.length(); i++)
{
codemessage.push_back(temp1[i]);
}
Code C;
cout << C.decode(codemessage) << endl;
}