0

I keep getting this error when trying to compile my vigenere code:

error: format specifies type 'int' but the argument has type 'unsigned long' [-Werror,-Wformat]

printf("%c", (((plain[j] - 'A' + 1) + (CipherKey % strlen(key) % 26)) + 'A'));

Can anyone explain to me what I've actually done here? Here's my program code:

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

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("Bad command or file name \n");
    return 1;
}//quits if there are not exactly 2 command line arguments

string key = (argv[1]);

for (int i = 0; i < strlen(key); i++)
{
    if (!isalpha(key[i]))
    {
        printf("Bad command or file name \n");
        return 1;
    }//quits program is any letter in the key is not alpha
    else
    {
        printf("plaintext: ");
        string plain = get_string();
        toupper(key[i]);
        int CipherKey = (key[i] - 'A' + 1);
        //create int which will be used to convert plaintext to ciphertext
        //each char in the CipherKey is now an int, from 1 - 26

        printf("ciphertext: ");
        {
        for (int j = 0; j < strlen(plain); j ++)//turn string plain into an array
        {
            if(isalpha(plain[j]) && isupper(plain[j]))
            {
                printf("%c", (((plain[j] - 'A' + 1) + (CipherKey % strlen(key) % 26)) + 'A')); //print capital encrypted letter
            }
            else if(isalpha(plain[j]) && islower(plain[j]))
            {
                printf("%c", (((plain[j] - 'a' + 1) + (CipherKey % strlen(key) % 26)) + 'a')); //print lowercase encrypted letter
            }
            else
            {
                printf("%c", plain[j]);
            }
        }
        printf("\n");
        return 0;
        }
    }
}

}

All your help is appreciated!

1
  • I can get the code to compile by changing "%c" to "%lu" on lines 39 & 43, and the program then prints lengthy numerical output. I'd love some advice for how to approach the vigenere cipher in a different way... clearly my logic is flawed.
    – Alex
    Nov 20 '17 at 0:41
1

I assume the % strlen(key) is causing some type conversion.

  1. Your logic is wrong. First iterate the key to ensure all characters are alphabetic, and maybe also uppercase the key there if you want. Then, after the first loop, iterate the plaintext, keeping a separate index for the key that you increment on encrypting a letter only. Separate loops.

  2. toupper(key[i]); does nothing. The function returns the uppercased version, but you ignore it. key[i] = toupper(key[i]); would have an effect.

  3. Don't add 1 for those numbers. Also, CipherKey % strlen(key) % 26 makes no sense. % strlen(key) needs to be applied to the key's index (unless you use other ways to wrap around the key's index), while % 26 should be the last step before adding 'A' or 'a' again.

2
  • Thanks Blauelf. Your advice helped. After a couple hours of work, I'm still stuck (the encryption isn't working as I expect), but my logic is much better.
    – Alex
    Nov 20 '17 at 22:04
  • I basically had to re-write the program, but I’ve got it working now. Your comments are much appreciated!!
    – Alex
    Nov 23 '17 at 18:54

You must log in to answer this question.

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