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

Challenge 1 out of 1: What am I doing wrong?

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

}

Program.cs
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);
        }
    }
}

2 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Joe,

The challenge states "Declare the output variable just before the if statement and assign it to an empty string (i.e. "")."

While you have added the output variable before the if, you've not assigned it the empty string, or terminated the line.

You have:

string output = 

Needs to be:

string output = "";

Easy to miss, but you're on track now :) Keep going!

Dave.

Steven Parker
Steven Parker
231,210 Points

You're really close, it looks like you just have a typo.

On the line where "output" is declared, there's an equal sign ("=") at the end instead of a semicolon (";"):

            string output;  // fixed

The instructions say: and assign it to an empty string (i.e. ""), but that's not really necessary either for correct syntax or for passing the challenge.