0

My water program seems to work decently, it passes all of the check50 tests just fine. My issue is that when the user enters a negative integer, the program re-prompts with "Minutes; ", but when the user enters a non integer the program re-prompts with "Retry: ". In the solution to water.c posted, the user should be re-prompted with "Minutes: " in both cases; ie when a character or non-positive integer is input. I've begun working on Mario.c, and this issue persists for me there as well so I suspect my code is out of order or I'm not grasping the get_int function. I was wondering if anyone has seen this issue because it feel like it could be common.

Any help would be much appreciated! (I'm a coding newbie)

1 Answer 1

1

what you are seeing is a side effect of how get_int works. If you have something like:

int num;
do
{
    printf("Number: ");
    num = get_int();
}
while (num < 0);

And run it, you will see this behavior:

Number:  -1
Number: foo
Retry
2

But if you change your do while to this:

do
{
    num = get_int("Number: ");
}
while num < 0;

Then you will see:

Number: -1
Number: foo
Number: 2

The water staff solution has the prompt as the argument to get_int, rather than as a separate print. When there is no prompt argument, get_int falls back to printing "Retry" when it receives a non-integer.

Both solutions are considered correct (the ability to call get_int with a prompt was added this year, so some of the materials haven't been updated to reflect that change, which is why you might not know about it.)

1
  • Thanks so much, I'd spent hours trying to figure that out! Excellent explanation and it works perfectly now. Commented Nov 12, 2017 at 23:06

You must log in to answer this question.

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