My version of a Caesar Cipher works, except for one final problem.

Hello,
First off I'd like to say this is my first post on these forums so I apologize for any formatting errors I make. Feel free to critique me on those errors as well as with my code. Also this is my first time getting back into C++ since i took my college classes over a year ago so I'm a little rusty and my code probably shows it.

Now I wanted to make a Caesar Cipher but instead of having all the leters shifted by the same number, I'm having them shifted by the digits of pi. So the first letter has a shift of 3, the second a shift of 1, the third a shift of 4, so on and so on. Everything works great except my input cant be more than 8 characters otherwise it will give me an error when i run it. I've searced the web and asked friends but they aren't positive of the reason, so I've come here for help. Here is my code
Using Visual Studios 2013 by the way.
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
//Pi Encoder 
//Kerchnasty
//July 2014

#include <iostream>
#include <cstring>
#include <string>
#include <limits>

using namespace std;

char Encryption(char);
string  Pi = "31415926535897932384626433832795028841971693993751";
int i = 0;

int main()

{

    string Unencrypted;       //this will be our input

    do
    {

        string Encrypted = "";      //this is the output

        cout << "Enter the Message : \n";

        getline(cin, Unencrypted);

        for (int x = 0; x < Unencrypted.length(); x++)    
        {
            Encrypted += Encryption(Unencrypted[x]);
        }

        cout << "\nYour encrypted message is : \n";

        cout << Encrypted << endl;

        std::cout << "Press ENTER to continue...";

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      }
    while (!Unencrypted.length() == 0);
    return 0;
}


char Encryption(char Encrypted)

{
    
    if (isalpha(Encrypted))    //to leave spaces and punctuation alone
        {
            Encrypted = toupper(Encrypted); //i'm just focusing on all caps
            Encrypted = (((Encrypted - 65) + (int(Pi[i]) - 48)) % 26) + 65;
            i++;
        }
    return Encrypted;

}


Now in my actual code I have 1 million digits of pi to ensure that almost any length message could be encrypted however I cut that down to 50 for the sake of this post. I was wondering if my error was due to me returning Encrypted as a char instead of string thus causing my input to be limited, but if i changed Encrypted to a string then there were too many conversion bugs. If that really is the reason I'll just have to start from scratch and
Again any help is accepted whether it be for my code or my forum posting format!
Thanks everyone!
Last edited on
Can't reproduce. Post your actual code or an input that causes the behavior on the code you posted.

Now in my actual code I have 1 million digits of pi to ensure that almost any length message could be encrypted
Well, most implementations support std::strings of over 4 billion characters, so you're a quarter of a thousandth there.
Tested the code with Visual Studio 2013 Express, the program did not crash.

A minor issue: there was a warning for line 31, signed-unsigned comparison.
Do not use int for sizes and lengths. C++ inherits size_t from good old C.
int is signed and size_t is unsigned.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstddef>

// ...

std::size_t i=0;

// ...

        for (std::size_t x = 0; x < Unencrypted.length(); x++)    
        {
            Encrypted += Encryption(Unencrypted[x]);
        }


One thing I asked myself while testing your program was: how do you decrypt the encrypted messages?

I also noticed that you use globals, and you should know how they say "globals are evil". Your program shows why. Did you notice that if you give the same input in one run, you get different outputs? This is because the global counter i is not reset.

Still, you should not use a global counter, even if you fix the bug by resetting it in the do-while() loop. To me personally, a global counter doesn't feel elegant.

You are also using namespace std; and you should unlearn doing so.
http://www.parashift.com/c++-faq/using-namespace-std.html
Last edited on
helios
When i would input AAAA it gives me DBEBF, but if i put in AAAAAAAAA then it will give me an error. Could it possibly be a buffer overflow problem? I can't put my entire code here because there is a max length for the forum posts. If you can't reproduce then maybe the problem is the fact that i have too many digits for Pi? since that is the only difference between our codes... I should mention I'm using Visual Studios 2013 as my compiler.
Last edited on
Catfish5
Thanks a lot for your input! Decrypting my message will be task number two, as i said I just got back into C++ so I'm taking baby steps.
A friend of mine did notice that same thing with the global coutner i so i just put it here
1
2
3
4
5
cout << Encrypted << endl;

        int i = 0;

        std::cout << "Press ENTER to continue...";

.
I'll work on dropping the using namespace std;, just an ugly habbit i picked up from begginers C++ classes.
Thanks again!
Last edited on
Still can't reproduce. Upload your code to pastebin.
Currently at work where pastebin is blocked, i'll upload it when i get home. In the meantime here is as much as i can fit.
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
#include <iostream>
#include <cstring>
#include <string>
#include <limits>

using namespace std;

