0

I can't figure out whats wrong.

Here's my code:

//argc and argv allow to for users to input data before the program runs.

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


   //Do-while loop to ensure user enters a string
   do {
   printf("Enter the message you wish to encrypt\n");
   string s = get_string(); 

   if (s != NULL) {

      //converting string from position 1 of argv to an integer value for the key
      int key = atoi(argv[1]); 


      //this loop iterates through the string and shifts each char by the key
      for (int i = 0, n = strlen(s); i < n; i++) {


         string c = (s[i] + key);

      }

      printf("Plaintext: %s\n", s);
      printf("Ciphertext: %c\n", c);
      break;
   }



} while ( argc == 2);

}

And heres the error message:

argv0.c:25:17: error: incompatible integer to pointer conversion
initializing 'string' (aka 'char *') with an expression of type 'int' 
[-Werror,-Wint-conversion]
         string c = (s[i] + key) 

Could I get a little push towards the right direction?

1
  • You are setting the string c to an integer value, which isn't allowed in C. What if you changed it to int c = (s[i] + key)? Also, question: why do you put the do-while loop around ALL your code? If the user puts an invalid amount of command arguments (i.e, argc == 1), the entire program would run before it'd re-prompt the user for input. What if you wrapped the do-while loop around the prompt for input, and then wrapped the rest of your program in an if/else statement that checked for the right amount of command arguments? This is just one way to go about it. Apr 20, 2017 at 12:22

1 Answer 1

0

This is because you are trying to add an integer (key) and a character(s[i]) and store in the "string c".

Also your implementation of shift wont work. Basically you need to iterate over every character in the plaintext and print the shifted value.

You have to be very careful of type conversion when implementing it.

You must log in to answer this question.

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