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 trialbenny thompson
1,806 PointsFix the code so that it compiles, but don't change the intent or intended behavior of the code.
i have spent so much time on this could someone please help me
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);
}
}
}
4 Answers
Steven Parker
231,269 PointsThere are a number of errors in this code.
They are all related to variable scope. Things to look out for:
- variables used before they are declared
- variables declared more than once
- variables used in a broader scope than the one in which they are declared
And remember, you can only declare a variable once, but you can assign it any number of times.
Jose Lopez
4,615 PointsYou never declared what variable type input was
Lawrence Babay
6,941 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine();
if (input == "quit")
{
Console.WriteLine("Goodbye."); //string output = "Goodbye."; this is the error
}
else
{
Console.WriteLine("You entered " + input + ".";) //string output = "You entered " + input + "."; this is the error
}
Console.WriteLine(input);// output is the error
}
}
}
Hopefully this helps.
Isaac Nilsen
568 PointsYou'll need the "string" in front of input = Console.ReadLine();
James Reinhold
12,361 Pointsusing 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();
}
}
}
Steven Parker
231,269 PointsFYI: I've been told by the moderators that Treehouse discourages code spoilers that have no explanations.