char Encryption(char);
string  Pi = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275900994657640789512694683983525957098258226205224894077267194782684826014769909026401363944374553050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382686838689427741559918559252459539594310499725246808459872736446958486538367362226260991246080512438843904512441365497627807977156914359977001296160894416948685558484063534220722258284886481584560285060168427394522674676788952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288797108931456691368672287489405601015033086179286809208747609178249385890097149096759852613655497818931297848216829989487226588048575640142704775551323796414515237462343645428584447952658678210511413547357395231134271661021359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435064302184531910484810053706146806749192781911979399520614196634287544406437451237181921799983910159195618146751426912397489409071864942319615679452080951465502252316038819301420937621378559566389377870830390697920773467221825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539057962685610055081066587969981635747363840525714591028970641401109712062804390397595156771577004203378699360072305587631763594218731251471205329281918261861258673215791984148488291644706095752706957220917567116722910981690915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398315019701651511685171437657618351556508849099898599823873455283316355076479185358932261854896321329330898570642046752590709154814165498594616371802709819943099244889575712828905923233260972997120844335732654893823911932597463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100449293215160842444859637669838952286847831235526582131449576857262433441893039686426243410773226978028073189154411010446823252716201052652272111660396665573092547110557853763466820653109896526918620564769312570586356620185581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318586769751456614068007002378776591344017127494704205622305389945613140711270004078547332699390814546646458807972708266830634328587856983052358089330657574067954571637752542021149557615814002501262285941302164715509792592309907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111790429782856475032031986915140287080859904801094121472213179476477726224142548545403321571853061422881375850430633217518297986622371721591607716692547487389866549494501146540628433663937900397692656721463853067360965712091807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862947265473642523081770367515906735023507283540567040386743513622224771589150495309844489333096340878076932599397805419341447377441842631298608099888687413260472156951623965864573021631598193195167353812974167729478672422924654366800980676928238280689964004824354037014163149658979409243237896907069779422362508221688957383798623001593776471651228935786015881617557829735233446042815126272037343146531977774160319906655418763979293344195215413418994854447345673831624993419131814809277771038638773431772075456545322077709212019051660962804909263601975988281613323166636528619326686336062735676303544776280350450777235547105859548702790814356240145171806246436267945612753181340783303362542327839449753824372058353114771199260638133467768796959703098339130771098704085913374641442822772634659470474587847787201927715280731767907707157213444730605700733492436931138350493163128404251219256517980694113528013147013047816437885185290928545201165839341965621349143415956258658655705526904965209858033850722426482939728584783163057777560688876446248246857926039535277348030480290058760758251047470916439613626760449256274204208320856611906254543372131535958450687724602901618766795240616342522577195429162991930645537799140373404328752628889639958794757291746426357455254079091451357111369410911939325191076020825202618798531887705842972591677813149699009019211697173727847684726860849003377024242916513005005168323364350389517029893922334517220138128069650117844087451960121228599371623130171144484640903890644954440061986907548516026327505298349187407866808818338510228334508504860825039302133219715518430635455007668282949304137765527939751754613953984683393638304746119966538581538420568533862186725233402830871123282789212507712629463229563989898935821167456270102183564622013496715188190973038119800497340723961";
int i = 0;

int main()

{

    string Unencrypted;

    do
    {

        string Encrypted = "";

        cout << "Enter the Message : \n";

        getline(cin, Unencrypted);

        for (int x = 0; x < Unencrypted.length(); x++)
        {
            Encrypted += Encryption(Unencrypted[x]);
        }

        cout << "\nYour encrypted message is : \n";

        cout << Encrypted << endl;

        std::cout << "Press ENTER to continue...";

        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    while (!Unencrypted.length() == 0);
    return 0;
}


char Encryption(char Encrypted)

{
    
    if (isalpha(Encrypted))
        {
            Encrypted = toupper(Encrypted); 
            Encrypted = (((Encrypted - 65) + (int(Pi[i]) - 48)) % 26) + 65;
            i++;
        }
    return Encrypted;

}
If everything still runs correctly even with this new code then Im not sure what the problem is. I'll have to test each portion when i get home from work this evening and see if it works for me.
As for my previous question, could the problem be that I'm returning Encrypted as a char instead of a string? Or shouldn't that matter...
Returning a char is fine, since std::string has an overload for += that accepts a char.

i is still global. I still don't see you resetting i within your do/while loop.

Burying the increment of i inside Encryption is a bad idea.
1) Make i local within main.
2) Reset i within do/while loop.
3) Pass i to Encryption as an argument.
Thanks for the response AbstractionAnon.
I should have mentioned this above,
In my new code I have
1
2
string Unencrypted;
int i = 0;

and
1
2
3
4
5
cout << Encrypted << endl;

       i = 0;

        std::cout << "Press ENTER to continue...";

and
char Encryption(char Encrypted, int i)
in order to fix the global i issue.

I didnt change it in the code above because I didnt want to mess with the code before I've gotten home to test it myself. Just incase i made some stupid error that causes a bug. I figured the code above will work fine and the global i isn't causing the error so no need to change it.
Last edited on
Topic archived. No new replies allowed.