0

Here is an excerpt from my code for Mario.

int main(void)
{
    printf("Please enter the pyramid height.\n(A positive integer <= 23.)\n");
    int h=0;

    do
    {
        h = GetInt();
    }
    while (... );

    ...
}

The code works properly for every case, except when the user enters an integer value outside the acceptable range. When such a number is input, after hitting ENTER, it goes to a new line and just waits.

If ENTER is pressed again, only then do I see the RETRY prompt.

Any ideas?

Thanks, ldf

  • I haven't tested Appliance 2014 yet, but Appliance 19 tends to be a little laggy when executing commands on the terminal. Have you waited long enough (like 5..10 seconds), just in case? It happened a few times to me (couldn't wait! =P) – abelinux Jan 7 '15 at 17:51
  • It was actually check50 that was throwing the error. – LDF Jan 7 '15 at 17:54
  • @LDF Does this mean the problem has been solved? If not, can you post the error-checking condition? – Calculemus Jan 7 '15 at 17:57
  • Honestly, @Calculemus, I'm not sure what you mean by "error-checking condition" unless you are looking for the CS50 output. Sorry, but can you please clarify. – LDF Jan 7 '15 at 18:53
  • @LDF I mean the conditional statements that execute different code blocks depending on whether the integer value is within or outside the acceptable range. It will likely be an if condition of some kind. – Calculemus Jan 7 '15 at 18:59
1

Don't worry about the retry. Retry is only displayed when you enter something that is not a number, say a character like "space" or abc.

But the problem with your code is that you put the printf statement outside of the do while loop. Remember the do while loop executes the stuff inside first and then does it check the condition. So think about what happened with your program. The printf statement will execute only once, but if you enter a number the second time, the printf statement will not show up again.

| improve this answer | |
0

Before the printf statement you must state int h; then you say h = GetInt(); after the printf statement. In your do while loop you must print Retry: and h = GetInt(); when h is less than zero and greater than 23. Format:

int h;
printf(...);
h = GetInt();
if (//conditions)
do
{
    printf("Retry: ");
    h = GetInt();
}
while(//conditions)
| improve this answer | |
  • you don't need to print retry. Retry prints automatically when the user hits a character or string like say, bar. – Xihai Luo Jan 8 '15 at 21:56
  • Okay... didn't realize that. But that's okay! – Richard Qiu Jan 9 '15 at 4:57

You must log in to answer this question.

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