I am designing a Caesar Cipher maker.
It will take characters in an array using
cin.getline()
then compare each character of the text with its numerical values (a = 1, b = 2...) and then add the second shift in the characters (if shift = 1;a = 1 [+1 = 2 = b]).
Well, that was what it is supposed to do.
So here's the problem:
When I enter the text
TEST
and the shift for the code
2
I get
Obviously, v is correct. But u and g(s) are wrong.
This is the code:
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
|
//Includes
using namespace std;
char *entry, text, let[501], choice[2];
unsigned int i = 0, let2[501],result[501], shift,len;
void code ()
{
entry = new char [501];
cout << "Enter text to be converted: ";
cin.getline (entry,501, '\n');
cout << endl << endl;
cout << "Enter the Character to shift in this cipher: ";
cin >>shift;
len = strlen(entry);
while((i < len))
{ (i = i+1);
let[i] = entry[i];
if (let[i] == 'a'|| let[i] == 'A')
{let2[i] = 1;}
//Similar if statements for all alphabets!!
result[i] = let2[i] + shift;
}
while ((i > 0))
{
i--;
if (result[i] == 1)
{text = 'a';}
//Again similar if statements
cout << text;
};
delete [] entry;
}
int main()
{
code();
return 0;
}
|
I needed help with two things.
One:
How do I denote a space for those if statements?i.e. If I enter two words, the code must also return a space in the solution.
Two:
Where's the problem in my code? (and yes I know that the code is overly and needlessly complex)
Thanks in advance.
EDIT: And yes. I forgot to add, I haven't added the modulo operator to the final step, but I will after it begins to work properly!!