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!

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 trial

C# C# Basics (Retired) Perform if / else

ali raafat
ali raafat
444 Points

i need help because its not working it quits after i enter only one thing

guys i did every thing right but i got an compiler error and my app is not working well the error is "Program.cs(18,21): error CS0103: The name 'entry' does not exist in the current contex" and here is my snapshot : https://w.trhou.se/mcz6if1ayc

2 Answers

Steven Parker
Steven Parker
225,730 Points

:point_right: That means you're trying to use a variable that has not been declared yet.

On line 18, you test the variable entry to see if it contains "quit", but entry is not defined until line 26. And there it is within a new code block, so it cannot be accessed by the scope which includes line 18.

You can fix this by defining and initializing entry before line 19, and then on line 26 do a simple assignment:

/* new */       string entry =  "";              // do this first
/* line 18 */   if( entry == "quit")
// ....
/* line 26 */       entry = Console.ReadLine();  // now just an assigment
ali raafat
ali raafat
444 Points

it worked thank you but i moved "entry = Console.Readline " to line 17 so it can work

The problem is the scope of entry is only good in the else clause because that's where you initialized it. Move the string entry to before the while clause. That will make the scope cover your while statement and all of your if statement.

string entry; // <- add this line before the while statement.

string entry = Console.ReadLine(); // <- Remove the string from this line.