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 trialTiago Ramos
2,380 Pointswritting output variable
Hi,
I know that if I declare a variable within curly braces {} it exists only inside them so I called WriteLine(output) inside braces{} as well.
I didn't erased the output variable. I can't see what's my mistake.
Can you help please? Thank you :)
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
Console.WriteLine(output);
}
else
{
string output = "You entered " + input + ".";
Console.WriteLine(output);
}
}
}
}
2 Answers
Steven Parker
231,248 PointsThis might be considered changing "the intent or intended behavior of the code". But you have the right idea about the scope of "output".
Perhaps a better fix would be to just declare it in the same scope as the "WriteLine" statement.
Joe Nguyen
25,189 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);
}
}
}
This worked for me
Tiago Ramos
2,380 PointsTiago Ramos
2,380 PointsThanks for the tip Steven, you mean like this?
I tried but it didn't work
Steven Parker
231,248 PointsSteven Parker
231,248 PointsI was suggesting that you might leave the "WriteLine" statement as it was supplied, but declare "output" before the conditional, and then in the conditional blocks do an ordinary assignment instead of an initialization.