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();
user3806140
  • 5
  • 1
  • 4
  • 2
    Change `(x <= 10 && x >= 50)` to `(x <= 10 || x >= 50)` – clover Aug 11 '14 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 '14 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. – user3806140 Aug 11 '14 at 17:34
  • @user3806140: yes it will. `x` can not be smaller than 10 AND bigger than 50 ;) – Matthijs Aug 11 '14 at 17:34

4 Answers4

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);
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • 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 '14 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. – barak manos Aug 11 '14 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. – barak manos Aug 11 '14 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 '14 at 17:34
  • @developerIntern53718: I agree, but you can infer from the question at hand that he or she has much more basic issues to learn before getting into exception, etc., point being, giving them "a lecture" about exceptions wouldn't be very effective to begin with... – barak manos Aug 11 '14 at 17:36
  • I actually do a lot of tutoring in basic programming at a college level and this is actually very helpful... If you take time to explain this to them now it helps them understand the more advanced topics later... I'm not saying explain everything about exception handling but explaining the basics of using tryParse vs Parse is very valid at this level... This is irrelevant at any point currently because your answer does do everything the OP asked for :) – DotN3TDev Aug 11 '14 at 17:40
  • Hey thanks, but i have a question. What does "unreachable code detected" means? When I'm fabbling around my code on the while being true/false the if inside and the console writeline outside suddenly displays it – user3806140 Aug 11 '14 at 17:40
  • 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. – barak manos Aug 11 '14 at 17:43
  • Oh, just by explaining it makes me enlightened. Thanks! – user3806140 Aug 11 '14 at 17:47
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.

Mike
  • 643
  • 3
  • 10
  • 1
    Do `while(true);` instead and use a `break` statement. Then remove the `else`. This will shrink it by 2 lines. :) – But I'm Not A Wrapper Class Aug 11 '14 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 '14 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 '14 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.. – But I'm Not A Wrapper Class Aug 11 '14 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 '14 at 17:44
  • And that thread was closed as not constructive. Let's not continue that debate here. – Mike Aug 11 '14 at 17:52
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();
StarPilot
  • 2,246
  • 1
  • 16
  • 18
Andrew Lapham
  • 478
  • 4
  • 7
-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();
user2054388
  • 105
  • 2
  • 13