Printing a Banner on Console Window

Cruckz

New member
Joined
Oct 21, 2022
Messages
3
Programming Experience
Beginner
Hello everyone,

I'm taking a c# course in college and still fairly new to the language so please bear with me on this one. I'm working on an assignment that is based solely on iterations and decision statements in a Visual Studio Console app, and essentially what I'm trying to do is create a hollow square with asterisks and have the user input a message that will read in the center of the square. The user must also input the width of the square and once everything is printed, they will be prompted again to write a message and input the width until they want to exit. I've been watching countless videos on loops and have spent an unhealthy amount of hours trying to get everything to line up properly, i'm slowly losing my mind.

This is what I have so far:
C#:
static void Main(string[] args)
        {
            Console.WriteLine("Enter a message:");
            string msg = Console.ReadLine();
            Console.WriteLine("Enter a width:");
            int width = int.Parse(Console.ReadLine());
            for (int k = 1; k < width; k++)
            {
                Console.Write("*");

            }
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("*");
                for (int j = 0; j < width; j++)
                {
                    Console.Write(" ");
                    if (i == 1)
                    {
                        for (int k = 1; k < msg.Length; k++)
                        {
                            Console.Write(" ");
                            Console.Write(msg);

                            break;
                        }
                    }
                }
                Console.WriteLine("*");
                Console.WriteLine();
            }
            for (int l = 0; l < width; l++)
            {
                Console.Write("*");
              
            }
          
            Console.ReadLine();
          
        }
I appreciate any and all help, thanks in advance.
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
It looks like you just jumped right in to write the code without a real plan on how to do things. Or if you did have a plan, your code implementation diverged from the plan.

In words, tell us the steps of your plan. Tell us how you will draw the box. Pay attention to the parts of the box that form the vertical sides. Be precise in describing how you intend to center the text with in the box.

Often, taking time to describe what you are planning or trying to do leads to insights. It's called rubber duck debugging.
 

Cruckz

New member
Joined
Oct 21, 2022
Messages
3
Programming Experience
Beginner
It looks like you just jumped right in to write the code without a real plan on how to do things. Or if you did have a plan, your code implementation succeed from the plan.

In words, tell us the steps of your plan. Tell us how you will draw the box. Pay attention to the parts of the box that form the vertical sides. Be precise in describing how you intend to center the text with in the box.
So, I've been messing around with the code and you are absolutely right, I had absolutely no plan going in. I re-evaluated what I'm trying to do, and now I'm getting somewhere, still struggling with how to properly center the text in the box, and i'm not entirely sure how to do it. But here's what i have after retrying:
Code:
static void Main(string[] args)
        {
            Console.WriteLine("Enter a message:");
            string msg = Console.ReadLine();
            Console.WriteLine("Enter a width:");
            int width = int.Parse(Console.ReadLine());
        
        
            for (int i = 0; i < 5; i++)
            {
                Console.Write("*");
              
                for (int j = 0; j < width - 2; j++)
                {
                    if(i == 0 || i == 4)
                    {
                        Console.Write("*");
                    }
                  
                    else
                    {
                  
                        Console.Write(" ");
                    }
                

                }
                Console.Write("*");
                Console.WriteLine();
            }
          
            Console.ReadLine();
        }
As you can see, I was able to nest a loop for the horizontal lines inside the loop for the vertical lines, where I created if/else statements to put the lines and spacing in the columns and rows where I wanted them to be executed. I believe I'll need to put the message input in the nested 'for' loop, I'm just unsure how to get it working as what I've tried so far has not executed the way I expected, the message seems to be caught up in the perimeter of the box no matter what specific rows or columns I assign to the statement.
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
Here's my recommendation. First try to solve the problem without playing code golf. In other words, skip using that outer i for loop for now. Use as many lines of code as needed to solve the problem.

Once you have solve the problem, then start looking at how to not do some things repetitively by duplicating lines of code. That will be when you will eventually have that other for i loop back in play again -- or you will make use of helper methods to encapsulate the duplicate code.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
So for now break down the problem.

Assuming that the message entered by the user is shorter than the width requested by the user you would have these high level steps:
1) Print the top border which is width asterisk characters wide.
2) Print the two sides for a blank line by printing an asterisk, width-2 spaces, and then another asterisk.
3) Print the centered message by printing an asterisk, some number of spaces, the message, some number of spaces, and then another asterisk.
4) Print the two sides for a blank line by printing an asterisk, width-2 spaces, and then another asterisk.
5) Print the bottom border which is width asterisk characters wide.

The number of spaces you need for step 3 should be relatively easy to compute by getting the width subtracting 2 and then subtracting the length of the message, and finally dividing by 2. You'll want that number of spaces on before the messages, and then the leftover spaces to the right.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
As aside, there is also a way to cheat at this by using:

This would reduce your steps to:
1) Print the top border which is width asterisk characters wide.
2) Do the following 3 times: Print the two sides for a blank line by printing an asterisk, width-2 spaces, and then another asterisk.
3) Print the bottom border which is width asterisk characters wide.
4) Setting the cursor position within the box previously drawn and printing the message.

Computing the cursor position will be similar to post #5 but you don't technically have to account for the asterisks at the sides, nor the leftover space.
 

Cruckz

New member
Joined
Oct 21, 2022
Messages
3
Programming Experience
Beginner
As aside, there is also a way to cheat at this by using:

This would reduce your steps to:
1) Print the top border which is width asterisk characters wide.
2) Do the following 3 times: Print the two sides for a blank line by printing an asterisk, width-2 spaces, and then another asterisk.
3) Print the bottom border which is width asterisk characters wide.
4) Setting the cursor position within the box previously drawn and printing the message.

Computing the cursor position will be similar to post #5 but you don't technically have to account for the asterisks at the sides, nor the leftover space.
Thank you for the help I really appreciate it.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,514
Location
Chesapeake, VA
Programming Experience
10+
Be careful with that cheat. Closed minded teachers in some Asian nations will give you a failing grade for doing things the non-standard way a traditional C, C++, or Java programmer would do it.
 
Top Bottom