Guest Posted April 16, 2020 Share Posted April 16, 2020 using System; namespace ReadlinPractice { class Program { static void Main(string[] args) { string passWord = "pass123"; string userName = "user"; Console.WriteLine("Please enter your username below!"); string userName2 = Console.ReadLine(); do { Console.WriteLine("User not found! Please re-enter your username"); string userName4 = Console.ReadLine(); } while (userName4 != userName); } } } I want it to loop "User not found! etc" while im typing a non-exisiting username, but i cant make it work since i cant use userName4 outside of the 4th curly brackets (dont remember what its called (parameters?)), how can i work around this. Link to comment Share on other sites More sharing options...
Ingolme Posted April 16, 2020 Share Posted April 16, 2020 You declare it outside of the loop and use it inside. string userName4; do { Console.WriteLine("User not found! Please re-enter your username"); userName4 = Console.ReadLine(); } while (userName4 != userName); Link to comment Share on other sites More sharing options...
Guest Posted April 16, 2020 Share Posted April 16, 2020 5 hours ago, Ingolme said: You declare it outside of the loop and use it inside. string userName4; do { Console.WriteLine("User not found! Please re-enter your username"); userName4 = Console.ReadLine(); } while (userName4 != userName); Didn't work or i misunderstood using System; namespace ReadlinPractice { class Program { static void Main(string[] args) { string passWord = "pass123"; string userName = "user"; Console.WriteLine("Please enter your username below!"); string userName2 = Console.ReadLine(); string userName4; do { Console.WriteLine("User not found! Please re-enter your username"); string userName4 = Console.ReadLine(); } while (userName4 != userName); } } } I did this, and i tried putting string userName4; in the do aswell but it just created more errors. Link to comment Share on other sites More sharing options...
Ingolme Posted April 16, 2020 Share Posted April 16, 2020 What error message? You must not declare it inside the loop. Only outside. It's been a while since I last programmed in C# so I don't remember if it requires variables to be initialized. If it does, just initialize it to an empty string. string userName4 = ""; It has to be done outside of the loop. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now