0

I have been working on Vigenere for an embarrassingly long time and so I decided to try a different approach and start over from scratch. I want to take the key that the user imputs as argv[1] and make a new array with the value of each letter turned into the numbers 0-25. Then I plan on adding these values to the plaintext.

I know I have some errors in my first loop, though. I was wondering if I could get advice on what's wrong.

Here's an except of the code:

int main(int argc, string argv[])
{
    int n = 0;
    char x = 'a';

    // checks if there is only one argument
    if (argc == 2)
    {
        string k = argv[1];
        // declare array of chars that is the key
        n = strlen(k);
        char key[n];
        for (int i = 1, n = strlen(k); i < n + 1; i++)
        {
            if (isalpha(k[i]))
            {
                if (isupper(k[i]))
                {
                    x = (k[i] - 'A');
                    key[i] = GetChar();
                }
                if (islower(k[i]))
                {
                    x = (k[i] - 'a');
                    key[i] = GetChar();
                }
            }
        }
    }
    return 0;
}
0

Your logic is fundamentally right, but you have a couple issues. While you remember to allow space for the end of string marker \0 on your strings, you've forgotten that the first element in an array is 0, not 1, so a string array that contained cat would be in array[0] through array[2] with \0 in array[3].

Next, I have no idea what you are doing with key[i]=GetChar();, especially while you are still recoding the key that is in array k[]. You might consider waiting until you've finished the recoding process before getting the string to encode and only then get the whole string at once, if that's what you are doing. (If not, key[] doesn't appear to serve any purpose.) Also, putting the string to be encoded into a string array named key is really confusing. Maybe use a more appropriate name, like plaintext[]?

That should get you going.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Thanks for the help! I had used i = 1 in the for loop to try something that didn't work and I never turned it back into i = 0.
    – RLSK
    Aug 2 '15 at 15:37

You must log in to answer this question.

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