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 trialroseleen zhou
2,321 Pointsvariable scope
i don't know were im getting wrong. challenge task 1 0f 1 compiling the below code currently produces the error: " the name 'output' does not exist in the c The output variable is declared twice: once within the if statement's curly braces and again within . The last line of code in this program, console.writeLine(output);, is attempting to use a variable named output, which doesn't exist outside of the if/else statement's curly braces. To fix this error: Declare the output variable just before the if statement and assign it to an empty string (i.e"") Remove the string data types from the output variables within the if/else statement's curly braces.
program.cs
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine();
output = Console.WriteLine();
if (input =="quit")
{
string output = "";
}
else
{
string output = +input + ;
}
Console.WriteLine(output);
}
}
}
2 Answers
Tim Strand
22,458 PointsYou moved output = Console.Writeline above the curly braces (but didnt define output) then you declared the string output inside the curly braces. What the challenge wants is for you to declare the output variable as an empty string right below the input declaration (scope = Main()) then remove the string declaration inside the if statement (current scope = if statement, target scope = Main()) then do the same in the else statement. now you have 1 output variable for the program and its value gets set during the if/else statement. Then at the bottom that value gets written out.
roseleen zhou
2,321 Pointsthank you got it
roseleen zhou
2,321 Pointsroseleen zhou
2,321 Pointsthank you got it