- #1
magnifik
- 360
- 0
i am trying to write a program that encodes and decodes. my code works fine for encoding 1 string of characters with no spaces, but when there is any type of white space, it fails. also, when i try to decode, i get a random mix of characters and symbols. any help would be appreciated.
for example (using the keyword lemon)
input text file: attackatdawn
output text file (encode): lxfopvefrnhr // success!
input text file: attack at dawn
output text file (encode): lxfopvXmhaoeib // FAIL...the spaces should merely show up as spaces
input text file: lxfopvefrnhr
output text file (decode): aÂ#„åF§iÊ+Œí // FAIL...idk what this is
for example (using the keyword lemon)
input text file: attackatdawn
output text file (encode): lxfopvefrnhr // success!
input text file: attack at dawn
output text file (encode): lxfopvXmhaoeib // FAIL...the spaces should merely show up as spaces
input text file: lxfopvefrnhr
output text file (decode): aÂ#„åF§iÊ+Œí // FAIL...idk what this is
Code:
void perform(istream& inf, ostream& outf, string key, char choice)
{
string text;
while(getline(inf, text))
{
for (int i = 0; i < text.length(); i++)
{
if (key.length() <= text.length())
key += key[i];
}
// encode
if (choice == 'e')
{
for (int j = 0; j < key.length(); j++)
{
key[j] = tolower(key[j]);
alpha_to_num(key[j]);
}
for (int k = 0; k < text.length(); k++)
{
text[k]= tolower(text[k]);
alpha_to_num(text[k]);
}
for (int l = 0; l < text.length(); l++)
{
eForm(c, text[l], key[l]);
num_to_alpha(c);
outf << (char)c;
}
}
// decode
else if (choice == 'd')
{
for (int j = 0; j < key.length(); j++)
{
key[j] = tolower(key[j]);
alpha_to_num(key[j]);
}
for (int k = 0; k < text.length(); k++)
{
text[k]= tolower(text[k]);
alpha_to_num(text[k]);
}
for (int l = 0; l < text.length(); l++)
{
dForm(c, text[l], key[l]);
num_to_alpha(c);
outf << (char)c;
}
}
}
}
int main(){
char choice;
string key;
string input;
string output;
ifstream inf;
ofstream outf;
cin >> choice >> key >> input >> output;
inf.open(input.c_str());
outf.open(output.c_str());
perform(inf, outf, key, choice);
}