0

after rewriting my code again i faced some proplems, when i compile it with 'a' or other key it's not printing anything, and i'm checking if user not providing plaintext it should print messege but it won't, any pointers?
here's my code

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

int shift(char c);

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

    if (argc != 2 && !isalpha(argv[1]))
    {

        printf("Usage: ./vigenere keyword\n");
        return 1;
    }

    string plaintext = get_string("plaintext: ");

    if (plaintext == NULL)
    {

        printf("please provide an input to cipher\n");
        return 1;
    }

    printf("ciphertext: ");

    int n = strlen(argv[1]);

    int key;

    for (int i = 0; i < n; i++)
    {

        key = shift(argv[1][i]);
    }

    return key;
    for (int i = 0, s = strlen(plaintext); i < s; i++)
    {

        if (isupper(plaintext[i]))
        {
            printf("%c", toupper(((plaintext[i] + key) - 65) % 26) + 65);
        }
        else if (islower(plaintext[i]))
        {
            printf("%c", tolower(((plaintext[i] + key) - 97) % 26) + 97);
        }
        else
        {
            printf("%c", plaintext[i]);
        }
        printf("\n");
    }
}

int shift(char c)
{

    if (isupper(c))
    {
        return c - 65;   
    }
    return c - 97;
}
1

Let's start here:

    if (argc != 2 && !isalpha(argv[1]))

BOTH conditions have to be met for the usage statement to be printed. The problem is that if either condition is met, it should print out the usage statement. Also, you're still trying to check a string with isalpha when it can only handle one char at a time.

Next is this:

for (int i = 0; i < n; i++)
{
    key = shift(argv[1][i]);
}

This for loop will walk through the key string, assigning a new value to key for each char processed. Unfortunately, all that does is end up assigning the value of the last char in the key string to key and reusing it for every char in the plain string processed later. The code needs to work and step through both the key string and the plain text string at the same time!!!! It can't just walk through the key string and then walk through the plain text string.

Finally, there's this:

    return key;

A return statement in main() will immediately terminate the program. Since this returns a value, key, it will just return the last key value processed to the operating system. Nothing is processed after this.

Since you haven't had a chance to analyze what happens with later code, and there's so much critical work to fix above, I'll let you chew on this for a while and try again. Maybe a new question is in order when you've had a chance to update the code again?

Also, you need to spend some quality time with debug50 (you'll learn about that soon), actually running your code and analyzing exactly what it is doing step by step and line by line. You should have been able to catch some of this on your own. You really need to develop debugging skills now. This is arguably the most important skill a programmer needs!

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

1
  • Thanks for the advice as i'm new to programming i just starring at the code for almost five days for now and saying what am i suppose to do here, you helped me a lot with your pointers here and there i assume i''l have to stare more with this code to figure out how i'll do it. Have a nice day :)
    – ox0o
    Sep 8 '19 at 0:37

You must log in to answer this question.

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