0

I'm working on pset2, Caesar's cipher part. I understand how to proceed to do the encryption and my program compile well. The first line of command allow the user to correctly enter his key. After the first return, user can write some plaintext to encrypt, and then, there appear the problem. My program get block to the "GetString()" command line and ask continually to enter strings...

May someone can help me to solve this problem?!

Thanks for your answer and your time.

[code hidden behind edit flag]

2 Answers 2

1

In testing your code, it appears to ask for the string correctly. However, if there is a non-alpha in the string, it will go into an infinite loop in the while loop because the only time you increment i is inside one of the if statements. In fact, this leads to another problem because you can potentially increment i twice inside of a single pass through the loop.

There are a couple of different ways to improve on this. One way would be to increment i once at the end of the loop instead of all over the place inside if statements. Another alternative would be to simply use a FOR loop instead of a while loop, since you already know how many characters to process (strlen() ).

Next, your formulas for encoding are flawed. Let's look at one:

cipher[i] = ((int)plaintext[i] + key) % 26 + 65;

Now, let's use a key of 0 and a letter of a.

cipher[i] = ( (int)'a' + 0 ) % 26 + 65
          = ( 65 + 0 ) % 26 + 65
          = (65 % 26)  + 65       (parentheses indicate operation priority)
          = ( 13 ) + 65
          = 78
          = 'n'

As you can see, this doesn't work. It should have resulted in 'a'. The secret to this formula is that % only works on alphas represented by numbers from 0 to 25 inclusive. You tried to apply it to the ASCII value of the letter.

Next, why do you have else return 1? Anything other than a lower case alpha would immediately trigger this return and exit the program!

Finally, you are not handling non-alphas (or at least not correctly). I think you thought about it, but you made an error. I'll leave this one for you to discover. ;-)

Fix these and it should get you close to success. There may be other issues, but this will get you going.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

2
  • Great thanks for formatting and answer, I'll keep working with these new clues.
    – CharlesD89
    Commented Sep 27, 2015 at 22:42
  • Speaking of formatting, you should spend some quality time with style50 and improve your code layout skills. I was able to take your code from 95 lines to about 45 lines just by removing the excess blank lines. After that, reformatting to align curly braces to make it easier to read was the next issue. Running your code through style50 will teach you how to cleanly format your code. While it may be tedious and seem like a pain in the beginning, once you get good at it, it'll be second nature.
    – Cliff B
    Commented Sep 27, 2015 at 22:57
0

Here my final program for those who are interested. This is certainly not the finest way to write it but I'll stand this for now. Thanks for help.

// program encrypts messages using Caesar’s cipher

[code removed]

1
  • please do not post solution code publicly.
    – curiouskiwi
    Commented Sep 29, 2015 at 0:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .