Kennedy Tran
Kennedy Tran

Reputation: 93

How to restrict the user input to five digits?

I'm trying to restrict the user to only input 5 digits into the console for C#. I have my code error check the user, but for some reason after I type let's say...6 digits, the console stops after the while loop initiates. Can someone help me figure out what's wrong please? Thank you in advance!

Console.WriteLine("Enter the the zip code of the contact.");
temp = int.Parse(Console.ReadLine());

while (Console.ReadLine().Length != 5)
{
    Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
    temp = int.Parse(Console.ReadLine());

    if (Console.ReadLine().Length == 5)
    {
        temp = int.Parse(Console.ReadLine());
        address.zipCode = temp;
    }
}

Upvotes: 0

Views: 5977

Answers (6)

Abbas
Abbas

Reputation: 13

Please view the code given below. I think this will fulfill your requirement. I have found that you have use address.zipCode which has not been declared anywhere in given code so I have replace the address.zipCode with zipCode.

        int temp,zipCode;

        bool breakLoop = false;

        Console.WriteLine("Enter the the zip code of the contact.");

        while (breakLoop == false)
        {
            string userInput = Console.ReadLine();
            if (userInput.Length != 5)
            {
                Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
                continue;
            }

            int.TryParse(userInput, out temp);
            if (temp == 0)
            {
                Console.WriteLine("Error. Please enter a valid number.");
                continue;
            }                

            zipCode = temp;
            breakLoop = true;
            break;

        }

Please let me know if you have any problem with the code.

Upvotes: 0

SiwachGaurav
SiwachGaurav

Reputation: 1966

Modify your statements as

Console.WriteLine("Enter the the zip code of the contact.");

do
{
    temp = Console.ReadLine();
    if (temp.Length!=5)
    {
        Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
    }
    else
    {
        address.zipCode = temp;
    }
} while(temp.Length!=5);

done!

Upvotes: 0

Shirish
Shirish

Reputation: 1250

I think you can use like this also.. if u want to check all the lengths.

if (Console.ReadLine().Length == 5)
    {
        temp = int.Parse(Console.ReadLine());
        address.zipCode = temp;
    }
else{
while (Console.ReadLine().Length != 5)
{
    Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
    temp = int.Parse(Console.ReadLine());
    }
}

Upvotes: 2

Rohit.Net
Rohit.Net

Reputation: 33

dont use Console.Readline() again and again

change your code like this.

    Console.WriteLine("Enter the the zip code of the contact.");
     var temp = int.Parse(Console.ReadLine());

        while (temp.ToString().Length != 5)
        {
            Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
            temp = int.Parse(Console.ReadLine());

            if (temp.ToString().Length == 5)
            {
                temp = int.Parse(Console.ReadLine());

            }
        }

(to save from null reference you can do if(temp < 100000 && temp > 9999) also.)

Upvotes: 0

A.T.
A.T.

Reputation: 26312

It can be done this way...

            Console.WriteLine("Enter the the zip code of the contact.");
            var temp = Console.ReadLine();
            string zipcode = string.Empty;
            while (temp.Length != 5)
            {
                Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
                if (temp.Length == 5)
                {
                    zipcode = temp;
                    break;
                }
                else
                {
                    temp = Console.ReadLine();
                }
            }
            Console.Write(zipcode);

Upvotes: 0

JleruOHeP
JleruOHeP

Reputation: 10376

You are accessing console multiple times. Check for length of your captured input - temp. Try this:

var temp = Console.ReadLine();
while (temp.Length > 5)
{
    Console.WriteLine("Error. Zip code is not 5 digits. Please enter a valid number.");
    temp = Console.ReadLine(); 
}

address.zipCode = int.Parse(temp);

Upvotes: 3

Related Questions