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 trialUnsubscribed User
1,653 PointsWhy Does Input Not Exist
Hi I am stuck and don't understand why input dies not exist in the context. Please tell me what is wrong with this code.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
Console.WriteLine(output);
}
else
{
string output = "You entered " + input + ".";
}
}
}
}
5 Answers
Alexander Davison
65,469 PointsTwo things missing:
- You need to put the word "string" in front of the variable declaration of
input
. -
Console.WriteLine(output)
should be also in theelse
statement.
If you accomplish those, you should end up as:
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);
}
}
}
}
~Alex
Kevin Ruddock
18,323 PointsYou need to declare input with either one of the following: var input = ...
or
string input = ...
Alexander Davison
65,469 PointsDid you try the challenge with just doing that?
It still causes an error that way, because you also need to add the Console.WriteLine()
part inside the else
statement :)
Kevin Ruddock
18,323 PointsHey Alexander I didn't actually attempt the challenge. I only answered regarding "Input".
Good call on the other error :)
It could also be solved by just declaring "var output = string.Empty;" just below or above "var input = ..." then the Console.WriteLine() can remain outside of the "if" conditions, but more than one way to skin a cat
Alexander Davison
65,469 PointsYeah.
Kevin Ruddock
18,323 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
var input = Console.ReadLine();
var output = string.Empty;
if (input == "quit")
{
output = "Goodbye.";
}
else
{
output = "You entered " + input + ".";
}
Console.WriteLine(output);
}
}
}
Kevin Ruddock
18,323 PointsAgreed
Unsubscribed User
1,653 PointsUnsubscribed User
1,653 PointsThanks very much!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsNo problem!