fix encode


Can anyone please help me fix this program

#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
};

Code::Code()
{
alpha=alphacode();
codewords=morsecode();
}

string Code::decode(vector<string> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp += decode(message[i]);
}
return temp;
}

string Code::encode(vector<char> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp+=encode(message[i]);
}
return temp;
}

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;
}

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<string> message;
string temp;
Code c;

cin >> temp;

while (cin.good())
{
message.push_back(temp);
cin >> temp;
}

cout << c.decode(message) << endl;




vector<char> message;
string temp;

getline(cin, temp);
for (int i=0; i <temp.length(); i++)
{
message.push_back(temp[i]);
}

Code C;
cout << C.encode(message) << endl;


}
closed account (Dy7SLyTq)
a) code tags
b) whats wrong with it
Duplicate definition on lines 121 and 138, and on lines 122 and 139.

Now, if only you had used code tags (or done a good thing and enabled line numbers in your IDE) so that you knew which lines these are. >:D

-Albatross
Last edited on
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
So when you tried to give it input after getting the...
++++ ./enc
...line, it did nothing?

-Albatross
Last edited on
i used g+++ -g enc.cpp to compile it and then ./a.out and also tried to compile it Q enc.cpp both ways didn't work it does nothing at all
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.)

-Albatross
Last edited on
nope it doesn't work
> 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()

Also, provide an example input/output.
#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
};

Code::Code()
{
alpha=alphacode();
codewords=morsecode();
}

string Code::decode(vector<string> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp += decode(message[i]);
}
return temp;
}

string Code::encode(vector<char> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp+=encode(message[i]);
}
return temp;
}

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;
}

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()
{

//this is the int main code for decode
vector<string> message;
string temp;
Code c;

cin >> temp;

while (cin.good())
{
message.push_back(temp);
cin >> temp;
}

cout << c.decode(message) << endl;



//this is the int main code for encode
vector<char> message1;
string temp1;

getline(cin, temp1);
for (int i=0; i <temp1.length(); i++)
{
message1.push_back(temp1[i]);
}

Code C;
cout << C.encode(message1) << endl;


}

So i fixed those duplicate i compiled it to see if other errors were there but it compiled fine so i ran it by using ./a.out but it does nothing
Please use code tags (like BB tags with the word code inside them) because it makes our job so much easier.

-Albatross
[code]"Please use code tags"[/code]
┬┐what was your input?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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
};

Code::Code()
{
    alpha=alphacode();
    codewords=morsecode();
}

string Code::decode(vector<string> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
        temp += decode(message[i]);
    }
    return temp;
}

string Code::encode(vector<char> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
        temp+=encode(message[i]);
    }
    return temp;
}

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;						
}

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()
{
    
    //this is the int main code for decode
    vector<string> message;
    string temp;
    Code c;
    
    cin >> temp;
    
    while (cin.good())
    {
        message.push_back(temp);
        cin >> temp;
    }
    
    cout << c.decode(message) << endl;
    
    
    
    //this is the int main code for encode
    vector<char> message1;	 
    string temp1; 
    
    getline(cin, temp1);	 
        for (int i=0; i <temp1.length(); i++)
        {
            message1.push_back(temp1[i]);
        }
    
    Code C;
    cout << C.encode(message1) << endl;
    
   
}


Sorry about that new to the website
Topic archived. No new replies allowed.