It wasn't completely clear, but it seems that you are having a problem with your key advancing when non-alpha letters are processed. If this is the case, the relevant code is what isn't shown. My best guess is that you are using the same index to advance through both your plaintext and your key. Because the key is not always used, (when non-alphas are processed in the plaintext), the key needs a separate and independent index variable that is only incremented when a letter is encoded.
BTW, what happens if there's a number in the plaintext in your code? Wouldn't it be a lot simpler to check if a letter isalpha(), or is not alpha?
If this answers your question, please click the check mark to accept. Let's keep up on forum housekeeping! ;-)