Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I need to input number 3 and it should output in square form as following:
###
# #
###


Here is my code

lang"C#">
string input = Console.ReadLine();

 int s = #;
for (int i = 0; i<input.Count();i++)
{
   int a = convert.int32(input[i]);
       s = s * #;
    //this where I am stuck



}
Posted
Updated 5-Jun-13 23:57pm
v2

That is pretty much the oddest code I've seen in a while! :laugh:

I don't think you quite understand what you are trying to do - neither do I come to that, since your example of a "square form" doesn't seem to match anything I would recognise.

However, this may be just a mixup in language, or you assuming I can see your screen - which I can't.

So, you want to read a number from the user:
C#
string input = Console.ReadLine();
Yes, that'l work - it won't just read numbers though, so you will have to do some checking later.
The rest of it doesn't make much sense though, you don't appear to do anything with the values you converted, and multiplying strings by anything doesn't work.

So, go back a stage, and try to tell me what you are trying to achieve - show me what input generates what output, and I'll see if I can help you get the code together.

[EDIT]

OK, this isn't difficult, but it needs a little thought. Since it's your homework, I'm going to let you write the actual code, pretty much, but I'l help you along.

First things first: split this into two parts, by writing two methods:

C#
private int GetNumber()
    {
    int result = 0;
    // Your code goes here
    return result;
    }

private static void PrintSquare(int num)
    {
    Console.WriteLine(num);
    // Your code goes here
    }

You then call them from your Main method.
The first one is pretty trivial - you have that already (although I would recommend you do some error checking in case the user enters "Hello" instead of a number)

The second one is more complex (but not a lot).
First off, check that the number is positive and not zero - if it isn't then you don't need to do anything else.
Then, create two character arrays, big enough to hold all the characters you need in your line:
C#
char[] outerLine = new char[num];
char[] centreLine = new char[num];

Then, use a loop to fill the outer line completely with '#' characters, and the center line with spaces.
Then set the first and last element of the centerline to '#' characters.
Then print the data:
1) Print the outer line once:
C#
Console.WriteLine(outerLine);

2) Check if the number is greater than one. If it isn't you don't need to do anything else.
3) Use a loop to print the centreline the (number - 2) times
C#
   Console.WriteLine(centreLine);
4) Print the outer line once more.


Done!

I realize that some bits of this may not make a lot of sense, but they will if you give it a try. If you can't get it to work, show me the code you have so far, and I'll show you how to correct it.

[/EDIT]
 
Share this answer
 
v2
Comments
5Sazi 6-Jun-13 6:09am    
this should be an output if i input number 3
###
# #
###

This is code I have type
namespace square
{
class Program
{
static void Main(string[] args)
{
int num =Convert.ToInt32( Console.ReadLine());


for (int i = 0; i < num; i++)
{



}
Console.WriteLine(num);
Console.ReadLine();
}
}
}
OriginalGriff 6-Jun-13 6:34am    
Answer updated
You have to:
Correctly parse user input to obtain a meaningful integer, say N.
Then you have two special cases: first and last line, composed of N '#'; all the other lines have 1 '#', (N-2) blanks and 1 final '#'.
It doesn't look a daunting task to me.
 
Share this answer
 

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