Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help with text
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
viper012289




PostPosted: Sun Feb 19, 2006 3:32 pm   Post subject: help with text

hey i was wondering how i would centre text in file. i managed to get the text centred into the run window but when i check the text file its still aligned right...help would be appreciated

var streamnumber : int
var streamnumber2 : int
var words : string
var i : int

open : streamnumber, "text.txt", get
open : streamnumber2, "centre.txt", put
i := 1
loop
get : streamnumber, words : *
put : streamnumber2, words
locate (i, maxcol div 2 - length (words) div 2)
put words
i := i + 1
exit when eof (streamnumber)
end loop
close : streamnumber2
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Feb 19, 2006 3:43 pm   Post subject: (No subject)

Since a file doesn't have a "maxcol", the best thing you could do is go through all the lines and find the longest line. Set things so that this line will, when everything is "centred", just touch the left margin (ie. don't add spaces to this line). To "centre" other lines, pad them with spaces.
viper012289




PostPosted: Sun Feb 19, 2006 4:05 pm   Post subject: (No subject)

how exactly would i go about doing that.
Cervantes




PostPosted: Sun Feb 19, 2006 4:22 pm   Post subject: (No subject)

You'll learn more if you do it yourself than if someone tells you the answer. Give it a try.

You may find the String Manipulation Tutorial of help.
viper012289




PostPosted: Sun Feb 19, 2006 4:33 pm   Post subject: (No subject)

i managed to calculate how much i will be spacing by counting the characters in each line and then subracting it from the largest line in my case 70....but i'm not sure how to place it in the put line with the file...
Cervantes




PostPosted: Sun Feb 19, 2006 4:38 pm   Post subject: (No subject)

viper012289 wrote:
but i'm not sure how to place it in the put line with the file...

code:

put : fileNo, repeat (" ", num_of_spaces) + line
viper012289




PostPosted: Sun Feb 19, 2006 4:51 pm   Post subject: (No subject)

thanks so much it worked like a charm....only one question....sometimes when centreing the number of spaces technically is supposed to be 2.5
so my question is does it actually space 2.5 from the edge or does it just round and go to 3?
Cervantes




PostPosted: Sun Feb 19, 2006 5:01 pm   Post subject: (No subject)

It would put 2 or 3 spaces. Unless your font has a "half a space" character, it's impossible to have half a space. If you're using a monospaced font, it's impossible.
Sponsor
Sponsor
Sponsor
sponsor
sylvester-27




PostPosted: Mon Feb 20, 2006 8:43 am   Post subject: (No subject)

just for reference, use code tags when posting code
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: