Computer Science Canada

Field Widths

Author:  YasserSalama [ Sat May 17, 2008 9:59 pm ]
Post subject:  Field Widths

I am trying to use field widths but I am having some issues. If I use a simple field width such as the following:

code:

put "Item" : 12, "Qu." : 12, "Cost" : 12, "Total" : 12

put "Apples" : 10, 3 : 4, 0.75 : 14, 10 : 13


I get a normal layout where everything is lined up and the apples is under items, and the 10 is under quantity etc.

What I am trying to make is a different field width. I have the following code:

code:

for i: 0..14
put "Item" : 12, "Qu." : 12, "Cost" : 12, "Total" : 12

put i : 10, i : 4, i : 14, i : 13
end for


In this code, all it will do is just display 0 to 14 over and over in each column. The problem is that the first number the appears under item down the column, is not lined up under item. It is way to the left, and I can't align it so that it was the way apples was in the earlier code.

Does anyone know a fix for this problem? It would be greatly appreciated!

Thanks
- Yasser

Author:  richcash [ Sat May 17, 2008 10:32 pm ]
Post subject:  Re: Field Widths

It's because strings and integers use the field width aligning differently. Strings left-align and integers right-align by default.

Removing any field width on the first integer will make it a bit better :
code:
for i: 0..14
put "Item" : 12, "Qu." : 12, "Cost" : 12, "Total" : 12

put i, i : 12, i : 12, i : 12
end for

I'm not sure, do you want it left-aligned or right-aligned?

Author:  YasserSalama [ Sun May 18, 2008 12:58 am ]
Post subject:  Re: Field Widths

Oh okay. What you did worked. I wanted it left-aligned. Thank you so much for your help!

Author:  YasserSalama [ Sun May 18, 2008 1:04 am ]
Post subject:  RE:Field Widths

Okay, what I am using this for is a bit more complex. It has integers and strings on one line. How would I go about doing that? I have this so far:

code:

for i: 0..14
put "Question" : 16, "Your answer" : 20, "Correct answer" : 12
put i, studentanswers (i) : 50, correctanswers (i) : 12
end for


Can you fix it up so it would align correctly on the left?

Author:  richcash [ Sun May 18, 2008 1:46 am ]
Post subject:  Re: Field Widths

I assume that studentanswers and correctanswers are arrays of strings.

You just need to put some space after your integer. You can :
code:
put i, "" : 15, "first string" : 20, "second string" : 12



Edit : The locate and locatexy procedures are other ways to align text, though in this case neither would be as neat/concise.


: