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 trialTojo Alex
Courses Plus Student 13,331 PointsI don't understand what i'm doing here
In this code challenge i didn't fully understand the second step of what its asking me to do I keep on getting the same error : Did you remove the 'output' variable? Instead of removing the 'output' variable, try correcting the problem by changing the scope of the variable.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();
string output = Console.WriteLine();
if (input == "quit")
{
string
}
else
{
string output = "You entered " + input + ".";
}
Console.WriteLine(output);
}
}
}
4 Answers
Robert Anthony
19,952 PointsThis is to do with the scoping of variables. A variable in C# only exists in the block of code { surrounded by curly brackets } in which is was declared. As the original code declared 'output' inside the if statement, once in each part, if either pieces of code executes, the variable 'output' will not exist by the time you get to the Console.WriteLine.
Remove the two 'string' keywords within the if statement. This will mean the output variable is modified with a new value rather than being declared again.
Modify your declaration to say:
string output="";
This will declare a variable called output, set it to an empty string ("") and make it available for the whole method.
Tojo Alex
Courses Plus Student 13,331 Pointsthanks for your help i understand now though it still shows the same error
Robert Anthony
19,952 Pointsusing 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);
}
}
}
Tojo Alex
Courses Plus Student 13,331 Pointsthank you its much clearer
Tojo Alex
Courses Plus Student 13,331 PointsTojo Alex
Courses Plus Student 13,331 Pointscould you show your code so i can see what i'm doing wrong in my code