AaronParkes
AaronParkes

Reputation: 311

C# if else stops code from continuing

I have a section of code where I am trying to make it so the user puts in their name and I want it to recognise if it's an actually name. So to keep things simple I just want it to accept letters only.

I type a valid name in and it carries on to the next section of code. However when I put in invalid characters I get the "Invalid Name" message but after that no matter what I type in it just keeps saying "Invalid Name".

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        {
            Console.WriteLine("Welcome {0}", firstName);
        }
        isNotName = false;
    }     
    else
    {
        Console.WriteLine("Invalid Name");
        Console.ReadLine();
    }
 }

Upvotes: 0

Views: 200

Answers (8)

david.pfx
david.pfx

Reputation: 10878

The bug is that the value of firstName is never updated.

The code can be improved:

Console.WriteLine("Please Enter First Name");
string firstName = Console.ReadLine();
While (!Regex.IsMatch(firstName, @"^[a-zA-Z'-]+$")) {
    Console.WriteLine("Invalid Name");
    firstName = Console.ReadLine();
}
Console.WriteLine("Welcome {0}", firstName);

Upvotes: 0

StevieB
StevieB

Reputation: 1000

Your issue is here in the else part of the conditional:

Console.ReadLine();

It should be:

firstName = Console.ReadLine();

What's happening is that you're saving the user input the first time you read input from the console but not saving it in subsequent calls to ReadLine()

i.e. your code should be:

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        Console.WriteLine("Welcome {0}", firstName);
        isNotName = false;
    }     
    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine();
    }
}

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {            
        Console.WriteLine("Welcome {0}", firstName);            
        isNotName = false;
    }    
    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine(); // <---- re-assign name here
    }
}

Also I would refactor your code to remove boolean flag:

Console.WriteLine("Please Enter First Name");   
string firstName = Console.ReadLine();

while(!Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
{
    Console.WriteLine("Invalid Name");
    firstName = Console.ReadLine();
}

Console.WriteLine("Welcome {0}", firstName);

Upvotes: 5

vgru
vgru

Reputation: 51224

Just to try to be original, your peers would probably enjoy reading something like this:

string firstName;
while (!(Regex.IsMatch(firstName = Console.ReadLine(), @"^[a-zA-Z]+$")))
{
    Console.WriteLine("Invalid Name"); 
}

Console.WriteLine("Welcome {0}", firstName);

It's just for lulz though, assignment inside a conditional expression is always a bad idea. :)

Upvotes: 0

SteveP
SteveP

Reputation: 19093

You are not setting the firstName variable in the subsequent readline:

   Console.WriteLine("Invalid Name");
   firstName = Console.ReadLine();

Upvotes: 1

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

Try this in your else part

else
{
firstName = Console.ReadLine(); 
}

Upvotes: 1

Plue
Plue

Reputation: 1790

You read the name once.

Use string firstName = Console.ReadLine(); at the begining of the while loop.

Upvotes: 1

AlexH
AlexH

Reputation: 2700

The firstName value is not updated in the loop, so try this :

string firstName = Console.ReadLine();

 while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        {
        Console.WriteLine("Welcome {0}", firstName);
        }
        isNotName = false;
    }

    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine();
    }
 }

Upvotes: 5

Related Questions