Stuck On Caesar Cipher

Link to project:
https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-caesar-cipher
.
My code:
program.cs:

using System; namespace CaesarCipher { class Program { static void Main(string[] args) { // This calls the Encrypt() method which asks the user to enter a secret message and it converts the message: Encrypt(); } static void Encrypt() { 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'}; while (true) { Console.Write("Enter a secret message: "); string stringMessage = Console.ReadLine().ToLower(); Console.Write("How many spaces do you want to shift your letters? "); int shiftAmount = Convert.ToInt32(Console.ReadLine()) % alphabet.Length; char[] secretMessage = stringMessage.ToCharArray(); char[] encryptedMessage = new char[secretMessage.Length]; for (int i = 0; i < secretMessage.Length; i++) { char currentChar = secretMessage[i]; int positionOfChar = Array.IndexOf(alphabet, currentChar); positionOfChar += shiftAmount; positionOfChar %= alphabet.Length; char newEncryptedChar = alphabet[positionOfChar]; encryptedMessage[i] = newEncryptedChar; } Console.WriteLine($"The encrypted message is {String.Join("", encryptedMessage)}"); while (true) { Console.WriteLine("Do you want to use the encrypting tool again? Enter yes or no."); string encryptAgain = Console.ReadLine().ToLower(); if ((!(encryptAgain == "yes") || (encryptAgain == "no"))) { Console.WriteLine("You should enter yes or no."); } else { break; } } if (encryptAgain == "no") { break; } } Console.WriteLine("Good bye!"); } } }

Error:


.
How do I fix this?

This is the same issue you had here. At compile time, the name encryptAgain does not exist as @toastedpitabread explained in the other topic. You declare it inside a while loop. The compiler doesn’t evaluate conditionals, so it doesn’t know that encryptAgain will be declared. You’ll need to declare it prior to the loop.

Anyway, I declared encryptAgain in the nested while loop which is an even inner scope. I wanted to find a way to exit the nested loop and outer loop in the nested loop but I found out a way, using return.

But what if this happened:
You were making a game(video game; thi is actually realistic because I’m learning C# to make video games) and you have mini games(a nested loop, level 2) and inside the mini game(nested nested loop, level 3) you are asking the player something until they get a valid choice. But in the inner inner loop when the computer checks the variable(the variable is the one the what the user typed when it asked the question in the inner inner loop), it has to exit to the outer loop(2 levels out) but if the program exits the level 3 loop(inner inner loop) they will go to the level 2 loop which doesn’t have the answer that was entered by the question asked in the level 3 loop, the level 2 loop can’t decide whether to exit that loop because the exiting relies on a condition which needs the level 3 question’s answer. So I mean that level 2 doesn’t have access to level three to break out of the 2 loops. I need a way to declare the question’s answer so that all of the loops(all 3 loops) can acess it so that the program can break out of it. Maybe I should just only declare but not set a value to the level 3 answer like this:

namespace HelloWorld { class Hello { // level 0(not in a loop): // declare but not give value: string ans; // level 3: // get user input for asking if the player wants to leave the game and store into ans and check if it's valid: // if it is yes or no(valid): //break;(stop asking) //not valid // continue; // level 2: // be able to access ans and perform a conditional to check if ans is yes: // if ans is yes: // get out of the inner loop to go to level 1(outer) loop: // break(go back to the main game) // if ans is no: // continue game; // level 1: // continue game. } } }

it’s so that the variable can be accessed in any loop which has is needed since I think there is no way to break out of the 2 inner loops to the outer one in the level 3 loop since I can’t do this:

// level 3(asking the player if they want to play the mini game again until they answer a valid answer) 
// if ans is yes(player said they want to no play again)
break (2); // used to break out 2 levels of the 3 level loop. (go to the main game and play more games if they say so)
// else
break; // it stops asking

Yeah, I mean that kind of structure? Is it possible to break out of a 3 level loop and break out of 2 levels but not use return?

I know that was long but I couldn’t really explain it that good.

I have fixed the Caesar Cipher game:
Look, here’s the code:
Program.cs:

using System; namespace CaesarCipher { class Program { static void Main(string[] args) { // This calls the Encrypt() method which asks the user to enter a secret message and it converts the message: Encrypt(); } static void Encrypt() { 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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '\'', '"', ',', '.', '<', '>', '/', '?'}; while (true) { Console.Write("Enter a secret message: "); string stringMessage = Console.ReadLine().ToLower(); Console.Write("How many spaces do you want to shift your letters? "); int shiftAmount = Convert.ToInt32(Console.ReadLine()) % alphabet.Length; char[] secretMessage = stringMessage.ToCharArray(); char[] encryptedMessage = new char[secretMessage.Length]; for (int i = 0; i < secretMessage.Length; i++) { char currentChar = secretMessage[i]; int positionOfChar = Array.IndexOf(alphabet, currentChar); positionOfChar += shiftAmount; positionOfChar %= alphabet.Length; char newEncryptedChar = alphabet[positionOfChar]; encryptedMessage[i] = newEncryptedChar; } Console.WriteLine($"The encrypted message is {String.Join("", encryptedMessage)}"); while (true) { Console.WriteLine("Do you want to use the encrypting tool again? Enter yes or no."); string encryptAgain = Console.ReadLine().ToLower(); if (!((encryptAgain == "yes") || (encryptAgain == "no"))) { Console.WriteLine("You should enter yes or no."); } else if (encryptAgain == "yes") { break; } else { Console.WriteLine("Good bye!"); return; } } } } } }