C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Square & hallow square using while loop Posted by b3hr0uz on 6 Feb 2011 at 6:15 PM
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 ...

#include <stdio.h>
int main( void ) 
{
	int row; 
	int col;
	int num;

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

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

	printf("\n");

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

	printf("\n");

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

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

	printf("\n");

return 0; 
}

Report
Re: Square & hallow square using while loop Posted by nebgast on 6 Feb 2011 at 7:06 PM
Oh man, you are so close!

Here it is: Changes are in red.
#include <stdio.h>
int main( void ) 
{
  int row; 
  int col;
  int num;

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

  printf( "\n" );

  //________________________ Square

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

  printf("\n");


  //________________________ Hollow Square

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

  printf("\n");

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

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

  return 0; 
}


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


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:
...
row = 1;
while(row < (num - 1))
...


Next was the move of
col++;

into the inner while loop. If you actually ran your code you should have noticed the infinite loop... ^^;;

And lastly the:
if ( row < num )
{
  ...
}

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 by hand. i.e. write down every step the code makes on paper. From inputs, to iterations, to outputs, and you are bound to find the tiny error that you made.

Regards.
Report
Re: Square & hallow square using while loop Posted by b3hr0uz on 6 Feb 2011 at 7:31 PM
how could i stop the user from providing an invalid input?
Report
Re: Square & hallow square using while loop Posted by pseudocoder on 6 Feb 2011 at 7:51 PM
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

#include <stdio.h>

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;
}


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

HTH
Report
Re: Square & hallow square using while loop Posted by b3hr0uz on 6 Feb 2011 at 8:08 PM
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.
Report
Re: Square & hallow square using while loop Posted by pseudocoder on 6 Feb 2011 at 8:50 PM

Do you mean display a message for invalid input?

#include <stdio.h>

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;
}


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

char *p;
if(strchr(buf, '\n') != NULL) *p = '\0';

Report
Re: Square & hallow square using while loop Posted by b3hr0uz on 6 Feb 2011 at 9:05 PM
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:

#include <stdio.h>
int main( void ) 
{
  int row; 
  int col;
  int num;

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

  printf( "\n" );
      
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("\n");
    row++;
  }

  printf("\n");


  //________________________ Hollow Square

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

  printf("\n");

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

  if ( row < num )
  {
    col = 0;
    while(col < num)
    {
      printf("*");
      col++;
    }	
  }
  printf("\n");
}
else {
printf(" " \n);
}
  return 0; 
}
Report
Re: Square & hallow square using while loop Posted by AsmGuru62 on 7 Feb 2011 at 5:32 AM
This code will crash.
:
:
: 
: char *p;
: if(strchr(buf, '\n') != NULL) *p = '\0';
: 
:
:
Report
Re: Square & hallow square using while loop Posted by pseudocoder on 7 Feb 2011 at 10:44 AM
Indeed - typo. :)

if((p = strchr(buf, '\n')) != NULL) *p = '\0';





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.