0

When I upload my program to check50, everything is green except I get this one error:

:( rejects a negative input like -.1
   \ killed by server

What do I need to do to fix this?

1

Case specific Approach

This is a piece of code that will take input a non negative integer.

int n = -1;
while (n < 0)
{
     //prompt to user to enter the desired integer
     n = GetInt();
}
// move further with value of n in range [0,+2147483647]

Use this or some similar logic to get value of n in desired range.

General Approach

Let our processing of any variable be defined in range from lower_limit to upper_limit (inclusive), then use this

int n = lower_limit - 1;
while (n < lower_limit || n > upper_limit)
{
     //prompt to user to enter the desired integer
     n = GetInt();
}
// move further with value of n in range [lower_limit, upper_limit]

to get the desired number.

2
  • I had just forgotten to re-ask for input after blocking the non-negative number. Silly me.
    – user1571
    Jul 13 '14 at 1:08
  • yes, if you involve a loop, then it prompts the user again and again for input unless a desired number is entered. For this, you must frame a test condition on basis of desired range of input.
    – sinister
    Jul 13 '14 at 3:39
0

You have to write code that doesn't accept a negative number, as negative change doesn't make sense. As long as the input is not in the correct range, keep asking for input, just like in mario.c

I can't help much more than that without having more details on what part you're having trouble with and what you have tried.

1
  • I had just forgotten to re-ask for input after blocking the non-negative number. Silly me.
    – user1571
    Jul 13 '14 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .