3

The immediate problem lies here: if ( isalnum(k [1]) || argc == 2) The isalnum() function requires a single char to be passed to it. k[1] is a string. Pass a string to isalnum or any of it's cousins and you get a seg fault. The next issue is in the same line of code. ALWAYS check the value of argc FIRST. If argc is too small, it means that there aren't ...


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

To answer your first question, check50 is not available in the Lab. (check50 is not introduced until Week 3 when you will move to the IDE). edit To answer your second question, you are only seeing a score for Scratch because that was submitted in 2018. Your other programs were submitted in 2019, but you used the 2018/x submit50 code. You'll need to ...


1

In that formula, use p[i], the ith character of p, not p itself.


1

When the key is sufficiently large enough, the algorithm that was designed here won't work. Imagine if the key were 1000. The results generated will not even be a valid ASCII code. A key of 65 is large enough to expose this problem but small enough to still generate letters as a result. A rethink is in order. When adding the key, it's necessary to wrap ...


1

I didn't test all of your code, but you're close. Look a few lines later. This will definitely cause a seg fault: if (isalpha(p)) The isalpha() function takes a single char. p is a string. This statement tries to stuff a string down isalpha's throat, so it chokes on it and throws up a seg fault. (Nice analogy, huh? ;-) ) You can pass it a single char,...


1

Quoting the problem specification: You can assume that, if a user does provide a command-line argument, it will be a non-negative integer (e.g., 1). No need to check that it’s indeed numeric. Same source: If your program is executed without any command-line arguments or with more than one command-line argument, your program should print an ...


1

The main problem lies in your if/if/else structure. The first if statement will execute, independently of what follows. Then, the second if statement will be executed. If it's true, then the else code will be skipped, but if it's false, the else clause will execute. So, for example, if the plaintext letter is a lower case letter, the first if clause will ...


1

First to the error: i<len,k>=0 uses the comma operator, which evaluates its left side, discards the result, and evaluates to its right side. So i<len,k>=0 is the same as k>=0, the value of i<len is ignored. You're doing Caesar's cipher, right? This one looks like a strange crossover of caesar and vigenère. In Caesar's cipher, you shift ...


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

Your spelling of caesar is wrong - you have it as caeser. You need to change both the file name and the folder name to "caes[a]r" The command you are using is most like an api endpoint and expects a certain parameter and/or path specified.


1

Does the code check to see that there is exactly one input parameter string BEFORE trying to do anything with argv[1]? Doesn't look like it. Also, you might benefit from reading this: Do YOU know how to find a seg fault?? Advice to new programmers


1

One of the things you'll learn in programming is that there are usually many "right" ways to do things. Later, you'll also see a lesson on programming efficiency. The question shifts from what's right to what's more efficient. And then, it gets even more interesting because you have to define what's more important, efficient programming, ...


1

The video on the assignment gave hints towards using that approach. I can't tell you it's "right" or "the best", but it makes perfect sense.


1

The code runs because it isn't validating that all of the chars in argv[1] are digits. The test in the code, isalpha(*argv[1]) actually takes the address of argv[1] as the location of a single char and checks it, but not the chars that follow. You have to understand that isalpha() and all of its cousins only check one single char, not an entire string. If it ...


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

First of all, the code MUST check for lack of a key, i.e., a valid number of parameters by checking the value of argc FIRST!!! Right now, the code is trying to use argv[1] before checking the value of argc, meaning that it's trying to use an array element that might not even exist! That should solve some of your issues. Next, there's no code at all to ...


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

Try using '\0' in place of (char) 0 for null terminating character of cipher. If this helps, click the tick mark.


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

Didn't the spec say that the code should test for non-numeric keys and should print an error message when a non-numeric is found? Perhaps you need to validate the key as all numeric, and then use atoi()


1

First of all, this is why it's important to get the indentation aligned correctly so you can see what goes with what. Here's the corrected indentation. int main(int argc, string argv[]) { if (argc != 2) { printf("Usage: ./caesar key\n"); } else if (argc == 2) { for (int i = 0; i < strlen(argv[1]); i++) { ...


1

It's pretty simple it means your program is handling non-numeric key. Try dis with your program ./caesar abaed You're program doesn't treat this as an error because the keys are supposed to be only numbers, no other characters but numbers only.


1

Let's look at what's really happening: char n = argv[1][i]; int digit = isdigit(n); if (n != digit) You have to thoroughly understand every step here. First, the code will copy the specified char from argv[1] to n. Second, the code will check whether the char stored in n is a digit. It will then store the return value from isdigit() in ...


1

The ceasar function (defined here: int ceasar (char Plaintext[], int key) does not have any return statement. Since this is a function "with side effects", ie it is printing the ouptut, then it could be defined void ceasar (char Plaintext[], int key). And don't forget the prototype definition.


1

cipher_digits is of type char, more precisely 8-bit signed char, meaning it can store values from -128 to 127. What will happen if the sum is greater than 127? It will wrap around, interpreting the least significant byte of the result as char again, this time with a negative value (it's like 125, 126, 127, -128, -127). A simple solution would be to make ...


1

Look carefully at the following two statements: void cyrpt(string s, int k); ..... crypt(s,k);


1

The code is trying to do too many things at once. It is trying to validate the key and encode the plaintext in the same loop. Can't do that. You need to break the tasks down into separate, independent tasks. First task is to validate the key. If it isn't a valid key, then the program should exit. The code is looking for alphas to validate the key. ...


1

The goal is to encode each alpha in text. So why is the for loop based on the length of the key, strlen(argv[i])? for (int i = 0, n = strlen(argv[i]); i < n; i++) If this answere your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


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