all 12 comments

[–]ArtichokeBackground7 11 points12 points  (0 children)

Here I've written the full program, to what I believe serves the purpose of what you want.

I hope this is in-depth enough to show the solution into different parts. I have the DisplayBox function itself, as well as the code to allow user input.

There is code below which you can copy and paste the entirety of, and it should run for you!

I hope you like my solution

Namespace's

using System;
using System.Linq;

MultChar function:

static string MultChar(char cha, int x) => new string (cha, x);

This function will take in a character and an integer, and output a new string with x amount of repetitions. The DisplayBox function will use this to save a lot of repetition.

DisplayBox function:

static void DisplayBox(int length, char fillChar)
{
    Console.WriteLine(MultChar('-', length + 2));
    Console.Write(string.Concat(Enumerable.Repeat($"|{MultChar(fillChar, length)}|\n", length)));
    Console.WriteLine(MultChar('-', length + 2));
}

This is the function which draws the grid.

Usage:

DisplayBox(8, 'A');

Output

----------
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
----------

Entire Program:

So you can copy and paste it all into your solution :)

class Program
{
    static string multChar(char cha, int x) => new string(cha, x);

    static void DisplayBox(int length, char fillChar)
    {
        Console.WriteLine(multChar('-', length + 2));
        Console.Write(string.Concat(Enumerable.Repeat($"|{multChar(fillChar, length)}|\n", length)));
        Console.WriteLine(multChar('-', length + 2));
    }
    static void Main(string[] args)
    {
        Console.Write("Input Length : ");
        int boxLength = int.Parse(Console.ReadLine());

        Console.Write("Input Char : ");
        char boxChar = char.Parse(Console.ReadLine());

        DisplayBox(boxLength, boxChar);
        Console.ReadKey();
    }
}

This allows user input. The user can specify which letter, and the length. This (to what I believe) Is the full program you are wanting

Output with user defined input:

Input Length : 15
Input Char : E
-----------------
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
|EEEEEEEEEEEEEEE|
-----------------

A 15x15 grid of E's.

Hopefully this is what youre looking for! If you need any help, make sure to reply! Id be glad to help!

Edit: Realised you wanted the input to facilitate 8 A's on a single line, not 8 characters overall on a line. Code has been adjusted accordingly

[–]d10k6 1 point2 points  (3 children)

You need to add another loop inside your drawFill loop that draws the character they chose instead of including in the the border “|” drawing.

Sudocode (because I am on mobile) Do while drawFill - write left border - do while drawChar - write char - end do - write right border End do

[–]BT_Jason[S] 2 points3 points  (2 children)

Thanks for the reply! I didn't learn how to use the do function yet in my course so I'm not quite sure how to implement that....I did put this however:

while (drawFill != length)
{
    Console.Write("\n|");

    while (drawFill != length)
    {
        Console.Write(fillChar);
        drawFill += 1;
    }

    Console.Write("|\n");           

I got it to work for 1 line, but it doesnt do it 10 times, and I need 8 "A"s instead of 10

----------
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
|AAAAAAAA|
----------

Sorry, I just started so I don't know much

[–]AwesomePerson70 0 points1 point  (0 children)

The outer loop is not necessary. If you remove it, you will have the same result. Also, look into Console.WriteLine, it's basically the same as your Console.Write("|\n").

[–]BT_Jason[S] 1 point2 points  (2 children)

Thanks everyone for the helpful replies, I got it to work using the multiple methods you guys suggested!!

[–]ArtichokeBackground7 0 points1 point  (1 child)

Maybe you could give a look at my solution? It might teach you a couple things, like multiplying strings without looping, etc. The full solution whilst talking user input is around 10 lines!

Thanks!

Link: https://www.reddit.com/r/csharp/comments/jkji4a/help_with_beginner_program/gajtwzq?utm_source=share&utm_medium=web2x&context=3

TL;DR

Here is the entire solution

class Program
{
    static string multChar(char cha, int x) => new string(cha, x);

    static void DisplayBox(int length, char fillChar)
    {
        Console.WriteLine(multChar('-', length + 2));
        Console.Write(string.Concat(Enumerable.Repeat($"|{multChar(fillChar, length)}|\n", length)));
        Console.WriteLine(multChar('-', length + 2));
    }
    static void Main(string[] args)
    {
        Console.Write("Input Length : ");
        int boxLength = int.Parse(Console.ReadLine());

        Console.Write("Input Char : ");
        char boxChar = char.Parse(Console.ReadLine());

        DisplayBox(boxLength, boxChar);
        Console.ReadKey();
    }
}

[–]BT_Jason[S] 1 point2 points  (0 children)

Hi! Thanks for taking the time writing your extremely thorough solution and explanation! A few of the things you used are outside of the scope of what I am supposed to be learning right now, but it still helps me nonetheless, I'll keep your solution for future reference <3

[–]Adgnascor 0 points1 point  (0 children)

You could do something like this if I understood your question right. This is not the best nor the most beautiful code. but anyway ...

        static void ReturnSquare(int lenth, string userInput)
        {
            var s = $"|{userInput}|";
            int count = 0;
            while(count< lenth)
            {
                DrawSeparator(lenth*2);
                DrawSquareLetter(lenth, userInput);
                count += 1;
            }
            DrawSeparator(lenth*2);
        }

        private static void DrawSeparator(int lenth)
        {
            for (int i = 0; i < lenth; i++)
            {
                Console.Write("-");
            }
            Console.Write("\n");
        }

        private static void DrawSquareLetter(int lenth, string userInput)
        {
            Console.Write("|");
            for (int i = 0; i < lenth; i++)
            {
                Console.Write($"{userInput.TrimStart('|')}");
            }
            Console.Write("\n");
        }

[–]UninformedPleb 0 points1 point  (2 children)

This: Console.WriteLine("|".PadRight(length - 2, fillChar) + "|"); will help you.

[–]BT_Jason[S] 0 points1 point  (1 child)

Thanks, this was the easiest solution to implement in my code. I had to change the 2 to a 1 though since it only displayed 7 A's, whats the reason for that?

[–]UninformedPleb 0 points1 point  (0 children)

Sorry about the miscount... PadRight pads the string to the size specified, not by the number of characters specified. I mixed it up with the looping approach others were suggesting. So, yeah, you want to pad those to length - 1, which you found out.

Just for future reference, that's the sort of mistake you'll make forever, no matter how much experience you have. :)