Hot answers tagged

2

Go through your conditionals if p[i] is a capital letter. if islower(p[i]) is a plain "if" statement, so you'll go into the "else" block if the character isn't a lower case letter. It looks like you want that to be an "else if" block, instead. Debugging tip: since you have the expected output and the actual output, look and see if you can find patterns in ...


2

This issue is with this: isdigit(argv[1]) The isdigit() function (and related functions in the same family) takes a single char as input. The problem here is that argv[1] is a string, not a char. By trying to shove an entire string down isdigit's throat, it chokes and coughs up a seg fault. You would need to check each char in the string individually. Hint:...


2

I ran your code and got something different than what you posted. After further checking, I found that the results are actually unpredictable. The code may or may not print extra characters after processing the plaintext, depending on what was left in memory from prior activity. Here's the problem. The code encodes each char and stores it in ciphertext[]. ...


2

You can't do what you're trying to do here, at least not like this. Unlike other languages, strings (aka, char arrays) are immutable in C. That means that once they're created, their length cannot be changed. This code is attempting to lengthen a string. When it tries to access memory beyond the end of the string, it's triggering a seg fault. The code ...


2

You're getting a seg fault because the code tries to work with argv[1] before checking whether it exists. "Ready, Fire, Aim!" The very first thing that must be done is to check the value of argc. If it is NOT 2, the program should terminate immediately. Period. It shouldn't be built on an if/else structure and shouldn't encapsulate any other code. ...


2

So, it's clear that the program compiles without errors (that big message is just an echo of the specific compiler commands and all the flags that are set.) It's also clear that it executes and completes execution. The problem is that it is not encoding the plain text. Your code has two significantly serious problems. The first is that the code calls a ...


2

This is a subtle logic error. The problem is that the code is processing one too many characters. The last character is unprintable, but check50 sees it. That's why the output looks correct but isn't. Take a close look at this: for (int i=0; i <= strlen(plaintext) ; i++) What happens when i == strlen(plaintext)? What char in the plaintext string ...


2

You are missing a { immediately after your main declaration and before your for loop. This means the computer sees the declaration and then a bunch of code, but doesn't understand the code belongs to the function because it isn't enclosed in braces.


1

Ooh, a very subtle error! Look at the following: for (i=0; i <= keylength; i++) Say that the key is 48. Then keylength is 2. Now, if i starts at 0, what's the last value of i that will run a pass through the loop? It's 2, right? Now, what is the char at key[1][2]???? Programming note. Best practice is to use the reserved variable name of argv, not ...


1

The error is that instead of printing the word 'ciphertext', it prints 'chipertext'. Please correct that. Hope this helps. If it does then please check the tickmark


1

for(int i = 0, len = strlen(plaintext); i < len, i++) The standard structure of a for loop is: for (initialization statement; test expression; update statement) You have a comma instead of a semicolon between your test expression and your update statement, meaning the compiler thinks your test expression or conditional is "i < len, i++" (...


1

The problem lies here: int asciiValue = plaintext[i]+key%26; Simply put, it won't work at all. plaintext[i] contains an ASCII value for a letter. That's a number somewhere between 65 and 122 inclusive. Add a key to it and it's even larger. Now, what happens when you apply %26 to it? You ALWAYS get a number between 0 and 25 inclusive. In other words, you ...


1

There are a lot of questions and problems here. Normally, it's one question to handle one or a few related problems. The messages like "handles lack of key" are describing the specific test being run. In this case, "Does the program handle the lack of a key, ie, a missing parameter when executing the program?" Another checks for ...


1

In your is_numerical function, the compiler sees that there is a theoretical path where no return statement would be encountered, so it generates that error. Specifically, if the for loop were to execute (say n=0), then the code would never encounter a return statement while executing. There would need to be a return statement following the loop to fix this....


1

It fails because the test for non-numeric key is bad. It won't catch a key where the first char is a number and any other char is a non-numeric. Look at this code: if (argc == 2 && isdigit(*argv[1])) This will ONLY look at the first char in argv[1]. The isdigit()function only looks at single chars, not at entire strings. *argv[1] is the ...


1

The single quotes around something say that "this is a literal character". Without the single quotes, or double quotes (indicating a string), it's a variable name. One thing you need to learn is this. A char is also treated by C as a single byte signed integer. It is not necessary to cast a char as an int. It will be treated as an int ...


1

There is no need for itoa function. If you make a separate function for doing the cypher which returns a integer and assigns it to a char variable then the integer is converted to its string equivalent. char c; c = cypher(text, key); int cypher(char text, char key) { int result; result = (65 + (key + atoi(text)%26); return result; } The function ...


1

The isdigit() function can only take a single character as input, but argv[1] is a string, complete with end of string marker. If you pass a string to isdigit() or any of it's cousins like isalpha, etc., it will always generate a seg fault. If you want to check a string to see if it's really all digits, you need to use a for loop in combination with a ...


1

You can't reprompt a user for command line arguments. You will have to exit the program after printing the usage.


1

Perhaps a review of the class material on command line arguments is in order? https://cs50.harvard.edu/x/2020/notes/2/#command-line-arguments


1

Two problems. First is the for loop setup. "argv[ ]" cannot be used like this. It requires an index inside the square brackets to select one of the array elements (one of the parameters given when the program is called.) Since you're trying to check each char in the first parameter given when the program is executed, it should be "argv[1]&...


1

I’m assuming by last else you mean the one that returns 1. At a glance it looks like that else does not belong to an if statement. Is this all right of your code?


1

The 'for' loop used to validate the key, should finish before ask the user for the input string. Then you start the second loop to process the input and return the encoded characters.


1

A segmentation fault occurs when you try to access memory that "doesn't belong to you". In your case, in the following for loop: for (int i = 0; i <= strlength; i++) // for loop for encoding with key because you set the loop to run until i = strlength, you'll end up accessing ciphertext[strlength], but that value does not exist. To elaborate with an ...


1

You can't see it in your output, but you're actually printing one character beyond the end of the string, which is the null terminating character. printf("ciphertext: "); for (j = 0; j <= text_length; j++) In the loop statement here, you're going through j = text_length, but text[text_length] is the null terminating character. :( handles non-...


1

Tip: If you open the link in the check50 results, it will show you the test data used and the result. In this case, it used 2x as the input parameter to caesar, and timed out. When it times out, it usually means one of two things: One, the code is waiting for more input but check50 doesn't expect to provide input there, so it times out waiting. Your code is ...


1

if (isalpha (key)) In this line, isalpha is written to take an int, but key is a string. There's no automatic conversion from string to int, the way there is from char to int. I think you intend to write isalpha(key[i]) instead. Also, it's not an error, but you have some variables that are outside of the scope where they're used. An example is: int i = ...


1

They are right, your program is only checking the first character of argv[1]. Try to think - your for loop checks the first character; if its not a digit, it throws an error and ends the program. Or if its a digit, it prompts for input. So basically you are stopping the program either way just after checking the first character! What you should do instead, ...


1

Current operating systems allocate a portion of memory to each application. If an application tries to directly access a memory location that does not belong to it or an incorrect memory location or a protected memory area, the operating system will stop the application and generate an error (under Linux: Segmentation error). The use of isdigit is not ...


1

It's a simple issue, an oversight. After printing the error messages, the program needs to end via a return statement. But there are no return statements where needed. Also, the code needs '\n' at the end of each message, which will cause a different check50 failure. If this answers your question, please click on the check mark to accept. Let's keep up on ...


Only top voted, non community-wiki answers of a minimum length are eligible