Square & hallow square using while loop

Question about this C program that I've written. The objective is to print a square and a hollow square to the size input by the user. I've got the solid square down fine, but I'm having a bit of trouble with the hollow one. I understand that something must be put in between the two loops, but I'm having difficulty finding the solution. Any suggestions for this source code? Thanks a lot ...

[code]
#include
int main( void )
{
int row;
int col;
int num;

printf("
");
printf( "Please Enter a Number Between 1 and 20: " );
scanf( "%d", &num );

printf( "
" );

row = 0;
while(row < num)
{
col = 0;
while( col < num)
{
printf("*");
col++;
}
printf("
");
row++;
}

printf("
");

col = 0;
while(col < num)
{
printf("*");
col++;
}

printf("
");

row = 0;
while(row < (num - 2))
{
printf("*");
row++;
col = 2;
while(col <= num)
{
if (col == num)
printf("*");
else
printf(" ");
}
printf("
");
col++;
}

col = 0;
while(col < num)
{
printf("*");
col++;
}

printf("
");

return 0;
}
[/code]

Comments

  • Oh man, you are so close!

    Here it is: Changes are in red.
    [code]
    #include
    int main( void )
    {
    int row;
    int col;
    int num;

    printf("
    ");
    printf( "Please Enter a Number Between 1 and 20: " );
    scanf( "%d", &num );

    printf( "
    " );

    //________________________ Square

    row = 0;
    while(row < num)
    {
    col = 0;
    while( col < num)
    {
    printf("*");
    col++;
    }
    printf("
    ");
    row++;
    }

    printf("
    ");


    //________________________ Hollow Square

    col = 0;
    while(col < num)
    {
    printf("*");
    col++;
    }

    printf("
    ");

    [color=Red] row = 1;
    while(row < (num - 1))[/color]
    {
    printf("*");
    row++;
    col = 2;
    while(col <= num)
    {
    if (col == num)
    printf("*");
    else
    printf(" ");
    [color=Red]col++;[/color]
    }
    printf("
    ");
    }

    [color=Red] if ( row < num )
    {[/color]
    col = 0;
    while(col < num)
    {
    printf("*");
    col++;
    }
    [color=Red] }[/color]
    printf("
    ");

    return 0;
    }
    [/code]

    Now lets make sure you understand this.
    First issue was:
    [code]
    ...
    row = 0;
    while(row < (num - 2))
    ...
    [/code]

    The issue here is that you already wrote the first row in the while loop preceding this one. So setting row to zero doesn't make any sense considering you are wanting to draw with this loop starting at the second (2-1) row. Also you want to draw these hollow lines up to the last row which is num-1 not num-2. Hence:
    [code]
    ...
    [color=Red]row = 1;
    while(row < (num - 1))[/color]
    ...
    [/code]

    Next was the move of
    [code]
    col++;
    [/code]
    into the inner while loop. If you actually ran your code you should have noticed the infinite loop... ^^;;

    And lastly the:
    [code]
    [color=Red]if ( row < num )
    {[/color]
    ...
    [color=Red]}[/color]
    [/code]
    In the case of 1 you draw the top and the bottom regardless of row size, this corrects that behavior.

    As a bonus side note: Developer tip: if your code doesn't do what you expect it to do, step through it [b]by hand[/b]. i.e. write down every step the code makes [b]on paper[/b]. From inputs, to iterations, to outputs, and you are bound to find the tiny error that you made.

    Regards.
  • how could i stop the user from providing an invalid input?
  • I don't know about stopping invalid input, but you can make it easier to tame should your program encounter it. For example, something like below would prevent stream errors

    [code]
    #include

    int main(void) {

    char buf[10];
    int num;

    do {

    printf("Enter a number 1-20: ");
    fgets(buf, sizeof buf, stdin);

    } while(((sscanf(buf, "%d", &num)) != 1) || (num < 1 || num > 20));

    printf("You entered %d", num);

    return 0;
    }[/code]

    As a side effect, something like 10asdf would be "valid" with 10 being stored in num.

    HTH
  • Well i tired to do something where it stays between 0 and 20 with and IF else but it didnt seem to work.


    so if anything other than 1-20 is given nothings given as an output.

  • Do you mean display a message for invalid input?

    [code]#include

    int main(void) {

    char buf[10];
    int num;

    do {

    printf("Enter a number 1-20: ");
    fgets(buf, sizeof buf, stdin);

    if(((sscanf(buf, "%d", &num)) != 1) || (num < 1 || num > 20))
    printf("invalid input: %s", buf);

    } while(num < 1 || num > 20);

    printf("You entered %d", num);

    return 0;
    }[/code]

    Note: fgets may store '
    ' char in buf, so it may affect any message. If you want to remove it

    [code]
    char *p;
    if(strchr(buf, '
    ') != NULL) *p = '';
    [/code]
  • nevermind, i put the whole square & hollow square in an if else like this and it worked

    here is the source for other people who may need it:

    [code]
    #include
    int main( void )
    {
    int row;
    int col;
    int num;

    printf("
    ");
    printf( "Please Enter a Number Between 1 and 20: " );
    scanf( "%d", &num );

    printf( "
    " );

    if (num <= 20){ /* if the number given is more than 20 quit */


    //________________________ Square

    row = 0;
    while(row < num)
    {
    col = 0;
    while( col < num)
    {
    printf("*");
    col++;
    }
    printf("
    ");
    row++;
    }

    printf("
    ");


    //________________________ Hollow Square

    col = 0;
    while(col < num)
    {
    printf("*");
    col++;
    }

    printf("
    ");

    row = 1;
    while(row < (num - 1))
    {
    printf("*");
    row++;
    col = 2;
    while(col <= num)
    {
    if (col == num)
    printf("*");
    else
    printf(" ");
    col++;
    }
    printf("
    ");
    }

    if ( row < num )
    {
    col = 0;
    while(col < num)
    {
    printf("*");
    col++;
    }
    }
    printf("
    ");
    }
    else {
    printf(" "
    );
    }
    return 0;
    }
  • [color=Blue]This code will crash.[/color]
    :
    : [code]:
    : char *p;
    : if(strchr(buf, '
    ') != NULL) *p = '';
    : [/code]:
    :
  • Indeed - typo. :)

    [code]
    if((p = strchr(buf, '
    ')) != NULL) *p = '';
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories