0

I have the following code:

int orderQuantity;

Write("Number of Items: \t");
while(int.TryParse(ReadLine(), out orderQuantity) && orderQuantity > 0 == false)
{
     Write("Error: Number of Items must be a positive, whole number: \t");
     int.TryParse(ReadLine(), out orderQuantity);
}

The goal is that while the input is not a positive integer, continue to return an error. Once the input is a positive integer, continue on.

The problem is this logic only works with the negative numbers. When I try to make the code into while(!int.TryParse(ReadLine(), out orderQuantity) && orderQuantity > 0 == false)

I run into the code not recognizing integers nor negative numbers as errors.

1
  • 1
    Use while (!int.TryParse(ReadLine(), out orderQuantity) || orderQuantity <= 0) - "While the line couldn't be parsed as an int OR the parsed value was less than or equal to zero". And remove the parse inside the loop - it's being done in the while statement. Commented Oct 22, 2021 at 9:33

4 Answers 4

2

Use:

while(!int.TryParse(ReadLine(), out orderQuantity) || orderQuantity <= 0)
{
    Write("Error: Number of Items must be a positive, whole number: \t");
}

So you have to handle the cases that it's not a valid integer and that it's valid but not greater than zero separately. Also remove the TryParse from the loop body.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting the following errors: CS0019 Operator '||' cannot be applied to operands of type 'bool' and 'int' and CS0029 Cannot Implicitly convert type 'string' to 'int'. Both errors seem odd to me though. I must have messed up the syntax, so copying your solution exactly seemed to solve it.
1

you need to parenthesize the condition so this :

int.TryParse(ReadLine(), out orderQuantity) && orderQuantity > 0 == false

changed to be this :

(int.TryParse(ReadLine(), out orderQuantity) && orderQuantity > 0) == false

Comments

1

An extension method might help you to get code that is more expressive and thus more easy to read:

public static class StringExtensions
{
   public static bool IsPositiveInt32(this string input, out int value)
   {
      if(!int.TryParse(input, out value))
      {
         return false;
      }
      return value > 0;
   }
}

Usage will be

int orderQuantity;
Write("Number of Items: \t");
while(!ReadLine().IsPositiveInt32(out orderQuantity))
{
    Write("Error: Number of Items must be a positive, whole number: \t");
}

Comments

0
while((!int.TryParse(ReadLine(), out orderQuantity)) || (orderQuantity <= 0))

1 Comment

While this code snippet may solve the question, including an explanation will help people understand the reasons for your code suggestion.

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.