Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Variable Scope

Fix the code so that it compiles, but don't change the intent or intended behavior of the code.

i have spent so much time on this could someone please help me

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            input = Console.ReadLine();

            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}

4 Answers

Steven Parker
Steven Parker
231,269 Points

There are a number of errors in this code.

They are all related to variable scope. Things to look out for:

  • variables used before they are declared
  • variables declared more than once
  • variables used in a broader scope than the one in which they are declared

And remember, you can only declare a variable once, but you can assign it any number of times.

Jose Lopez
Jose Lopez
4,615 Points

You never declared what variable type input was

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() {
input = Console.ReadLine();

        if (input == "quit")
        {
            Console.WriteLine("Goodbye."); //string output = "Goodbye."; this is the error 
        }
        else
        {
           Console.WriteLine("You entered " + input + ".";) //string output = "You entered " + input + "."; this is the error
        }

        Console.WriteLine(input);// output is the error
    }
}

}

Hopefully this helps.

Isaac Nilsen
Isaac Nilsen
568 Points

You'll need the "string" in front of input = Console.ReadLine();

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {    
            Console.WriteLine("Please enter your name or \"quit\" to exit: ");
            string input = Console.ReadLine();

            if (input == "quit")
            {
                Console.WriteLine("Goodbye.");
            }
            else
            {
                Console.WriteLine("You entered " + input + ".");
            }

            Console.WriteLine();
        }
    }
}
Steven Parker
Steven Parker
231,269 Points

:warning: FYI: I've been told by the moderators that Treehouse discourages code spoilers that have no explanations.