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
171,242 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.