0

I'm trying to have my program work by inputting an integer between 10-50 and if they didn't input within the acceptable range the loop will go back by making them input again. But I can't seem to understand why my program doesn't work. I know the logic behind but I think the codes is the problem. Here is my code

Console.WriteLine("Enter a digit between 10 and 50 ");
xx = Console.ReadLine();
x = int.Parse(xx);
do
{
    if (x > 10 && x < 50)
        Console.WriteLine("Pleae input again: ");
}
while (x <= 10 && x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();
4
  • 2
    Change (x <= 10 && x >= 50) to (x <= 10 || x >= 50)
    – clover
    Aug 11, 2014 at 17:24
  • 1
    The problem is that you don't grab input inside the loop. The variable x never changes once you enter the loop. (In addition to what clover said)
    – Coda17
    Aug 11, 2014 at 17:25
  • But i guess that the && will not make a difference since it's constraining the digit to be just between 10 to 50. Aug 11, 2014 at 17:34
  • @user3806140: yes it will. x can not be smaller than 10 AND bigger than 50 ;)
    – Matthijs
    Aug 11, 2014 at 17:34

4 Answers 4

3

The if condition is wrong, and the while condition is wronger!

Try this instead:

Console.WriteLine("Enter a digit between 10 and 50 ");
do
{
    xx = Console.ReadLine();
    x = int.Parse(xx);
    if (10 <= x && x <= 50)
        break;
    Console.WriteLine("Pleae input again: ");
}
while (true);
9
  • 2 things, not wronger it's more wrong... Number 2 the OP should also use tryParse in another if statement for input validation otherwise it will throw an error when they enter a character instead of a number... unless they want it to throw an error then it is fine :)
    – DotN3TDev
    Aug 11, 2014 at 17:30
  • 1
    @developerIntern53718: Thank you for the English lesson. I did not really mean that one was "more wrong" than the other, just wanted to emphasize that the entire logic was generally incorrect. Aug 11, 2014 at 17:32
  • @developerIntern53718: As to the exception issue, since OP's code is not given inside a method, there is no way to tell anything about it, therefore no point in addressing that issue. Aug 11, 2014 at 17:33
  • That makes sense I know they didn't ask for it probably because they don't know to ask for it though...
    – DotN3TDev
    Aug 11, 2014 at 17:34
  • 1
    @user3806140: It means that you have a piece of code which will never be reached during runtime. If you're having problems understanding where exactly that piece of code is, then please publish that in a separate question, as it's a little hard for me to answer it without seeing the actual code. Aug 11, 2014 at 17:43
3

How about this:

    string xx;
    int x;
    Console.WriteLine("Enter a digit between 10 and 50 ");
    bool cont = true;
    do
    {
        xx = Console.ReadLine();
        if (int.TryParse(xx, out x) && 10 <= x && x <= 50)
            cont = false;
        else
            Console.WriteLine("Pleae input again: ");
    }
    while (cont);

Seeing while(true) makes my skin crawl. And, you should always use int.TryParse instead of int.Parse for user input.

6
  • 1
    Do while(true); instead and use a break statement. Then remove the else. This will shrink it by 2 lines. :) Aug 11, 2014 at 17:37
  • Yea, you're hilarious. And, it would shrink by 2 lines (removal of the cont variable declaration, and the else) - that's it. The cont=false would be replaced by a break.
    – Mike
    Aug 11, 2014 at 17:39
  • while(true) is an awful structure for code. break statements should be avoided whenever possible. I'm serious too.
    – Mike
    Aug 11, 2014 at 17:40
  • 1
    Who taught you that? As long as you know how to use break statements correctly, then it's perfectly fine to use them. There is a topic on that on this site actually.. Aug 11, 2014 at 17:41
  • 1
    In a small loop like this - not really terrible I suppose; however, it can make readability and traceability of more complex code difficult. And - that was reiterated in just about every software development class I've taken. I'm less concerned with 2 more lines of code - and more concerned with code maintainability.
    – Mike
    Aug 11, 2014 at 17:44
0

Check your IF and WHILE conditions.. Try this:

Console.WriteLine("Enter a digit between 10 and 50 ");

do
{
    xx = Console.ReadLine();
    x = int.Parse(xx);
    if (x <= 10 || x >= 50)
        Console.WriteLine("Pleae input again: ");
}
while (x <= 10 || x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();
1
  • This is my first code, it doesn't work and just gives me unreachable code detected. Aug 11, 2014 at 17:42
-1

You need to ask for the input each time or you wont come out of the loop.

    do
     {
      xx = Console.ReadLine();
      x = int.Parse(xx);  
      if (x > 10 && x < 50)
          Console.WriteLine("Pleae input again: ");
     }
    while (x <= 10 && x >= 50);
    Console.WriteLine("The number is in between!");
    Console.Read();
2
  • He still won't come out of the loop. It will loop while x is smaller than 10 and x is bigger than 50...
    – Matthijs
    Aug 11, 2014 at 17:33
  • This has a redundant check for the value of x - not really ideal.
    – Mike
    Aug 11, 2014 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.