hi

## Recommended Answers

Hi, try with this code:

``````private static void DrawABox( int x, int y, int width, int height,char Edge,string Message )
{
int LastIndex =0 ;
Console.SetCursorPosition(x, y);
for ( int h_i = 0; h_i <= height ; h_i++ )
{
if ( LastIndex != -1 )
{ …``````

Since your screen is flat you can't draw a cube on it. You can draw a 2-D projection of a cube, which, depending on the angle of viewing, could look like a square. Thus he's given you the drawing of a cube.

it's usually best if you try to write the code yourself and then ask for help with specific problems you're having rather than trying to just get someone to do your homework for you

## All 12 Replies

Hi, try with this code:

``````private static void DrawABox( int x, int y, int width, int height,char Edge,string Message )
{
int LastIndex =0 ;
Console.SetCursorPosition(x, y);
for ( int h_i = 0; h_i <= height ; h_i++ )
{
if ( LastIndex != -1 )
{
int seaindex = (LastIndex + ( width - 1) );
if(seaindex >= Message.Length -1 )
seaindex = Message.Length - 1;
int newIndex = Message.LastIndexOf(' ',seaindex);
if(newIndex == -1 )
newIndex = Message.Length - 1;
string substr = Message.Substring(LastIndex, newIndex - LastIndex);
LastIndex = newIndex;
Console.SetCursorPosition(x + 1, y + h_i);
Console.Write(substr);
}
for ( int w_i = 0; w_i <= width; w_i++ )
{

if ( h_i % height == 0 || w_i % width == 0 )
{
Console.SetCursorPosition(x + w_i, y + h_i);
Console.Write(Edge);
}

}

}``````

hi tnx for the code..but this program gives rectangle as its output but i want a CUBE.

Since your screen is flat you can't draw a cube on it. You can draw a 2-D projection of a cube, which, depending on the angle of viewing, could look like a square. Thus he's given you the drawing of a cube.

ok tnx..is it posible to draw a cube in windows application then?but i didn understand the use declaring message as a string..I'm a beginner..

it's usually best if you try to write the code yourself and then ask for help with specific problems you're having rather than trying to just get someone to do your homework for you

You could start the first line with 5 blanks, use dash - or underscore _ to print the top edge of the cube. (1 short of the cube length followed by |)
Next line 4 blanks, a /,2 short of the box length with spaces, followed by /|.
For the next 3 lines reduce a space at the beginning and add the space between the / and |.
Now for 2 short of the box length's lines put |, 2 short of length spaces, |, 4 spaces, /
Repeat that for 3 lines reducing the number of spaces to / by 1
Finish with dashes for the length.

Here's a not quite the same representative cube. Increase the length of the cube to make it more realistic. (Height 70% of width, + for corners
+-----------+
/ /|
/ / |
/ / |
/ / |
+-----------+ |
| | |
| | /
| | /
| | /
| |/
------------+

And of course the web formatting removed all the blanks I added in the above example.

For webapps, you've got a lot of reading to do. Look up System.Drawing.Drawing2D and look at their examples. That's a whole 'nother can of worms, you can spend a lot of time working on it. Enjoy.

Of course you could have a BitMap put in your console app and save your cube in an image file like png. (Once you've got your Winapp running, most of what you learn there, transfers to here.) Bits are more 1 to 1 in x/y directions. Think 3,4,5 triangles. Depending on the compression your viewer uses 32X32 is about a quarter of an inch more or less square.

``````using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the value");

int height = Convert.ToInt16(Console.ReadLine());

Program.DrawABox(height);
Program.DrawABox2(height);
Program.Drawline(height);
Console.ReadKey();
}
private static void DrawABox(int height)
{
int LastIndex =0 ;
string Message="asdf";
Console.SetCursorPosition(10, 10);
for ( int h_i = 0; h_i <= height ; h_i++ )
{
if ( LastIndex != -1 )
{
int seaindex = (LastIndex + ( height - 1) );
if(seaindex >=Message.Length -1 )
seaindex = Message.Length - 1;
int newIndex = Message.LastIndexOf(' ',seaindex);
if(newIndex == -1 )
newIndex = Message.Length - 1;
string substr = Message.Substring(LastIndex, newIndex - LastIndex);
LastIndex = newIndex;
Console.SetCursorPosition(10 + 1, 10 + h_i);
Console.Write(substr);
}
for ( int w_i = 0; w_i <= height; w_i++ )
{

if ( h_i % height == 0 || w_i % height == 0 )
{
Console.SetCursorPosition(10 + w_i, 10 + h_i);
Console.Write('*');
}

}

}
}

private static void DrawABox2(int height)
{
int LastIndex = 0;
string Message = "asdf";
Console.SetCursorPosition(15, 15);
for (int h_i = 0; h_i <= height; h_i++)
{
if (LastIndex != -1)
{
int seaindex = (LastIndex + (height - 1));
if (seaindex >= Message.Length - 1)
seaindex = Message.Length - 1;
int newIndex = Message.LastIndexOf(' ', seaindex);
if (newIndex == -1)
newIndex = Message.Length - 1;
string substr = Message.Substring(LastIndex, newIndex - LastIndex);
LastIndex = newIndex;
Console.SetCursorPosition(15 + 1, 15 + h_i);
Console.Write(substr);
}
for (int w_i = 0; w_i <= height; w_i++)
{

if (h_i % height == 0 || w_i % height == 0)
{
Console.SetCursorPosition(15 + w_i, 15 + h_i);
Console.Write('*');
}

}

}
}
private static void Drawline(int height)
{
int height1 = height / 2;
for (int w_i = 0; w_i <=height1; w_i++)
{
Console.SetCursorPosition(20 + w_i, 10+w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(10 + w_i, 10 + w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(10 + w_i, 20 + w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(20 + w_i, 20 + w_i);
Console.Write('*');
}

}
}
}
``````

Here is my code to draw a cube but its not working fine for all the inputs..It works fine if we give the x and y cursor position through output screen..But i want the cube by entering only one value from output screen

``````using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the value");
//int x = Convert.ToInt16(Console.ReadLine());
//int y = Convert.ToInt16(Console.ReadLine());
int height = Convert.ToInt16(Console.ReadLine());
//int width = Convert.ToInt16(Console.ReadLine());
//char edge = Convert.ToChar(Console.ReadLine());
//string message=Convert.ToString(Console.ReadLine());
//int size = Convert.ToInt16(Console.ReadLine());
Program.DrawABox(height);
Program.DrawABox2(height);
Program.Drawline(height);
Console.ReadKey();
}
private static void DrawABox(int height)
{
int LastIndex =0 ;
string Message="asdf";
Console.SetCursorPosition(10, 10);
for ( int h_i = 0; h_i <= height ; h_i++ )
{
if ( LastIndex != -1 )
{
int seaindex = (LastIndex + ( height - 1) );
if(seaindex >=Message.Length -1 )
seaindex = Message.Length - 1;
int newIndex = Message.LastIndexOf(' ',seaindex);
if(newIndex == -1 )
newIndex = Message.Length - 1;
string substr = Message.Substring(LastIndex, newIndex - LastIndex);
LastIndex = newIndex;
Console.SetCursorPosition(10 + 1, 10 + h_i);
Console.Write(substr);
}
for ( int w_i = 0; w_i <= height; w_i++ )
{

if ( h_i % height == 0 || w_i % height == 0 )
{
Console.SetCursorPosition(10 + w_i, 10 + h_i);
Console.Write('*');
}

}

}
}

private static void DrawABox2(int height)
{
int LastIndex = 0;
string Message = "asdf";
Console.SetCursorPosition(15, 15);
for (int h_i = 0; h_i <= height; h_i++)
{
if (LastIndex != -1)
{
int seaindex = (LastIndex + (height - 1));
if (seaindex >= Message.Length - 1)
seaindex = Message.Length - 1;
int newIndex = Message.LastIndexOf(' ', seaindex);
if (newIndex == -1)
newIndex = Message.Length - 1;
string substr = Message.Substring(LastIndex, newIndex - LastIndex);
LastIndex = newIndex;
Console.SetCursorPosition(15 + 1, 15 + h_i);
Console.Write(substr);
}
for (int w_i = 0; w_i <= height; w_i++)
{

if (h_i % height == 0 || w_i % height == 0)
{
Console.SetCursorPosition(15 + w_i, 15 + h_i);
Console.Write('*');
}

}

}
}
private static void Drawline(int height)
{
int height1 = height / 2;
for (int w_i = 0; w_i <=height1; w_i++)
{
Console.SetCursorPosition(20 + w_i, 10+w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(10 + w_i, 10 + w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(10 + w_i, 20 + w_i);
Console.Write('*');
}
for (int w_i = 0; w_i <= height1; w_i++)
{
Console.SetCursorPosition(20 + w_i, 20 + w_i);
Console.Write('*');
}

}
}
}
``````

Here is my code to draw a cube..It works fine for some input.I want it to work for all possible input's.

The term "output screen" is accurate, but a little confusing when you talk about getting input from it. You're getting input from the keyboard in this case, the screen echos what you type because of your programming.

I think you are confusing the visual display with physical attributes. If you type 10 the box routines physically create an 11X11 box of asterisks. (IE A totally square box.) Visually, it looks like a rectangle because characters are set up in a rectangular box where the vertical height is HIGHER than the horizontal width.

I don't know the exact proportions, but it looks like the width is about 80% of the height. That changes per device. You need to change the physical characteristics so the box LOOKS square. Try setting the width = height * 100 / 75; and playing around with the 75 to see if the box consistently looks square when you enter 5 or 80 for the width.

(You'll need to adjust the right lines' starting point with the same width.)

okay tnx..ll try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.