C++ simple caesar cipher

Borland Delphi 2009 professional & c++bu...
March 8, 2010 at 12:16:48
Specs: Windows 7
I'm trying to create a simple caesar cipher. I am currently trying to learn c++ so I am not very advanced.

The code does compile but it does not encrypt the word/s entered in by the user. I was wondering if someone could help me figure out where I went wrong. Thanks.


#include <vcl.h>
#include <iostream>
#include <string>
#pragma hdrstop

#include <tchar.h>
using namespace std;

#pragma argsused
int main ()

string nonCryptWord;
int i;
int crypted;
int offset;

cout << "what do you want to encrypt? ";
cin >> nonCryptWord;
cout << "\nWhat do you want the offset to be? ";
cin >> offset;

for (i = 0; i != nonCryptWord.length(); ++i)

if (nonCryptWord[i] + offset > 122)
crypted = ((nonCryptWord[i] + offset) -122) + 9;
cout << "\n" << char(crypted);


return 0;

See More: C++ simple caesar cipher

Report •

March 8, 2010 at 18:03:48
First glance, many things are wrong:
1) You don't actually do anything unless the letter + the offset is greater than 'z'.
2) You're actually using numbers instead of characters.
3) You're assuming you'll only receive lower case text.
4) If the ciphered value IS greater than 'z', you subtract 'z', which isn't what you want.
5) You're adding 9, at the end of your ROT cipher, for whatever reason. (Note, this might give you a result greater than 'z')

Report •

March 8, 2010 at 18:57:10
Thanks for the reply Razor 2.3
Looks like I needed an else statement and I needed to fix my math. It is working now. Thanks again

Report •
Related Solutions

Ask Question