Caesar Cipher [Need a hand]

Hi all,

I started programming in C++ this year with the "Introduction to Programming" module I took. In it, we covered the basics of loops, arrays and functions. I want to learn more, and start writing my own simple programs though.

I decided to try and make a Caesar Cipher program, which reads in a string, reads the amount to shift the characters by, and prints the ciphertext.

So far, it only encrypts (I want to make sure everything is working first). Now, I'm a little stuck on where to go next.

Originally, I had the entire program in the main function. I tried to create a function for encryption and I know I've made a mistake, I just can't see where.

Here's my code so far:

REMOVED

Can someone help me out a little? It'd be greatly appreciated.

Thanks in advance.

-Matt
Last edited on
You never assign a value to cipher but you seem to expect it to contain your encrypted data for some reason. The variable "plain" should contain your encrypted data since you passed it by reference.
Sorry, my fault, I edited the post when I realised a stupid error, and forgot to change that.
Thank you.

I'm currently working on the code, trying to implement a switch menu for encrypting and decrypting.
It's a little messy, but I'll post it when I finish up.

You still didn't fix it, on Line 24 you are outputting "cipher" which is a string that you never assign any kind of a value to.
Ok, I've added an option to decrypt now too. I'm having trouble with the switch menu now though.

Whenever I chose 1 or 2. It prompts for the shift amount straight away, and doesn't give me a chance to enter a string.

Here's the updated code:

REMOVED
Last edited on
You are mixing unformatted (getline) and formatted (>>) input methods. Either use only one or only the other.
Thanks for your reply. I was wondering what was causing the error. After some searching, I appended cin.ignore() after the choice in the menu function. That seems to have done the trick too.

I thought I was in the clear after that problem, however, my decrypt function isn't working properly at all.

REMOVED

When I use a simple shift, such as 4, my output becomes weird symbols.

e.g. If I input "e" as the ciphertext, with a shift of 4, I expect an "a". This isn't the case. My algorithm is all wrong, but I've been tinkering for the past hour to no avail.

Can anyone shed some light?

-Matt
Last edited on
Topic archived. No new replies allowed.