0

I am just getting lost and overloaded. Been looking at this for 2 days. I think something is wrong, since my text does not get encrypted after I enter it and then hit enter. I get a weird print out in the command line of a little bits box. Anyone know what I'm doing wrong?

My guess is storing the encrypted code into the int cipher[] is not right, but I feel like I have to store it into something, so I can do math on it in my loop, as well as print it out in my final print statement. Please point out anything you see that is wrong, even if there are several mistakes! Thanks, any help is so greatly appreciated. :(

screenshot of bad caesar output in Terminal

So I changed my code, but now I'm getting a Segmentation fault (core dumped) error when I enter some text into the command line (my plain text) and press enter.

  • Please don't share your entire solution in public. Even when the solution is incomplete or non-working, it's considered "not reasonable" according to the academic honesty policy outlined at the beginning of every pset specification. These "code dumps" will be removed. – Air Aug 11 '14 at 16:07
  • I didn't share the answer in public, I only shared non-working code. – Azurespot Aug 12 '14 at 3:20
  • 1
    A problem set is not just one question with one answer. Even code that doesn't produce the desired output or pass check50 may contain many "answers" in its different parts. You're expected to make your best effort to isolate a specific, brief part of the code -- a "snippet" -- that illustrates your question. Or as an alternative, you can put together a Minimum, Complete, and Verifiable Example (MCVE). Not only does this help us solve your problem quickly and efficiently, you'll find it often helps you solve problems on your own, which is priceless. – Air Aug 12 '14 at 15:14
  • Glenn, one of the staff members for CS50, gave a great explanation of the policy on the subreddit some time ago. – Air Aug 12 '14 at 15:16
  • 1
    Okay, I thought it was a snippet. It wasn't the whole thing. But I will be aware in the future. – Azurespot Aug 13 '14 at 4:12
1

I took your code and examined it and I suggest the following:

a) Completely remove the isblank() function from the if statement. It is not needed.

b) Put all of calculations inside of the respective if statement. No need to keep the addition of the key and others outside.

c) Remove the printf("Re-enter an alphabetic word only: %s\n", plain_text); completely. The program is just supposed to ignore numeric inputs not alert the user.

d) I recommend you use 'a' and 'A' instead of 97 & 65. e.g. plain_text[i] - 'A';

e) Put printf("%c\n", cipher[i]); outside the for loop and change it to
printf("%s\n", plaintext); This will just print the entire string, no need for a loop.

f) Lastly I challenge you to trace your calculations one more time. Use a simple example such as baz and on paper do the calculation using the ascii table & what your code currently has and you should see why it prints a big space.

| improve this answer | |
  • Thanks Christopher! I can see how those changes would help. I am now trying to figure out the blank space, but I had a question, when we declare a variable at the top of our code, this variable can change as it goes down the code, correct? Because I was thinking, maybe because I declared plain_text as equal to GetString(), maybe it will always be a blank that prints. Although it is not waiting for user input, so not sure if that theory is correct. It just prints a blank then quits the program. Which brings me to a 2nd question: is plain_text the same as plain_text[]? I assume yes. – Azurespot Aug 11 '14 at 6:31
  • As you mentioned in the FB group, my code has wrong ASCII math. So it was not calculating from ASCII to alphabet back to ASCII, but something else entirely. So once I fixed that, it stopped printing zero, and worked! Thanks again! – Azurespot Aug 11 '14 at 8:03
1

The int cipher []as you stated is mainly what is incorrect. In caesar cipher you are encrypting words and phrases. The are declared using character array referred to as 'strings'. So declaring it as: string cipher; will be just fine. Remember that under the hood all characters and numbers are represented as bits & bytes so manipulating it with math will affect the its Ascii representation and map it to a next character.

PS: Your code need to make some changes.

  1. Think about how you have to adjust it to loop around back to the beginning of the alphabet.
  2. Consider how to ignore spaces and numbers. You are only required to encrypt text.
| improve this answer | |
  • See link for reference – Christopher Clarke Aug 11 '14 at 3:18
  • Thanks Christopher, but I get errors in the command line when I change cipher to a string. It is seeing plain_text[] as an int for some reason, even though it's originally declared as a string. – Azurespot Aug 11 '14 at 4:17
  • Your current code is missing something fundamental when manipulating the bits of the characters entered under the hood. Try tracing through your current code using a simple example and calculating the changes to the bits yourself using the ascii table as a guide – Christopher Clarke Aug 11 '14 at 5:01
  • segmentation fault in layman terms occurs when you try to trouble a place in memory where you are not allowed to touch. You are trying to manipulate the string cipher before initialising it. For simplicity you could set cipher to be equal to plain_text or you could get rid of the entire cipher variable and manipulate plain_text instead. e.g string cipher = plain_text; – Christopher Clarke Aug 11 '14 at 5:33

You must log in to answer this question.

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