Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,919 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,573 people online right now. Registration is fast and FREE... Join Now!




If the user doesn't enter a numeric value, have them keep trying

 
Reply to this topicStart new topic

If the user doesn't enter a numeric value, have them keep trying, until they enter a numeric value

OliveOyl3471
23 May, 2008 - 08:40 AM
Post #1

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,606



Thanked: 17 times
Dream Kudos: 150
My Contributions
I read PsychoCoder's snippet to check if a value is numeric in C#.

If I had that snippet in my code, I could write:

csharp


if(IsNumeric(val))
{
//do stuff
}
else
{
Console.WriteLine("You must enter a number. Try again.")
}



Could you put a while loop in there to make the user keep trying until they enter a number?

csharp

while(!IsNumeric(val))
{
int val=Console.Readline();
if(IsNumeric(val))
{
//do stuff
}
else
{
Console.WriteLine("You must enter a number. Try again.");
}
}





would that cause an infinite loop? do I need a break; ?

This post has been edited by OliveOyl3471: 23 May, 2008 - 08:43 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: If The User Doesn't Enter A Numeric Value, Have Them Keep Trying
23 May, 2008 - 12:29 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you wanted to use a while loop you just have to set it up a slight bit differently but yeah you have the general idea. Keep in mind that Console.Readline actually returns a string, not an integer.

csharp

String val = Console.Readline();

while(!IsNumeric(val))
{
Console.WriteLine("You must enter a number. Try again.");
val=Console.Readline();
}

// Do stuff here since val is numeric


Now of course using something like TryParse() might be more effective in a situation like this but it is up to you. You can read about tryparse at the link below...

Int32.TryParse Method on MSDN

Edit: Corrected redefinition of variable. My bad.

Enjoy!

"At DIC we be number parsing code ninjas... we be bool ninjas = Int32.TryParse("1337",intDIC);" decap.gif

This post has been edited by Martyr2: 23 May, 2008 - 12:39 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: If The User Doesn't Enter A Numeric Value, Have Them Keep Trying
23 May, 2008 - 12:34 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,907



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Just a minor correction to the code Martyr2 supplied. You don't want to redeclare 'val' inside the loop.
CODE

String val = Console.Readline();

while(!IsNumeric(val))
{
     Console.WriteLine("You must enter a number. Try again.");
     val=Console.Readline();
}

// Do stuff here since val is numeric

User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: If The User Doesn't Enter A Numeric Value, Have Them Keep Trying
23 May, 2008 - 06:30 PM
Post #4

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,606



Thanked: 17 times
Dream Kudos: 150
My Contributions
Thank you. biggrin.gif

I wanted to use TryParse, but I didn't know how to let the user keep trying until they enter a number, til I tried it. The following code seems to work correctly. There is a switch statement where the user must enter 1, 2, or 3. Should I put a default in the switch statement?

csharp


int game;
//if they didn't enter a number, return 0
int.TryParse(Console.ReadLine(), out game);
while (game <= 0 || game > 3)
{
Console.WriteLine("You must enter either 1, 2, or 3.");
int.TryParse(Console.ReadLine(), out game);
}

//do stuff, since they entered a number


There are several places in the program where the user is asked to input a number. Would it be more efficient to use a method
such as PsychoCoder's IsNumeric() , or to keep doing it as I have done this?



edit--I almost never get it right the first time. Forgot the error message, and then some other stuff. lol

This post has been edited by OliveOyl3471: 23 May, 2008 - 07:08 PM
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: If The User Doesn't Enter A Numeric Value, Have Them Keep Trying
23 May, 2008 - 07:19 PM
Post #5

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,606



Thanked: 17 times
Dream Kudos: 150
My Contributions
TryParse won't work for this, since sometimes the answer will be 0.

This is where I would need the IsNumeric() method...correct?

csharp

Console.WriteLine("Let's start subtracting!");
int sub1 = RandomClass.Next(1, 100);
int sub2 = RandomClass.Next(1, 100);
int trys = 0;
//make sure the first number is more than the second one, to avoid negative numbers
while (sub1 < sub2)
{ sub1 = RandomClass.Next(1, 100);
sub2 = RandomClass.Next(1, 100);
}
int diff = sub1 - sub2;
Console.WriteLine("Subtract " + sub1 + " minus " + sub2);
Console.WriteLine("Type your answer now.");
int diffGuess = int.Parse(Console.ReadLine());//check if input is numeric


This post has been edited by OliveOyl3471: 23 May, 2008 - 07:27 PM
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: If The User Doesn't Enter A Numeric Value, Have Them Keep Trying
24 May, 2008 - 09:28 PM
Post #6

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,606



Thanked: 17 times
Dream Kudos: 150
My Contributions
Instead of putting in a method for just one block of code, I changed the while loop:

csharp

//make sure the first number is more than the second one, to avoid negative numbers
//and make sure the two numbers are not equal, so the answer will never be zero
while (sub1 <= sub2)
{ sub1 = RandomClass.Next(1, 100);
sub2 = RandomClass.Next(1, 100);
}


Now TryParse will work. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:02AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month