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

Confused

I am not sure what exactly needs to be done.

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);
        }
    }
}

2 Answers

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

You need to declare the variable input and output

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        { 

            string input = Console.ReadLine(); // declare the variable
            string output = ""; // declare the variable

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

            Console.WriteLine(output);
        }
    }
}

Yes, this task is all about declaring the variables and doing so in such a way that they are in scope when you use them.

Steven Parker
Steven Parker
231,269 Points

There are actually three issues, requiring a minimum of four changes:

  • the variable input must be declared when (or before) it is assigned
  • the variable output must be declared in the widest scope it is used (*Main()" in this case)
  • inside the test blocks, output should be assigned but not declared

Geovanie's code is a complete solution, so you can see all these items fixed in it.