Caesar Cipher Extension Method

Hi,

I completed the Caesar Cipher project and decided to try some suggested extensions. Sadly my Encrypt method (which takes in char array and key, and returns another char array) is not working. It feels like I tried everything, but I’m still left with the CS0161 error (“not all code paths return a value”).
It worked before, when it read user input and converted it to “encrypted” password.

I would appreciate some help :slightly_smiling_face:

using System;

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
   {
      char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

      Console.WriteLine("Welcome to Caesar cipher! Please write the message you want encrypted below...");
    
    }
  
  static char[] Encrypt(char[] secretMessage, int key)
  {
    char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      
      
      char[] encryptedMessage = new char[secretMessage.Length];
      bool isLetter;
     
     
      for (int i = 0; i < secretMessage.Length; i++)
      {
        
        char char1 = secretMessage[i];
        isLetter = Char.IsLetter(char1);

        if (isLetter == true)
        {
        int pos = Array.IndexOf(alphabet, char1);
        int newPos = (pos + key) % alphabet.Length;
        char newChar = alphabet[newPos];
        encryptedMessage[i] = newChar;
       }
        else 
        {
          i++;
        }
      }
     
      }
  
  
  
  }
}

P.S. I already tried taking out the if statement - no effect.

Hello, @viliuspl, and welcome to the forums.

You’ve specefied a return type, but the method lacks a return statement. Hence:

Beyond that, there are a few more issues, but see what you can do after figuring out what you want to return.

Hi @midlindner
Thanks for quick reply! I solved the problem right after I posted on forums…
But if you could point me to my other mistakes in this code, please do so, I would appreciate. I know this code is very basic and not really organized, but it’s a start!

I’d be happy to offer further suggestions, but it would be best if you posted your most recent version of the code first, so I don’t suggest changes that no longer apply.