0

I'm an absolute beginner. Sorry for the long code. It seems to be returning the desired result, i.e. print correct shape and reject number that is our of range. But what's the problem of my code? Thank you so much!

#include <stdio.h>
#include <cs50.h>

void print(void);
int main(void) 
{
    print();
    return 0;
}


void print(void)
{
    int n;
    do
    {
        printf("Please type a number 0~23:");
        n = get_int();
    }
    while ( n < 0 || n >23 );
    //print each line's header.
    //printf("Height: %i\n",n);
    for (int k = 0; k < n;k++)
    {
        //print spaces for left
        int i = 0;
        while(i < n-k+1)
        {
            printf(" ");
            i++;
        }

        //print hashes for left
        int j = 0;
        while(j < k+1)
        {
            printf("#");
            j++;
        }
        //print two spaces
        printf("  ");
        //print hashes for right
        int h = 0;
        while(h < k+1)
        {
            printf("#");
            h++;
        }
        //print a new line
        printf("\n");
    }
}

error message from check 50:

~/workspace/pset1/ $ check50 2016.mario.more mario.c:) mario.c exists :) mario.c compiles

:) rejects a height of -1

:( handles a height of 0 correctly \ expected an exit code of 0, not output of "\n"

:( handles a height of 1 correctly \ expected output, but not "\n # #\n"

:( handles a height of 2 correctly \ expected output, but not "\n # #\n ## ##\n"

:( handles a height of 23 correctly \ expected output, but not "\n # #\n ..."

:) rejects a height of 24

:) rejects a non-numeric height of "foo"

:) rejects a non-numeric height of ""

1

You have two leading spaces that shouldn't be there. Instead of i < n-k+1, use i < n-k-1.

| improve this answer | |
  • Thank you so so much!!! – Yang Mar 22 '17 at 20:53

You must log in to answer this question.

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