Hellow, i am writing a program that will decode and encode messages. (A = 1, B = 2, space = 0, etc.) I am having trouble with my encode, specifically inputting the string and then converting it. I think i have my function right, but do i need a loop and/or convert to array? Also, my decode function is outputting only question marks, i don't know why.
// lettercode class
#include "stdafx.h"
#include "LetterCodeLogic.h"
#include <string>
#include <vector>
#include <sstream>
usingnamespace std;
usingnamespace System;
string LetterCodeLogic::Encode(string m)
{
//process character of string
//use int = char feature to get value (w/offset)
//space is ASCII 32
int c;
unsignedint i;
stringstream result;
for (i = 0; i < m.length(); i++)
{
if (m.at(i) == ' ')
{
c = 0;
}
else
{
if (m.length() > 0)
{
m.at(i) = toupper(m.at(i));
c = (((int)(m.at(i))) - 64);
}
}
result << c<<" ";
}
return result.str();
}
string LetterCodeLogic::Decode(vector<int> letters)
{
char c;
unsignedint i;
stringstream result;
for (i = 0; i < letters.size(); i++)
{
if (letters[i] == 0)
{
c = ' ';
}
else
{
if (letters[i] < 1 || letters[i] > 26)
{
//out of bounds
c = '?';
}
else
{
//convert to char based on ASCII coding
c = (char)(letters[i] + 64);
}
}
result << c;
} // end of for loop
return result.str();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// letter code header
#pragma once
#include <string>
#include <vector>
usingnamespace std;
usingnamespace System;
class LetterCodeLogic
{
public:
static string Encode(string m);
static string Decode(vector <int> letters);
private:
};
Got the getline to work, now onto the function in encoding. If I take out the if statement, lines 73-76, excluding the cout then I get an output to the console. Not what I want, however, I get 48 repeating many times. It isn't an infinite loop.
Yea, I realized that and fixed it. I now don't get any compile errors and my encode returns a value, but it is always in the form of so many '48' repeating.
I took out that if statement for now; I am still getting wrong output (excluding space errors). I am not converting it right obviously, but can anyone point me in the right direction?
Almost done, i am getting a correct output except for the spaces problem. I get the ASCII char for space, 48, instead of a space. Should I check for spaces after I use the function to convert?
Last question; how would I insert a space into the output of the string of numbers from the encode function? ex) instead of 51023405897 it would be 5 1 0 23 4 0 5 8 9 7 (zeros still representing spaces)
Fixed that, sorry for the numbers of posts by the way. How should I start validation of the input string in the encode part? I don't want numbers.
I don't quite follow. Are you saying there is a conflict with 0s and spaces because a space always encodes as a zero?
If that's the case then you have a problem because when you decode it there is no way to differentiate between the two. One way is for the encoder to completely ignore spaces?
There are plenty of other alternatives but they would be complicate - when a space is detected encrypt the word 'space' into the cypher text. Or use a more comprehensive alphabet and include symbols.
Keep in mind that a Caesar code is easily cracked anyway so the simplest is the best answer. I,d ignore spaces and live with the fact that the decoded text is slightly, but not impossible, hard to read.
I fixed the zero/space problem i was having. How can i check the input string for numbers, or only having letters and spaces? Actually, to be more specific, I need to change the incorrect characters (not letters or spaces) into a '99'. I am lost on how to start doing this for a string.