Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently trying to draw a solid square with (*)'s that is N length by N height using while loops but it returns as just a long line of (*)'s. Any help would be very much appreciated.

(Note: Sorry if this is a very novice question, I have only just begun to learn coding so please don't be too harsh, thank you :)).

What I have tried:

using System;
					
public class Program
{
	public static void Main()
	{
		var N = 5;
		var i = 0;
		
		while (N*N > i)
		{
			while (N > i)
			{
				Console.Write("*");
				i = i + 1;
			}
			Console.Write("\n");
		}
	}
}
Posted
Updated 20-Apr-19 23:44pm
v2

Think about it: can you really use the same variable in both loops?
You want two loops: one for rows, and one for columns:
while (more rows to do)
   set up for a row
   while (row not finished)
      print a star
   print a new line
N will remain the same for both loops, but you need separate X and Y counters.
 
Share this answer
 
Comments
QuantumNova 21-Apr-19 23:23pm    
Thank you, this help was very much appreciated :)
OriginalGriff 22-Apr-19 1:44am    
You're welcome!
You need to use a different counter for the inner loop. After printing the first 5 asterisks i will be equal to 6, so that loop will no longer be performed, and the outer loop will run forever. Try the following:
C#
while (N*N > i)
{
    var j = 0;
    while (N > j)
    {
        Console.Write("*");
        j = j + 1;
    }
    i += j;
    Console.Write("\n");
}
 
Share this answer
 
Comments
QuantumNova 21-Apr-19 23:23pm    
Thanks so much, this really helped a lot, you are a lifesaver :)
Quote:
but it returns as just a long line of (*)'s.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
QuantumNova 21-Apr-19 23:21pm    
Thank you, I appreciate the advice :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900