The following program is refusing to compile because of these errors:
vigenere.c:52:31: error: incompatible integer to pointer conversion assigning to
'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
...ciphertext[i] = ((((plaintext[i] - 65) + keyword[num % keylength]) % 26) + 65);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vigenere.c:56:31: error: incompatible integer to pointer conversion assigning to
'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
...ciphertext[i] = ((((plaintext[i] - 97) + keyword[num % keylength]) % 26) + 97);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And here's the program, which is meant to implement a simple vigenere cipher:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("Invalid input; try again!\n");
return 1;
}
else if(argv[1])
{
for(int i = 0, n = strlen(argv[1]); i < n; i++)
{
if(!isalpha(argv[1][i]))
{
printf("Invalid input; try again!\n");
return 1;
}
}
}
// get plaintext from user
string plaintext = GetString();
string ciphertext[100];
int num = 0;
string keyword = argv[1];
int keylength = strlen(keyword);
// change key values from letters to shifts
for(int i = 0, n = keylength; i < n; i++)
{
if(isupper(keyword[i]))
{
keyword[i] = keyword[i] - 65;
}
else if(islower(keyword[i]))
{
keyword[i] = keyword[i] - 97;
}
}
for(int i = 0, n = strlen(plaintext); i < n; i++)
{
if(isalpha(plaintext[i]))
{
if(isupper(plaintext[i]))
{
ciphertext[i] = ((((plaintext[i] - 65) + keyword[num % keylength]) % 26) + 65);
}
else if(islower(plaintext[i]))
{
ciphertext[i] = ((((plaintext[i] - 97) + keyword[num % keylength]) % 26) + 97);
}
num++;
}
// non-alphabetic characters
else
{
ciphertext[i] = plaintext[i];
}
printf("%c", ciphertext[i]);
}
printf("\n");
}
I've no idea why the compiler throws the error, because I have an older version of the program, compiled a few months ago (the code is exactly the same on lines 52 and 56) which works just fine.
I'll really appreciate any and all help :)
Variable ciphertext
is array of char*
, I think it should be:
char ciphertext[1000]