Search code examples
c#goto

How to do I clear the console and execute a specific block of code in an interactive story scenario


I've been taking computer programming classes all throughout high school and now that I'm in my senior year we're finally getting into actually typing code and making programs. My class is based off of C# and we just recently (this past monday) started learning actual code rather than theory.

I'm trying to get ahead of the curve and I've got Console.WriteLine(); and assigning basic variables, integers, ect. Basic things, I know, but we all start somewhere. I want to get ahead of the curve so I decided to start working on a VERY simple text based interactive story if you will. It is not anything with a battle system or stats. Simply a question is asked and options presented for the user to choose.

    Console.WriteLine("Please enter your name: "); 
    string name = Console.ReadLine();
    Console.Clear();
    Console.Write("Hi " + name);
    Console.WriteLine(", how are you?);
    Console.WriteLine();
    Console.WriteLine("1.) I'm well.");
    Console.writeLine("2.) I'm not well.");
    string areYouWell = Console.ReadLine(); 

    if (areYouWell=="1")
       {
           Console.WriteLine("I'm Well.");
       }
    else if (areYouWell== "2")
       {
           Console.WriteLine("I'm not well.");
       }
    else
       {
         // this is where my question is. i want this else statement to
            re-execute the Console.Write("Hi, " +name); line

My question is: I want to have the user returned to the beginning of a block of code if they input an unassigned value, how could I do that? I've used batch sparingly and the goto function in there but during my research on the topic I've learned goto is basically the 8th deadly sin. What is an alternative?

This is my code right now:

    namespace NameGame
    {

     class Jade
     {
        public static void Main(string[] args)
        {
            /*Tx-Y
             * T= Tier  x=tier level     Y=option
             *
             * 
             * 
             * */
            Start: Console.WriteLine("Hi, welcome to JADE. Im sure I could've come up with a clever acronymn.. but I didn't.");
            Console.WriteLine("JADE is the project that I work on in my free time to practice programming. I hope you enjoy.");
            Console.WriteLine();
            Console.WriteLine("(At any time press \"Esc\" to close the program or type \"Ctrl\" to come back to this screen");
            Console.WriteLine("What is your name?");
            string usersName = Console.ReadLine();
            Console.Clear();
            Console.Write("It's nice to meet you " + usersName);
            Console.WriteLine("! My name is Jade, a programmable coversationalist!");
            Console.Write("I would LOVE to have a conversation with you! Afterall, it ");
            Console.WriteLine("is what I was made for!");
            Console.WriteLine("\n");
            Console.WriteLine("So, what do you want to talk about?");
            Console.WriteLine("\n");
            Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
            Console.WriteLine("1.) How your day is going");// start of tier one questions | Jade T1-O1
            Console.WriteLine("2.) Relationships"); //Jade T1-O2   || For relationships use if statement to personalize based on a hand full of names or default to a simple message
                                                //add more dialogue branches 

            string questionOne = Console.ReadLine(); //declaring string questionOne for the first question jade asks the user
            if (questionOne == "1")
            {

                Console.Clear();
                Console.WriteLine("OOO! I love talking about people's days! What have you done today?");
                Console.WriteLine("\n");
                Console.WriteLine("1.) School work mostly"); //Tier2-Question1-Option1
                Console.WriteLine("2.) Took a test or quiz"); //T2-Q1-O2
                Console.WriteLine("3.) Died on the inside due to school's neverending drag of despair."); //T2-O3
                string T2Q1 = Console.ReadLine();
               // continue this path
            }
            else if(questionOne=="2")
            {
                Console.Clear();
                Console.WriteLine("OOO! I love a good romance! So, tell me. Is it good or bad?");
                Console.WriteLine("\n");
                Console.WriteLine("\n");
                Console.WriteLine("1.) Good"); //T2-Q2-O1
                Console.WriteLine("2.) Bad"); //T2-Q2-O2
                string T2Q2 = Console.ReadLine();
                //continue this path
            }
            else
            {
                return (if (questionOne == "1"));
            }

            Console.ReadKey();
        }
    }
}

Sorry for the lengthy post, I've been searching for a while now lol


Solution

  • There are many different ways to do this. One of the easiest to understand is a do ... while loop.

    string questionOne;
    
    do
    {
        Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
        Console.WriteLine("1.) How your day is going");
        Console.WriteLine("2.) Relationships");
    
        questionOne = Console.ReadLine();
        if (questionOne == "1")
        {
            // questions about how your day is going
        }
        else if (questionOne == "2")
        {
            // relationships questions
        }
        else
        {
            Console.WriteLine("I didn't understand your answer. Try again.");
        }
    } while (questionOne != "1" && questionOne != "2");
    

    The do ... while loop will repeat as long as questionOne is anything other than "1" or "2". So if the user enters "3", the program will output "I didn't understand your answer", and then it encounters the while statement. Since questionOneis neither "1" nor "2", it will go back to the start of the loop.