Alignment - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Alignment

Hi all

I have an question this code

[code]
AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
reset(DataFile);
redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
'Balance'+#10 );

WHILE not eof(DataFile) DO
BEGIN
read(DataFile,OneRec);

if (OneRec.bAdult = true ) then
Adult := 'Yes'
else
Adult := 'No';

redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
Adult +' '+#9 + FloatTOStr(OneRec.rBalance));

END;
CloseFile(DataFile);
[/code]

gives me this answer

[code]
Initials Surname Adult Balance

AB DU PLESSIS Yes 235.5
G HOLTZHAUSEN No 110
Q SMITH Yes 0
K JONES No 350.71
M ZWANE Yes 0
MW DE KOKER No 35
P PETERS Yes 766.67
A MOLOI No 25
J VAN DER MERWE Yes 0
JJ LESSING Yes 0
LCM KRUGER No 99.43
B POTOMAC Yes 0
[/code]

now my question is how do I get it all aligned under its header

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi all
    :
    : I have an question this code
    :
    : [code]
    : AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
    : reset(DataFile);
    : redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
    : 'Balance'+#10 );
    :
    : WHILE not eof(DataFile) DO
    : BEGIN
    : read(DataFile,OneRec);
    :
    : if (OneRec.bAdult = true ) then
    : Adult := 'Yes'
    : else
    : Adult := 'No';
    :
    : redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
    : Adult +' '+#9 + FloatTOStr(OneRec.rBalance));
    :
    : END;
    : CloseFile(DataFile);
    : [/code]
    :
    : gives me this answer
    :
    : [code]
    : Initials Surname Adult Balance
    :
    : AB DU PLESSIS Yes 235.5
    : G HOLTZHAUSEN No 110
    : Q SMITH Yes 0
    : K JONES No 350.71
    : M ZWANE Yes 0
    : MW DE KOKER No 35
    : P PETERS Yes 766.67
    : A MOLOI No 25
    : J VAN DER MERWE Yes 0
    : JJ LESSING Yes 0
    : LCM KRUGER No 99.43
    : B POTOMAC Yes 0
    : [/code]
    :
    : now my question is how do I get it all aligned under its header
    :
    :
    Make sure all the surnames are of equal length by adding spaces to them. Or use the Format() function and specify the width of the string.
  • Hav0c #1Hav0c #1 Posts: 50Member
    : : Hi all
    : :
    : : I have an question this code
    : :
    : : [code]
    : : AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
    : : reset(DataFile);
    : : redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
    : : 'Balance'+#10 );
    : :
    : : WHILE not eof(DataFile) DO
    : : BEGIN
    : : read(DataFile,OneRec);
    : :
    : : if (OneRec.bAdult = true ) then
    : : Adult := 'Yes'
    : : else
    : : Adult := 'No';
    : :
    : : redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
    : : Adult +' '+#9 + FloatTOStr(OneRec.rBalance));
    : :
    : : END;
    : : CloseFile(DataFile);
    : : [/code]
    : :
    : : gives me this answer
    : :
    : : [code]
    : : Initials Surname Adult Balance
    : :
    : : AB DU PLESSIS Yes 235.5
    : : G HOLTZHAUSEN No 110
    : : Q SMITH Yes 0
    : : K JONES No 350.71
    : : M ZWANE Yes 0
    : : MW DE KOKER No 35
    : : P PETERS Yes 766.67
    : : A MOLOI No 25
    : : J VAN DER MERWE Yes 0
    : : JJ LESSING Yes 0
    : : LCM KRUGER No 99.43
    : : B POTOMAC Yes 0
    : : [/code]
    : :
    : : now my question is how do I get it all aligned under its header
    : :
    : :
    : Make sure all the surnames are of equal length by adding spaces to them. Or use the Format() function and specify the width of the string.
    :

    Can u explain Format() please because u gave it to me before but I dont understand it and Delphi doesnt explain it well
  • zibadianzibadian Posts: 6,349Member
    : : : Hi all
    : : :
    : : : I have an question this code
    : : :
    : : : [code]
    : : : AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
    : : : reset(DataFile);
    : : : redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
    : : : 'Balance'+#10 );
    : : :
    : : : WHILE not eof(DataFile) DO
    : : : BEGIN
    : : : read(DataFile,OneRec);
    : : :
    : : : if (OneRec.bAdult = true ) then
    : : : Adult := 'Yes'
    : : : else
    : : : Adult := 'No';
    : : :
    : : : redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
    : : : Adult +' '+#9 + FloatTOStr(OneRec.rBalance));
    : : :
    : : : END;
    : : : CloseFile(DataFile);
    : : : [/code]
    : : :
    : : : gives me this answer
    : : :
    : : : [code]
    : : : Initials Surname Adult Balance
    : : :
    : : : AB DU PLESSIS Yes 235.5
    : : : G HOLTZHAUSEN No 110
    : : : Q SMITH Yes 0
    : : : K JONES No 350.71
    : : : M ZWANE Yes 0
    : : : MW DE KOKER No 35
    : : : P PETERS Yes 766.67
    : : : A MOLOI No 25
    : : : J VAN DER MERWE Yes 0
    : : : JJ LESSING Yes 0
    : : : LCM KRUGER No 99.43
    : : : B POTOMAC Yes 0
    : : : [/code]
    : : :
    : : : now my question is how do I get it all aligned under its header
    : : :
    : : :
    : : Make sure all the surnames are of equal length by adding spaces to them. Or use the Format() function and specify the width of the string.
    : :
    :
    : Can u explain Format() please because u gave it to me before but I dont understand it and Delphi doesnt explain it well
    :
    Format() uses a string to describe how the result should be formatted. This string uses various specifiers to place parameters into the string. Of these specifiers the interesting ones for now are %s, and %m. These two are described in the help files under "Format Strings". The parameters to be entered are provided as an array, which can have nearly any time. For example:
    [code]
    Label1.Caption := Format('Hello %s', [Edit1.Text]);
    [/code]
    will do the same as:
    [code]
    Label1.Caption := 'Hello '+Edit1.Text;
    [/code]
    because it replaces the %s with the Text in Edit1. In a program this isn't that interesting, but the following is more interesting:
    [code]
    Label1.Caption := Format('Hello %12s!', ['world']);
    [/code]
    Now the label will contain:
    [code]
    Hello world !
    [/code]
    Notice the spaces between the 'world' and the '!'. Since the second function parameter is an array, you can give more than 1 value:
    [code]
    Label1.Caption := Format('%s %s!', ['Hi', 'world']);
    [/code]
    The order in which the parameter specifiers (in this case %s) appear must match the order in the array.
    In your program you want to give each of the strings a certain minimum and maximum length. This can also be done:
    [code]
    Label1.Caption := Format('%4s %4.4s!', ['Hi', 'world']);
    [/code]
    This code will generate the following output:
    [code]
    Hi worl!
    [/code]
    The word "Hi" gets 2 spaces appended, while the word "world" gets truncated to 4 characters, or gets spaces added to fill 4 characters. Given this code, you should be able to use the format to create each line, except for the money.
    The money is added by using the %m specifier:
    [code]
    Label1.Caption := Format('I have %m dollar.', ['125.23']);
    [/code]
    This will create the following line:
    [code]
    I have 125.23 dollar.
    [/code]
    Again the specifier is replaced by the value, and the value is also converted into a string.

    So by writing the correct format string as first function parameter and giving the values to insert into that string, you can quickly create a string matching a certain format. It takes a bit of practice to get accustomed to the Format() function, but once mastered, it is a very powerful tool. I would suggest that you experiment a little with it, in a small test project, before using it in your current project.
  • Hav0c #1Hav0c #1 Posts: 50Member
    : : : : Hi all
    : : : :
    : : : : I have an question this code
    : : : :
    : : : : [code]
    : : : : AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
    : : : : reset(DataFile);
    : : : : redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
    : : : : 'Balance'+#10 );
    : : : :
    : : : : WHILE not eof(DataFile) DO
    : : : : BEGIN
    : : : : read(DataFile,OneRec);
    : : : :
    : : : : if (OneRec.bAdult = true ) then
    : : : : Adult := 'Yes'
    : : : : else
    : : : : Adult := 'No';
    : : : :
    : : : : redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
    : : : : Adult +' '+#9 + FloatTOStr(OneRec.rBalance));
    : : : :
    : : : : END;
    : : : : CloseFile(DataFile);
    : : : : [/code]
    : : : :
    : : : : gives me this answer
    : : : :
    : : : : [code]
    : : : : Initials Surname Adult Balance
    : : : :
    : : : : AB DU PLESSIS Yes 235.5
    : : : : G HOLTZHAUSEN No 110
    : : : : Q SMITH Yes 0
    : : : : K JONES No 350.71
    : : : : M ZWANE Yes 0
    : : : : MW DE KOKER No 35
    : : : : P PETERS Yes 766.67
    : : : : A MOLOI No 25
    : : : : J VAN DER MERWE Yes 0
    : : : : JJ LESSING Yes 0
    : : : : LCM KRUGER No 99.43
    : : : : B POTOMAC Yes 0
    : : : : [/code]
    : : : :
    : : : : now my question is how do I get it all aligned under its header
    : : : :
    : : : :
    : : : Make sure all the surnames are of equal length by adding spaces to them. Or use the Format() function and specify the width of the string.
    : : :
    : :
    : : Can u explain Format() please because u gave it to me before but I dont understand it and Delphi doesnt explain it well
    : :
    : Format() uses a string to describe how the result should be formatted. This string uses various specifiers to place parameters into the string. Of these specifiers the interesting ones for now are %s, and %m. These two are described in the help files under "Format Strings". The parameters to be entered are provided as an array, which can have nearly any time. For example:
    : [code]
    : Label1.Caption := Format('Hello %s', [Edit1.Text]);
    : [/code]
    : will do the same as:
    : [code]
    : Label1.Caption := 'Hello '+Edit1.Text;
    : [/code]
    : because it replaces the %s with the Text in Edit1. In a program this isn't that interesting, but the following is more interesting:
    : [code]
    : Label1.Caption := Format('Hello %12s!', ['world']);
    : [/code]
    : Now the label will contain:
    : [code]
    : Hello world !
    : [/code]
    : Notice the spaces between the 'world' and the '!'. Since the second function parameter is an array, you can give more than 1 value:
    : [code]
    : Label1.Caption := Format('%s %s!', ['Hi', 'world']);
    : [/code]
    : The order in which the parameter specifiers (in this case %s) appear must match the order in the array.
    : In your program you want to give each of the strings a certain minimum and maximum length. This can also be done:
    : [code]
    : Label1.Caption := Format('%4s %4.4s!', ['Hi', 'world']);
    : [/code]
    : This code will generate the following output:
    : [code]
    : Hi worl!
    : [/code]
    : The word "Hi" gets 2 spaces appended, while the word "world" gets truncated to 4 characters, or gets spaces added to fill 4 characters. Given this code, you should be able to use the format to create each line, except for the money.
    : The money is added by using the %m specifier:
    : [code]
    : Label1.Caption := Format('I have %m dollar.', ['125.23']);
    : [/code]
    : This will create the following line:
    : [code]
    : I have 125.23 dollar.
    : [/code]
    : Again the specifier is replaced by the value, and the value is also converted into a string.
    :
    : So by writing the correct format string as first function parameter and giving the values to insert into that string, you can quickly create a string matching a certain format. It takes a bit of practice to get accustomed to the Format() function, but once mastered, it is a very powerful tool. I would suggest that you experiment a little with it, in a small test project, before using it in your current project.
    :


    zibadian thank u
    your answers is always help full and is makes more sense
    then Delphi's help files !!!!

  • zibadianzibadian Posts: 6,349Member
    : : : : : Hi all
    : : : : :
    : : : : : I have an question this code
    : : : : :
    : : : : : [code]
    : : : : : AssignFile(DataFile,'D:Delphi 7Projectsvraag 1 van eksamenmembersD.dat');
    : : : : : reset(DataFile);
    : : : : : redOutput.Lines.Add('Initials'+' '+#9+'Surname'+' '+#9+'Adult'+' '+#9+
    : : : : : 'Balance'+#10 );
    : : : : :
    : : : : : WHILE not eof(DataFile) DO
    : : : : : BEGIN
    : : : : : read(DataFile,OneRec);
    : : : : :
    : : : : : if (OneRec.bAdult = true ) then
    : : : : : Adult := 'Yes'
    : : : : : else
    : : : : : Adult := 'No';
    : : : : :
    : : : : : redOutput.lines.add(OneRec.sInitials+' '+#9 + OneRec.sSurname+' '+#9+
    : : : : : Adult +' '+#9 + FloatTOStr(OneRec.rBalance));
    : : : : :
    : : : : : END;
    : : : : : CloseFile(DataFile);
    : : : : : [/code]
    : : : : :
    : : : : : gives me this answer
    : : : : :
    : : : : : [code]
    : : : : : Initials Surname Adult Balance
    : : : : :
    : : : : : AB DU PLESSIS Yes 235.5
    : : : : : G HOLTZHAUSEN No 110
    : : : : : Q SMITH Yes 0
    : : : : : K JONES No 350.71
    : : : : : M ZWANE Yes 0
    : : : : : MW DE KOKER No 35
    : : : : : P PETERS Yes 766.67
    : : : : : A MOLOI No 25
    : : : : : J VAN DER MERWE Yes 0
    : : : : : JJ LESSING Yes 0
    : : : : : LCM KRUGER No 99.43
    : : : : : B POTOMAC Yes 0
    : : : : : [/code]
    : : : : :
    : : : : : now my question is how do I get it all aligned under its header
    : : : : :
    : : : : :
    : : : : Make sure all the surnames are of equal length by adding spaces to them. Or use the Format() function and specify the width of the string.
    : : : :
    : : :
    : : : Can u explain Format() please because u gave it to me before but I dont understand it and Delphi doesnt explain it well
    : : :
    : : Format() uses a string to describe how the result should be formatted. This string uses various specifiers to place parameters into the string. Of these specifiers the interesting ones for now are %s, and %m. These two are described in the help files under "Format Strings". The parameters to be entered are provided as an array, which can have nearly any time. For example:
    : : [code]
    : : Label1.Caption := Format('Hello %s', [Edit1.Text]);
    : : [/code]
    : : will do the same as:
    : : [code]
    : : Label1.Caption := 'Hello '+Edit1.Text;
    : : [/code]
    : : because it replaces the %s with the Text in Edit1. In a program this isn't that interesting, but the following is more interesting:
    : : [code]
    : : Label1.Caption := Format('Hello %12s!', ['world']);
    : : [/code]
    : : Now the label will contain:
    : : [code]
    : : Hello world !
    : : [/code]
    : : Notice the spaces between the 'world' and the '!'. Since the second function parameter is an array, you can give more than 1 value:
    : : [code]
    : : Label1.Caption := Format('%s %s!', ['Hi', 'world']);
    : : [/code]
    : : The order in which the parameter specifiers (in this case %s) appear must match the order in the array.
    : : In your program you want to give each of the strings a certain minimum and maximum length. This can also be done:
    : : [code]
    : : Label1.Caption := Format('%4s %4.4s!', ['Hi', 'world']);
    : : [/code]
    : : This code will generate the following output:
    : : [code]
    : : Hi worl!
    : : [/code]
    : : The word "Hi" gets 2 spaces appended, while the word "world" gets truncated to 4 characters, or gets spaces added to fill 4 characters. Given this code, you should be able to use the format to create each line, except for the money.
    : : The money is added by using the %m specifier:
    : : [code]
    : : Label1.Caption := Format('I have %m dollar.', ['125.23']);
    : : [/code]
    : : This will create the following line:
    : : [code]
    : : I have 125.23 dollar.
    : : [/code]
    : : Again the specifier is replaced by the value, and the value is also converted into a string.
    : :
    : : So by writing the correct format string as first function parameter and giving the values to insert into that string, you can quickly create a string matching a certain format. It takes a bit of practice to get accustomed to the Format() function, but once mastered, it is a very powerful tool. I would suggest that you experiment a little with it, in a small test project, before using it in your current project.
    : :
    :
    :
    : zibadian thank u
    : your answers is always help full and is makes more sense
    : then Delphi's help files !!!!
    :
    :
    Actually I find Delphi's help files rather good. I taught myself Delphi (including the Format() function) from the help files and trial-and-error. They explain a lot, once you know the basics of the Pascal language adn know the terminology used.
Sign In or Register to comment.