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

Why Does Input Not Exist

Hi I am stuck and don't understand why input dies not exist in the context. Please tell me what is wrong with this code.

Program.cs
using System;

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

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


        }
    }
}

5 Answers

Two things missing:

  • You need to put the word "string" in front of the variable declaration of input.
  • Console.WriteLine(output)should be also in the else statement.

If you accomplish those, you should end up as:

using System;

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

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

~Alex

Thanks very much!

No problem!

You need to declare input with either one of the following: var input = ...

or

string input = ...

Did you try the challenge with just doing that?

It still causes an error that way, because you also need to add the Console.WriteLine() part inside the else statement :)

Hey Alexander I didn't actually attempt the challenge. I only answered regarding "Input".

Good call on the other error :)

It could also be solved by just declaring "var output = string.Empty;" just below or above "var input = ..." then the Console.WriteLine() can remain outside of the "if" conditions, but more than one way to skin a cat

Yeah.

using System;

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

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

            Console.WriteLine(output);
        }
    }
}

Agreed