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.


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 post
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 post
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 post
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.

  • Thanks 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...