0

I have the problem that my vigenere code somehow doesn´t seem to output anything after entering the plaintext...

I need help please.

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

int main(int argc, string argv[])
{
    if (argc < 2 || argc > 2)
    {
        printf("Please enter only one Command-Line Argument with only Letters.\n");
        return 1;
    }

    string key = argv[1];

    printf("plaintext: ");
    string input = get_string();

    int keyCounter = 0;

    for (int i = 0; i < strlen(input); i++)
    {
        if (input[i] < 65 || input[i] > 122)
        {
            printf("%c", input[i]);
            continue;
        }
        else if (input[i] > 90 && input[i] < 97)
        {
            printf("%c", input[i]);
            continue;
        }

        int cipher = 0;

        if (key[keyCounter] >= 65 && key[keyCounter] <= 90)
        {
            cipher = key[keyCounter] % 65;
        }
        else if (key[keyCounter] >= 97 && key[keyCounter] <= 122)
        {
            cipher = key[keyCounter] % 97;
        }

        if (input[i] >= 65 && input[i] <= 90)
        {
            if (input[i] + cipher > 90)
            {
                printf("%c", 97 + ((input[i] + cipher) % 90));
            }

            printf("%c", input[i] + cipher);
        }
        else if (input[i] >= 97 && input[i] <= 122)
        {
            if (input[i] + cipher > 122)
            {
                printf("%c", 65 + ((input[i] + cipher) % 122));
            }

            printf("%c", 65 + input[i] + cipher);
        }

        keyCounter++;
        if (keyCounter == strlen(argv[1]))
        {
            keyCounter = 0;
        }

    }

    return 0;
}

1 Answer 1

1

Actually, there is output. The problem is that the output is unprintable characters. Try printing the output as integers instead of characters. You might also want to print out the value of cipher next to the encoded letter's integer value. You'll find there's an issue with the formula for calculating the encoded letters. I'll leave it to you to find that issue, once you see what you're dealing with. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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