Reputation: 13
I am trying to limit the input of the console in order to add an error message on invalid input. The input must be 9 numbers (Edit : In a range of 100000000 - 999999999) for it to be valid. The problem I am running into is getting the error to display properly as well as getting the program to continue to function. The program is set up to work with sin as an integer.
int sin;
Console.Write("Please enter sin number: ");
sin = int.Parse(Console.ReadLine());
I tried using this below but got endless error messages as well as invalid input on all inputs
var digits = Console.ReadLine();
while (digits.Length > 9 || digits.Length < 9)
{
Console.WriteLine("Error invalid entry");
}
How do I get the program to check the length of the digits as well as continue the program with sin as an int value so the rest of the program can work properly?
Upvotes: 0
Views: 9841
Reputation: 11763
you need to read again.
you're simply printing there's an error, but not prompting to get a new input with a new sin = int.Parse(Console.ReadLine());
The following should work (edited, cheers ben)
int sin;
Console.Write("Please enter sin number: ");
string input = Console.ReadLine();
bool parse_ok= int.TryParse(input,out sin);
while (!parse_ok || digits.Length != 9 || sin < 100000000 )
{
Console.WriteLine("Error invalid entry");
Console.Write("Please enter sin number: ");
input = Console.ReadLine();
parse_ok= int.TryParse(input,out sin);
} ;
Upvotes: 1
Reputation: 9041
You just need a series of three checks:
int.TryParse(string, out int)
With that, you can try the following:
using System;
public class Program
{
public static void Main()
{
int sin = 0;
Console.Write("Please enter sin number: ");
string input = Console.ReadLine();
while(input.Length != 9 ||
!int.TryParse(input, out sin) ||
sin < 100000000)
{
Console.WriteLine("Error invalid entry!");
Console.WriteLine("");
Console.Write("Please enter sin number: ");
input = Console.ReadLine();
}
Console.WriteLine("Entered number: {0}", sin);
}
}
Results:
Please enter sin number: 012345678
Error invalid entry!
Please enter sin number: 0123456789
Error invalid entry!
Please enter sin number: asdf
Error invalid entry!
Please enter sin number: 999999999
Entered number: 999999999
See working example here... https://dotnetfiddle.net/eHNy2O
Upvotes: 1
Reputation: 26333
Your Console.ReadLine()
is outside the loop, so once you get to the error message there's no way for the user to enter a new value; the error message just displays in an endless loop. Try this instead - once you're safely out of the loop due to good input the sin
variable will have the integer value of the SIN:
int sin = 0;
while (true) {
String digits = Console.ReadLine();
if (digits.Length == 9 && Int32.TryParse(digits, out sin)) {
if (sin >= 100000000) {
break;
}
}
Console.WriteLine("Error invalid entry");
}
Upvotes: 1
Reputation: 1387
This should work
var digits = int.Parse(Console.ReadLine());
while (digits.Length > 9 || digits.Length < 9)
{
Console.WriteLine("Error invalid entry");
// then add something like this
Console.Write("Enter a number: ");
digits = int.Parse(Console.ReadLine());
}
Upvotes: 0