Need help with spacing in a console app

T

Tony!

My teacher wants us to have 2 space margin from the left side of the
console window.

The following code displays the results fine but w/o the required
indented 2 spaces

//Write Header
Console.WriteLine("\n\nExponents
--------------------:\t\tRoots ----------------:");

Console.WriteLine("Number\tSquare\tCube\tFourth\t\tCube\tFifth\tEighth");


When I add the 2 spaces from left window to the start of each of the
two above writelines, it screws up the spacing of the first tab in the
string I'm writing.

So my header lines gets all screwed up.

Any thoughts on how to get around this?

Thanks,

Tony!
 
P

Peter Duniho

My teacher wants us to have 2 space margin from the left side of the
console window.

The following code displays the results fine but w/o the required
indented 2 spaces

//Write Header
Console.WriteLine("\n\nExponents
--------------------:\t\tRoots ----------------:");

Console.WriteLine("Number\tSquare\tCube\tFourth\t\tCube\tFifth\tEighth");

When I add the 2 spaces from left window to the start of each of the
two above writelines, it screws up the spacing of the first tab in the
string I'm writing.

So my header lines gets all screwed up.

Perhaps you could be more specific about "all screwed up".

Assuming you simply mean that the "Cube\tFifth..." part of the string
doesn't line up under the "Roots --..." part of the string, it seems to me
that you simply need to change the number of hyphens preceding "Roots
--..." or maybe even just remove one of the tabs.

In general, I tend to avoid tabs for plain text output. Because there's
no control over _where_ the tab stops actually are (in contrast to working
in a real word processer, for example), every time you change one little
bit of the string, you run the risk of the tab count being wrong for the
formatting you want. You can adjust for that, but it's just a headache if
you ask me.

If you want to use tabs, then you'll just have to make sure you use the
right number, as well as the right number of other characters that show up
as padding.

Pete
 
J

Julia M

Try and replace tabs with "hard" padding.
e.g.
string.Format("{0,10}","Number") returns "____Number"
string.Format("{0,-10}","Number") returns "Number____"

In your case I'd suggest

Console.WriteLine("{0,-10} {1,-10} {2,-10}", "Number", "Root",
"Square");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0,-10} {1,-10:0.0000} {2,-10}",i,Math.Sqrt
((double)i),Math.Pow((double)i,2));
}
 
T

Tony!

Perhaps you could be more specific about "all screwed up".

Assuming you simply mean that the "Cube\tFifth..." part of the string
doesn't line up under the "Roots --..." part of the string, it seems to me
that you simply need to change the number of hyphens preceding "Roots
--..." or maybe even just remove one of the tabs.

In general, I tend to avoid tabs for plain text output. Because there's
no control over _where_ the tab stops actually are (in contrast to working
in a real word processer, for example), every time you change one little
bit of the string, you run the risk of the tab count being wrong for the
formatting you want. You can adjust for that, but it's just a headache if
you ask me.

If you want to use tabs, then you'll just have to make sure you use the
right number, as well as the right number of other characters that show up
as padding.

Pete

Thanks for the replies...

I ended up using a combination of tabs and spaces. I'm used to writing
results back to a database table, an excel object, or a comma
delimited file... I'll be glad when we get past the console app stage
of the class.

-T-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top