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

Think about how to properly declare a variable and variable scope, then fix the following code so that it compiles. Be s

I dont know how to do this challange.

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

5 Answers

John Vieyra
John Vieyra
2,826 Points

using System;

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

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


    }
}

}

Because it took forever to get!!!

Stuart Wright
Stuart Wright
41,120 Points

A good way to start this challenge is to run the code exactly as it is, then read the error message when it fails to compile. The first error tells you that the name 'input' does not exist in the current scope, so perhaps you need to check that the 'input' variable was properly declared (hint: it isn't!). Hopefully that should get you started. Let me know if you still run into problems.

I added string infront of input and recheck the work. And the error says that the name output does not exist in the text.

Michael Milone
Michael Milone
1,474 Points
class Program
{        
    // the scope of these variables are available in the entire class
    private string output = string.Empty;

    static void Main()
    {
        // the scope of variables defined here are only available in its method block
        string input = Console.ReadLine();
        // we can also define string output = string.Empty here
        if (input == "quit")
        {
            // the scope of variables inside if statements, for loops, while loops are only
            // available inside them
            // string output = "Goodbye." will be only available in this if statement and not
            // in its else statement
            // so we use the variable defined in this class above or we can define it in the
            // method block like string input.
            output = "Goodbye.";

        }
        else
        {
            output = "You entered " + input + ".";

        }

        Console.WriteLine(output);
    }
}
John Vieyra
John Vieyra
2,826 Points

i just dont get it.