Nov 2, 2013 at 12:40pm UTC
Heya, need a tad help or input with my Caesar cipher program, what seems to be the issue is that when I input xzy, or if characters exceed the final alphabet character 'z' it creates strange symbols instead, such as [/].
Anyone kind enough to give me a hint? :)
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
int length;
int shift;
int decenc;
cin >> decenc;
cin >> shift;
cin.ignore();
getline(cin, input);
length = (int )input.length();
for (int count = 0; count < length; count++)
{
if (isalpha(input[count]))
{
if (decenc == 1)
{
for (int i = 0; i < shift; i++)
{
input[count] = toupper(input[count]);
if (input[count] == 'z' )
{
input[count] = 'a' ;
}
else
{
input[count]++;
}
}
}
if (decenc == 2)
{
for (int i = 0; i < shift; i++)
{
input[count] = tolower(input[count]);
if (input[count] == 'a' )
{
input[count] = 'z' ;
}
else
{
input[count]--;
}
}
}
}
}
cout << input;
}
Last edited on Nov 2, 2013 at 2:28pm UTC
Nov 2, 2013 at 2:27pm UTC
So hmm, maybe I should include how my program works first.
first input decides if you want to encrypt or decrypt a message (1 for decrypt and 2 for encrypt)
and second input is how far you want to push back the letters.
third input would be the message you want to encrypt/decrypt.
So, with that, I seem to hit a snag because there are certain "symbols"(e.g [ / ] ) there that are not supposed to be included in the program.
For instance,
if I input:
1
25
Attack at dawn
It produces: ZMMZ/D ZM [ZPG
but more desired output would be without the symbols.
Last edited on Nov 2, 2013 at 2:29pm UTC
Nov 2, 2013 at 2:34pm UTC
26 27
input[count] = toupper(input[count]);
if (input[count] == 'z' ) //¿how this could ever happen?
Last edited on Nov 2, 2013 at 2:35pm UTC
Nov 2, 2013 at 2:57pm UTC
it seems to be working in some cases, if it exceeds 'z', it will continue to go through the start of the alphabet, i.e 'a'.
or so is the initial thought.
Nov 2, 2013 at 3:20pm UTC
Ok, so you were right, it doesn't do jack, how do I fix this?
I've been trying to include something like another while loop but it doesn't seem to be functioning correctly.
Any ideas, anyone?
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
int length = 0;
int shift = 0;
int decenc = 0;
cin >> decenc;
cin >> shift;
cin.ignore();
getline(cin, input);
length = (int )input.length();
//while(getline(cin, input))
{
for (int count = 0; count < length; count++)
{
if (isalpha(input[count]))
{
if (decenc == 1)
{
for (int i = 0; i < shift; i++)
{
input[count] = toupper(input[count]);
{
input[count]++;
}
}
}
if (decenc == 2)
{
for (int i = 0; i < shift; i++)
{
input[count] = tolower(input[count]);
{
input[count]--;
}
}
}
}
}
cout << input;
}
}
my current code.
Last edited on Nov 2, 2013 at 3:21pm UTC
Nov 2, 2013 at 4:13pm UTC
auto is not working with my current compiler for some reason, produces some juicy errors (codeblocks)
I'm thinking, maybe stick with my current, but if all else fails, I will check out that one, thanks JL
Nov 2, 2013 at 5:01pm UTC
> auto is not working with my current compiler for some reason,
You need to enable C++11 support:
In CodeBlocks,
Menu => Settings => Compiler... Compiler settings =>Compiler Flags
check -std=c++11
While you are at it, also check -Wall, -Wextra and -pedantic-errors