# hollow square

• 07-04-2009
mdnealy
hollow square
need a hollow square 1-20 only. I cannot figure out how to make it hollow, only solid. where if you input something less than 1=a square of 1 asterek(sp) and if greater than 20=square of 20x20

here is what I have, just don't understand how to make it hollow, do I use a loop or if else..

Code:

``` static void Main(string[] args)         {             int side;             Console.Write("Enter the side of your square: ");             side = Convert.ToInt32(Console.ReadLine());             Console.WriteLine();                                         for (int i = 0; i < side;++ i )                               {                 for (int j = 0; j < side; ++j )                     Console.Write("* ");                 Console.WriteLine();                             }                                     Console.WriteLine();         }     }```
• 07-04-2009
tabstop
Either (a) you'll need to decide, inside your loop whether to print a * or a space--if you look at coordinates (your i and j), what's true when you need to print * and what's true when you need to print space or (b) you'll have to adjust your loops slightly to print "side" vs "inside".
• 07-05-2009
VirtualAce
Why in the world are you writing asterisks in C# when you have so many simple graphics at your disposal? If this is for an assignment, I understand, but IMO they sorely need to alter the curriculum a bit.

Simple window-graphics are about as easy and basic as they can be in C# .NET.
• 07-05-2009
theoobe
Seems like a weird way to draw a rectangle, but anyway...

Code:

```using System; namespace ConsoleApplication4 {     class Program     {         static void Main()         {             Console.Write("select the length of the sides: ");             int len;             if (!int.TryParse(Console.ReadLine(), out len))                 Main(); // invalid input             if (len < 1 || len > 20)                 Main(); // invalid input             for (int i = 0; i < len; i++)             {                 if (i == (len - 1) || i == 0) // first or last line                     Console.WriteLine(Repeater("* ", len));                 else // middle line                     Console.WriteLine("* " + Repeater("  ", (len - 2)) + "* ");             }             Console.WriteLine();             Main();         }         static String Repeater(String text, int count)         {             String tmp = "";             for (int i = 0; i < count; i++)                 tmp += text;             return tmp;         }     } }```
• 07-05-2009
mdnealy
I am just starting out in C# so please be a little patient :).

When I run the program I posted, it will give me a full square, i don't want a rectangle. but what i am having trouble with is how I make it hollow. The book we are learning from is easy when I put a program together like a gradebook or a calculator, but to create a graphic picture in console is throwing me off. The square can only have an input from 1 to 20. I do not have any examples in which to complete this in. I can create gui's but this is console only. In the post above this, it has commands that we have not gotten to. I am a quick study but I learn with repition(sp). It is an assignment but I am just having trouble grasping it. Once I see the code, I can usually build off of that. Dietel books are a little vague and I did not go over this in class...
• 07-05-2009
theoobe
I used the word Rectangle because in Graphics, a square would fall into the designation of Rectangle, although obviously all the sides will be the same length.

Could you please explain how the code I've posted fails to meet your criteria?
• 07-05-2009
mdnealy
Code:

```if (!int.TryParse(Console.ReadLine(), out len))                 Main(); // invalid input             if (len < 1 || len > 20)                 Main(); // invalid input             for (int i = 0; i < len; i++)             {                 if (i == (len - 1) || i == 0) // first or last line                     Console.WriteLine(Repeater("* ", len));                 else // middle line                     Console.WriteLine("* " + Repeater("  ", (len - 2)) + "* ");             }             Console.WriteLine();             Main();         }         static String Repeater(String text, int count)         {             String tmp = "";             for (int i = 0; i < count; i++)                 tmp += text;             return tmp;```
some of the coding i highlighted we haven't gotten to yet. like tryparse. it is quite a few chapters away. your code is helpful especially once we get there. your If statements helped. but like I said i am just a beginner in coding :)

I am trying to rewrite it to match up to what we have learned so far. :)
• 07-05-2009
theoobe
Ok, well int.TryParse works by evaluating a string and converting the result to an integer, similar to Convert.ToInt32().

However it is useful in that it incorporates error handling. In other words, if the user entered something non-numeric, rather than C# throwing an exception, it will simply return FALSE instead.

Example 1:

Code:

```int i; bool test = int.TryParse("12345", out i);```
Example 1 shows that the string value "12345" will be converted to the integer value of 12345 and stored in local variable i. The return value of the int.TryParse operation will be TRUE (because the conversion was successful).

Example 2:

Code:

```int i; bool test = int.TryParse("hello world", out i);```
Example 2 shows me trying to convert "hello world" into a number, which clearly would never work. As a result, local variable i remains unassigned, and the int.TryParse operation returns FALSE, but without throwing an exception.

Hope this helps. :-)
• 07-05-2009
mdnealy
it helped tons and made more sense. thanks for taking the time to do this. this discussion board has given me more answers than i have found in about 4 days!! thanks again for your help!!