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

Neil Gordon
Neil Gordon
8,823 Points

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

I am not sure how to approach this problem to see if the code complies. What should it comply to, an int or string data for input? i understand what the body is trying to accomplish but this is very vague was it suppose to be this way? I am still reworking things ....

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            input = Console.ReadLine("How are you today? or  \"quit\" to end the loop.:  ");

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

            Console.WriteLine(output);
        }
    }
}

4 Answers

Neil Gordon
Neil Gordon
8,823 Points

bothxp

Thank you the hint really helped a lot here is my new code

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {    
            Console.WriteLine("Please enter your name or \"quit\" to exit: ");
            string input = Console.ReadLine();

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

            Console.WriteLine();
        }
    }
}

What you said about the variable scope helped me to realize i needed a few steps in place before i could execute the if statements. also the if statements needed syntax adjustment. thanks again!

Hi Neil,

Here is the starting code:

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

But as is, it will not compile. Your task is to get it to. Basically just get the code to run.

The aim of the code is to read in some input from the end user. It then checks that input to see if it was 'quit'. If it was quit then the text 'Goodbye' should be printed out to the screen. If it was anything else then the text 'You entered' + the input should be printed out.

As it currently stands there are 2 problems in the code (you could call it 3) that you need to fix before the code will compile.

So have another go and then I can always take you through it later if you want.

Hint: Think about how you declare variables and about variable scope.

Update: Ok, I'll add my solution so you can compare it to yours

using System;

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

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

            Console.WriteLine(output);
        }
    }
}
Neil Gordon
Neil Gordon
8,823 Points

Ok thank you , Variable scope came into mind but I was so cautious about changing anything .lol gotta go with your gut sometimes. thanks for the help jumping back in shortly.

Thank you so much!!! I had the string output part right but thought I had to declare it at the same time. Then ended up with 3 declarations.

Neil Gordon
Neil Gordon
8,823 Points

the code is correct on my second post for those of you who might be stuck ....

Hi, You could review the video, "Validating input" (https://teamtreehouse.com/library/c-basics/perfect/validating-input)