Jump to content

I know the problem but I do not know how to fix, so it works the same way as i intended to.


Guest

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...