Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, (i'm a beginner :) i defined a variable (string = userSecondInput) in the do-while loop ("do" part), but i can't use the variable in the "while"! why? :(
please help me :( i'm

What I have tried:

Console.WriteLine("Please Enter Your name: ");
string userInput = Console.ReadLine();
if (string.IsNullOrEmpty(userInput))
{
    do
    {
        Console.WriteLine("invalid value! Please Enter Your name: ");
        string userSecondInput = Console.ReadLine();
    } while (userSecondInput == "");
}
Console.WriteLine("welcome " + userInput);
Console.ReadKey();
Posted
Updated 16-Feb-17 23:37pm
Comments
Patrice T 17-Feb-17 4:47am    
Define: 'but i can't use the variable in the "while"!'
What is the problem.

This is called "scope". When you define a variable inside a section of curly brackets ({ ... }) then it isn't visible outside of those brackets.

do
{
    Console.WriteLine("invalid value! Please Enter Your name: ");
    string userSecondInput = Console.ReadLine(); // defined here
} while (userSecondInput == ""); //can't be seen here as outside of the brackets it is defined in


Do something like

Console.WriteLine("Please Enter Your name: ");
string userInput = Console.ReadLine();
string userSecondInput = null;
if (string.IsNullOrEmpty(userInput))
{
    do
    {
        Console.WriteLine("invalid value! Please Enter Your name: ");
        userSecondInput = Console.ReadLine();
    } while (userSecondInput == "");
}
 
Share this answer
 
Comments
Philippe Mori 18-Feb-17 9:50am    
Use while loop instead of do while here (and a single variable).
No need to create a variable inside the loop, you shall use the existing one.

static void Main(string[] args)
        {
            Console.WriteLine("Please Enter Your name: ");
            string userInput = Console.ReadLine();
            if (string.IsNullOrEmpty(userInput))
            {
                do
                {
                    Console.WriteLine("invalid value! Please Enter Your name: ");
                      userInput = Console.ReadLine();
                } while (userInput == "");
            }
            Console.WriteLine("welcome " + userInput);
            Console.ReadKey();
        }
 
Share this answer
 
Comments
Philippe Mori 18-Feb-17 9:50am    
Use while loop instead of do while here.
You have to create a variable before the loop if you want to use it in a loop condition. But why do you need
userSecondInput 
, it has never been used outside of the loop. You just want to capture none-empty input from the user, just use
userInput
will do. Try this
		if (string.IsNullOrEmpty(userInput))
{
    do
    {
	 Console.WriteLine("invalid value! Please Enter Your name: ");
	 userInput = Console.ReadLine();
    }
    while (string.IsNullOrEmpty(userInput));
}
 
Share this answer
 
Comments
Philippe Mori 18-Feb-17 9:50am    
Use while loop instead of do while here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900