0

So I'm working on a C project which builds a pyramid and I need a little help. The program is to prompt the user for the pyramids height using an integer between 1-8 inclusive. If the users input does not cooperate with the prompt the program will re-prompt the user until they do so. So far I got the program to work however, there's a slit bug. Whenever I type in a non integer the program runs an infinite loop. I just want to program to accept integers. HELP!!

int main(void)
{
    int height;
    int dashes;
    printf("Height Please:\n");
    scanf("%d", &height);
    while(height<1 || height>8){
        printf("Wrong input, try again\n");
        scanf("%d", &height);
    } 

    for(int i=1;i<=height;i++)
    {
        for(dashes=1;dashes<=i;dashes++)
        {
        printf("#");
        }
        printf("\n");

    }

}
Patrick Dankyi
  • 99
  • 1
  • 1
  • 7
  • 1
    It is not safe to use `scanf()` without checking the return value. For further information, I suggest you read this: [A beginner's guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Mar 04 '20 at 03:53
  • Yes, I find that link very useful, too. – Andreas Wenzel Mar 15 '20 at 01:40

0 Answers0