0

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?

1

4 Answers 4

1

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");
}
Sign up to request clarification or add additional context in comments.

9 Comments

What happens if I enter 000000000? It has a length of 9, but is not a number between 100000000 - 999999999.
Ed, you're missing the opening brace on that while loop
@Shar1er80, that wasn't in your question so I didn't know it was a requirement. I've updated the answer to check for >= 100000000.
As stated by the OP, "The input must be 9 numbers (1-9) for it to be valid"
Aah, my bad. Does that mean 100000000 is also invalid because it has zeros in it? In that case you may want a regular expression for validation.
|
1

You just need a series of three checks:

  1. The length of the input is 9
  2. The input can successfully parsed to an integer int.TryParse(string, out int)
  3. The parsed integer is in the range of 100000000 - 999999999

With that, you can try the following:

Updated to include an error message

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

Comments

1

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);
}   ;

11 Comments

As long as you understand why it was in a loop (because you didn't ask for new input), you should be ok :)
Noctis what's with the prompt appearing twice and and the while keyword appearing twice?
I did what you said but now the program asks for the sin number twice in a row, and only checks validity on the second one.
Yep, it was a paste error (as I've said, didn't test, used a text editor). Updated, should work, better logic as well. If you fail to enter digits (write 34e34, it will pick it up as well.
This does not work if you enter 000000000. It has a length of 9, but does not fall in the range of 100000000 - 999999999.
|
0

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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.