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

Derian McCrea
Derian McCrea
1,370 Points

Did you remove the 'output' variable? lost on the syntax of output.

I'm not sure what it means to expand the variable "output". It's placement looks incorrect in a few areas which i have replaced with Console.WriteLine. I've also tried to declare the output variable as a bool of string...but i think i'm reaching for a solution without abtually understand the logic.

any clarity and help is greatly appreciated!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            // promp user
            Console.Write("Enter your color or type \"quit\" to exit: ");
            input = Console.ReadLine();



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

            Console.WriteLine();
        }
    }
}

3 Answers

Simon McGuirk
Simon McGuirk
2,673 Points

Looking at the code above you’ve created a variable to store the user input however you havent specified what type of data the input variable will store.

Simon McGuirk
Simon McGuirk
2,673 Points

Okay just had a look at the original challange. The issue is the output variable is being defined inside the if statement meaning it is out of scope / no longer exists once the the code exits the if statement. In order to rectify this, you would need to create an output variable before the if statement. This would the store the value set in the if statement and is available to be called by the Console.WriteLine(output);

Derian McCrea
Derian McCrea
1,370 Points

Hello Simon,

I've tried to work out the scope of the variable and have reviewed the video's relating to it in previous lessons, but i'm stuck. I know what changing the scope of a variable is but my application of what to declare output is lacking. I'm not even sure what the output variable is supposed to equal. currently it's: string output = Console.WriteLine; (which i know is lame.) But I don't understand how to correct it. My code so far is:


using System;

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

    {   

        //get input from user 
        Console.WriteLine("enter \"quit\" or enter color");
        string output = Console.WriteLine;
        string input = Console.ReadLine();

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

        Console.WriteLine(output);
    }
}

}

I know this sounds bad but would it be ok to have the answer and then I can break it down on what i did wrong? I want to keep moving forward so I don't get snagged on this.

Thanks for your help!

Simon McGuirk
Simon McGuirk
2,673 Points

No problem. You almost had the answer.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            // Need to add the string Type for the declaration of input
            string input = Console.ReadLine();

            // Creates a variable called output of type string and assigns it the value of an empty string
            string output = "";

            if (input == "quit")
            {
                // Remove string declaration so you're just assigning the value to your existing variable
                output = "Goodbye.";
            }
            else
            {
                // Remove string declaration so you're just assigning the value to your existing variable
                output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}
Derian McCrea
Derian McCrea
1,370 Points

Thanks for your feed back!

I'm going to take a break for tonight and get at it first thing tomorrow morn.