Evan Mosseri Evan Mosseri - 4 months ago 23
C Question

Whats wrong with my simple encryption in C? (cs50 pset 2 - crypt)

I don't quite understand where these errors are coming from. I am trying to create a simple C program that takes a string and adds an offset to the ASCII value in order to create an extremely simple encryption.

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>

char obscufate_char(char origchar, int offset){
if(strcmp(origchar, " ") != 0){
int temp = origchar;
char newChar = temp + 4;
return newChar;
}
else{
return 20;
}
}

int main(int argc, string argv[]){

if(argv[1] != NULL){
string message = argv[1];
}else{
printf("%s\n", "Enter a string to encrypt\n");
string message = GetString();
}

if(argv[2] != NULL){
int offset = atoi(argv[2]);
}else{
printf("%s\n", "Enter a offset\n");
int offset = GetInt();
}

printf("%s%s\n", "Your original text is: ", message);
printf("%s\n", "Your new message is: ");
for(int i = 0; i < strlen(message); i++){
printf("%c\n", obscufate_char(message[i]),offset);
}

return 0;
}


simple_crypt.c:7:12: error: incompatible integer to pointer conversion passing
'char' to parameter of type 'const char *'; take the address with &
[-Werror]
if(strcmp(origchar, " ") != 0){
^~~~~~~~
&
/usr/include/string.h:143:34: note: passing argument to parameter '__s1' here
extern int strcmp (__const char *__s1, __const char *__s2)
^
simple_crypt.c:20:10: error: unused variable 'message'
[-Werror,-Wunused-variable]
string message = argv[1];
^
simple_crypt.c:23:10: error: unused variable 'message'
[-Werror,-Wunused-variable]
string message = GetString();
^
simple_crypt.c:27:7: error: unused variable 'offset' [-Werror,-Wunused-variable]
int offset = atoi(argv[2]);
^
simple_crypt.c:30:7: error: unused variable 'offset' [-Werror,-Wunused-variable]
int offset = GetInt();
^
simple_crypt.c:33:46: error: use of undeclared identifier 'message'
printf("%s%s\n", "Your original text is: ", message);
^
simple_crypt.c:35:28: error: use of undeclared identifier 'message'
for(int i = 0; i < strlen(message); i++){
^
simple_crypt.c:36:33: error: use of undeclared identifier 'message'
printf("%c\n", obscufate_char(message[i]),offset);

Answer
simple_crypt.c:7:12: error: incompatible integer to pointer conversion passing
      'char' to parameter of type 'const char *'; take the address with &
      [-Werror]
        if(strcmp(origchar, " ") != 0){

You are passing a character when the function expects a char *. origchar is not a char *.

simple_crypt.c:20:10: error: unused variable 'message'
      [-Werror,-Wunused-variable]
                string message = argv[1];

I'm assuming that cs50.h has a 'string' type for you with this one. You are making an initialization within an if statement and you expect it to be seen outside of the if statement's scope, which is not going to happen. The message outside of your if statement is not the same as the message inside. The message inside is just declared and initialized but nothing is done with it, essentially making it unused for its scope. To the compiler the message outside the if statement is an undeclared variable.

You should declare message outside of the if statements first so the compiler knows that message is and will be created within the scope of main or at least is to be seen outside of the scope of the if statements. You can assign values to it in the if statements, but you shouldn't declare it within an if statement if you expect it to be seen outside of the if statement. The if statement has its own scope and therefore after it is done the message you initialized will be gone without doing any operations on it.

same for offset and the rest of your message errors.

Comments