Hot answers tagged

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

You have to understand how printf and strlen work. Both of them (and several other functions) depend on the existence of the end of string marker, \0 at the end of the target string. If the EOS marker isn't there, they will keep reading data from memory until they find some random data byte that looks like the EOS marker, or 0x00. Next, you need to ...


1

Simply put, the logic of encoding the letters is wrong. The logic of the current code is to take the letter from the input string, find the same matching letter in an array (ie, replace q with q, which does nothing), add the key to that ASCII value and apply %26. Finally, applying %26 directly to an ASCII value doesn't produce a valid result. The code needs ...


1

I have tested your code, including the new code you've added in your edit, and your conditions work when the arithmetic is removed. This means changing capAlph[p] == text[i] - 'A' to capAlph[p] == text[i] and smaAlph[p] == text[i] - 'a' to smaAlph[p] == text[i]. The arithmetic, as I explained in a comment, ensures that the two values will never be equal. I ...


1

You have identified the variables correctly, but used your brackets and keywords wrongly. Inside the for loop, you open a if statement bracket but don't close. In your control_caesar_key function, you wanted to use a for loop and wrote down the condition properly, but you replace the keyword for with the keyword if. Along with that, you will be required to ...


1

The function atoi returns an integer. You're assigning it to a variable of type char *. Change it to int key to fix.


1

You have a logic error in your declaration of ciphertext. Take a look at how it is declared (it's type) and the type of plaintext. ;) This should hopefully make it clear why one seems to work with multiple letters and the other only one. If this helps, feel free to click the check mark. But if not, let me know and I will help further.


1

Program doesn't know if all the characters in key are digits until after it has processed all of them. The "Success" message should come after the for loop is finished. And remember this hint from the spec: Recall that we can return nonzero values from main to indicate that our program did not finish successfully. As soon as a non-digit is found, ...


1

In other words, the isdigit() function (and all of it's issomething() cousins) takes a single char as input. If you pass it a string, it chokes and generates a seg fault. Otherwise, @MARS gave a great explanation. ;-)


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]&...


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