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

Jorge Kalil
Jorge Kalil
1,832 Points

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

How to make input and output availabe ?

Program.cs
using System;

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

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

            Console.WriteLine(output);
        }
    }
}

2 Answers

Steven Parker
Steven Parker
229,786 Points

:point_right: You just asked this same question two hours ago!

Since then, it looks like you've made some other changes that are not related to the assignment. Be careful to change only the parts needed to fix the existing code.

Here's the answer I already posted to your previous question, and an extra hint:

This challenge is unusual in that the provided code has errors, and your task is to fix them. The major issue here has to do with the scope of the variable named output:

  • output is defined more than once
  • output is defined inside the if statement, but it is referenced outside of it

To fix these, you'll want to move the definition of output to the proper scope, and then replace the multiple definitions with ordinary assignments.

You won't need to make any changes to input, all the problems are with output.

Jorge Kalil
Jorge Kalil
1,832 Points

Thank you so much!