0

I am having issues tackling the corner cases for Mario (e.g. height < 0 or height > 23). I'm assuming you prompt the user to keep entering an integer in a while loop until he/she has entered an integer within the bounds defined. However, I keep getting an infinite loop in my code as shown below:

printf("Please enter the half-pyramid's height:\n");
do 
{
    printf("Height: \n");
    int height = GetInt();
}

while (height < 0 || height > 23);
2
  • I would call this an "edge" case, because it's at the max/min of only a single variable and it's relatively likely to occur. "Corner" cases exist at the extremes of two or more variables (e.g., at the corner of an x-y grid) and as a result tend to be exponentially more rare. It's not uncommon for people to use "corner" case to describe situations that aren't even worth the time to deal with because they are so unlikely. Just FYI - some more discussion here. :) – Air Jul 30 '14 at 17:37
  • Thanks for the clarification! – Arun Suresh Jul 30 '14 at 22:06
3

You've declared your integer height inside the curly braces, so it is no longer in scope (no longer in existence) once you reach the end of the curly brace. I'm assuming you've also declared it before this bit of code you are showing, and it will be that height that is being compared.

Inside the do, simply set the height without declaring it again.

4
  • I changed it up a bit, but I'm still running into some issues. When I enter a valid integer, the program still goes through the while loop and prints whatever's inside it. – Arun Suresh Jul 29 '14 at 23:37
  • Here's the code for reference: printf("Please enter the half-pyramid's height: "); int height = GetInt(); while (height < 0 || height > 23) { printf("Height: "); height = GetInt(); } – Arun Suresh Jul 29 '14 at 23:37
  • Your initial code was fine. The only problem was that you were declaring int height inside the loop when you only needed to set it height = getInt(); – curiouskiwi Jul 30 '14 at 0:01
  • Made the appropriate fix and it worked. Thanks :). – Arun Suresh Jul 30 '14 at 0:05

You must log in to answer this question.

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