-1
#include <stdio.h>
#include <string.h> // strlen()
#include <ctype.h> // isupper() , tolower()

void vigenereCipher(char* plainText, char* key);

int main(int argc, char argv[])
{
    char* key = argv[1];
    char plainText[101];

    // Ask the user for a sentence/word to encrypt
    printf("Please enter a word or sentence: ");
    fgets(plainText, sizeof(plainText), stdin);

    // Print the used encryption key
    printf("Your encryption key: %s\n", key);

    // Print the encrypted plaintext
    printf("Your encrypted message is: ");
    vigenereCipher(plainText, key);

    return 0;
}
void vigenereCipher(char* plainText, char* key)
{

    int i;
    char cipher;
    char cipherValue;
    int len = strlen(key);

    // Loop through the length of the plainText string
    for (i = 0; i < strlen(plainText); i++)
    {

        if (islower(plainText[i]))
        {
            cipherValue = ((int)plainText[i] - 97 + (int)tolower(key[i % len]) - 97) % 26 + 97;
            cipher = (char)cipherValue;
        }
        else
        {
            cipherValue = ((int)plainText[i] - 65 + (int)toupper(key[i % len]) - 65) % 26 + 65;
            cipher = (char)cipherValue;
        }

        // Print the ciphered character if it is alpha numeric
        if (isalpha(plainText[i]))
        {
            printf("%c", cipher);
        }
        else
        {
            printf("%c", plainText[i]);
        }

    }

}

vigenere.c:7:5: error: second parameter of 'main' (argument array) must be of type 'char **' int main(int argc, char argv[]) ^ vigenere.c:10:15: error: incompatible integer to pointer conversion initializing 'char ' with an expression of type 'char'; take the address with & [-Werror,-Wint-conversion] char key = argv[1]; ^ ~~~~~~~ & 2 errors generated.

I'm aiming for the encryption key of the program to be provided as an argument to the program but got the 2 errors above and don't know where to go from here. Any ideas? (end of code snippet)

This is for the CS50 project.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

The standard signature for main() is either an array of character pointers:

int main(int argc, char* argv[])

Or if you prefer a pointer to other pointer(s) to characters:

int main(int argc, char** argv)

What you have is an array of characters.

user2736738
  • 30,591
  • 5
  • 42
  • 56
tadman
  • 208,517
  • 23
  • 234
  • 262
1

You have missed an asterisk * in main. The second parameter of main is an array of char pointers: char *argv[].

Please note, as arrays decay to pointers when passing it to a function, it is also valid to write the second parameter as:

char **argv.

So, your main() should be:

int main(int argc, char *argv[])
{
    ...
}
user2736738
  • 30,591
  • 5
  • 42
  • 56
machine_1
  • 4,266
  • 2
  • 21
  • 42