Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPress in Console Application

Status
Not open for further replies.

hisheeraz

Programmer
Mar 24, 2003
28
0
0
AU
Hello Friends;
I am making this basic programm in C# using concole application. My programm Prompth the user to enter a number (integer) within the range 0 - 100. up this point the programm is working fine.

but when the user enters some non numeric value the i start getting system error. is there possibly a way, syntex that can actually tell that user that Only Numerals ar allowed no other value can be added.

I have Tried System.Windows.Forms and it is not working...may be bacause im working on a console application.

So how can i display an error message on the console screen that only numeric value should be entered.

here is a lice of code...that prompts the user...

Code:
do // start of do while loop
                {
                    // start of if statement
                    if (Number < MinRange || Number > MaxRange)
                    {
                        Console.Write("\n Error - Number must be between " + MinRange + " - " + (MaxRange - 1));
                    }
                    // tell teh user how many tries left
                    Console.Write("\n Tries Left " + i);
                    // prompt the user to enter the number and try their luck
                    Console.Write("\n Enter your choice --> ");

                    // convers the value to integer and then save it to Number variable
                    Number = Convert.ToInt32(Console.ReadLine());
                } while (Number < MinRange || Number > MaxRange);// end while loop

could any one please help me with this ?

Thanks

§ Rented Lips §
----------------
Begning To Learn
----------------
 
why not just Console.Readline() and then check if the item is numeric before converting it. If it's not numeric - ask again.

private void IsNumeric(string val)
{
try
{
int i = Convert.ToInt32(val);

return true;
}
catch
{
return false;
}
}
 
... or if you are using 2005 then have a look at:

[tt]Integer.TryParse[/tt]


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top