#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Code
{
public:
Code(); // Default constructor - loads and uses morse code
string decode(vector<string> message); // decodes a message
string encode(vector<char> message); // encodes 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
char decode(string c); //returns the character for the codeword c
string encode(char c); //returns the string for the codeword c
};
char Code::decode(string c)
{
for (int i=0;i<alpha.size();i++)
{
if(c == codewords[i]) //looks for codeword[i] and matches it with alpha [i]
{
return alpha[i]; //returns c as a char in alpha
}
}
}
string Code::encode(char c)
{
for (int i=0;i<codewords.size();i++)
{
if (c==alpha[i])
{
return codewords[i];
}
}
}
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;
}
Thank you guys it fixed my problem however when i try to run the program it compiles but doesn't do anything it just stays like this
Quickie for Linux...
enc.cpp looks like a C++ program.
+++ g++ -w -I/usr/include -o enc enc.cpp
+++ echo 'Compiled, and Linked OK. Loading...'
Compiled, and Linked OK. Loading...
+++ rm enc.cpp.errors
+++ eval ./enc
++++ ./enc
Not even after you add an end-of-file character to the stream? (Hit ctrl+z at the start of a line on Windows, or ctrl+^d at the start of a line on *nixes.)
> Thank you guys it fixed my problem
I would like to see your fix.
If you are putting eof to terminate the first reading, it is obvious that the second one would not happen as std::cin remains in an invalid state
So you need to cin.clear()
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Code
{
public:
Code(); // Default constructor - loads and uses morse code
string decode(vector<string> message); // decodes a message
string encode(vector<char> message); // encodes 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
char decode(string c); //returns the character for the codeword c
string encode(char c); //returns the string for the codeword c
};
char Code::decode(string c)
{
for (int i=0;i<alpha.size();i++)
{
if(c == codewords[i]) //looks for codeword[i] and matches it with alpha [i]
{
return alpha[i]; //returns c as a char in alpha
}
}
}
string Code::encode(char c)
{
for (int i=0;i<codewords.size();i++)
{
if (c==alpha[i])
{
return codewords[i];
}
}
}
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;
}