I know this is probably basic stuff with an easy solution, but I can't seem to find it anywhere in my textbook.

I am working with an array of records. The user enters the data for each field of the record. The first field is a string entered by the user naming the type of record. The user can enter up to 15 characters. I want the field width to remain at 15 regardless of how many characters the user enters. This is necessary in order for my the fields to align correctly when the array is displayed. With my limited programming knowledge, the display of the first field is always the number of charcacters the user entered which causes the diplay of the remaining fields to be unaligned and will no doubt cost me points on this assignment. Any help would be greatly appreciated. I have tried specifying a string length in the type declaration with no success.

Dani AI

Generated

Short answer: declaring CatTitle = string[15] limits how many characters can be stored, but it does not force a fixed-width display. For aligned columns print the name with a field width (or pad/truncate it) when you output it.

already had the right idea about a 15-character name; the missing piece is formatting at output time. In Turbo/Delphi/FreePascal you can specify a field width for strings in Write/Writeln. A positive width right-justifies; a negative width left-justifies. For example, to left-align the category name in a 15-character column and keep the amount aligned you can do:

writeln(BudgeRec.CatName:-15, BudgeRec.Amount:8:2);

That prints the name padded on the right to 15 columns and the amount in an 8-character field with 2 decimals.

's suggestion to use Copy is useful when input may exceed your limit — truncate to 15 characters first, then pad when printing. For more control (pad or truncate in one place) a small helper function that pads right to a fixed width is handy:

function PadRight(const S: string; Width: Integer): string;
begin
  if Length(S) >= Width then
    Result := Copy(S, 1, Width)
  else
    Result := S + StringOfChar(' ', Width - Length(S));
end;
...
writeln(PadRight(BudgeRec.CatName, 15), BudgeRec.Amount:8:2);

Quick tips: string[15] prevents longer storage but does not add trailing spaces, so always apply padding at display time. Use negative width for left alignment (common for names) and a positive width with :n:m for numeric columns. Also guard file operations (check file existence or handle I/O errors) so the display routine isn’t fed an empty or out-of-range array.

I forgot to post the code I have so far. I know this iis ugly but I have two excuses. First there are going to be several modules in the program so I trying to write one module at a time the past the whole thing together. The second reason is I don't know what I'm doing.

program displaycats1;


{$APPTYPE CONSOLE}


uses
SysUtils;


Const
space = ' ';



Type
CatTitle = string[15];
CatType = (Income,Expense);
BudgRec = Record
Category : CatType;
CatName : CatTitle;
Amount : real
end;
BudgArray = array[1..50] of BudgRec;
FileSource = File of BudgRec;
Var
BudgeRec : BudgRec;
Budge : BudgArray;
BudgFile : FileSource;
NumRecs,h : integer;
TotIncome,TotExpense : real;


begin
Assign (BudgFile,'budget.bin');
Reset (BudgFile);
NumRecs := 0;


While not EOF (BudgFile) do
begin
NumRecs := NumRecs + 1;
Read (BudgFile, Budge[NumRecs]);
End;         {while}
Close (BudgFile);
writeln;
TotIncome := 0.0;
h := 0;
writeln ('Income:');


Repeat
h := h + 1;
BudgeRec := Budge[h];
If BudgeRec.Category = Income Then
Begin
writeln (Space:5,BudgeRec.Catname,space,BudgeRec.Amount:8:2);
TotIncome := TotIncome + BudgeRec.Amount;
End;
Until h = NumRecs;
writeln (Space:20,'-----------');
writeln (Space:5,'Total Income', space:5,TotIncome);
writeln;
readln
end.

You can use the Copy function like this.

program TruncateString;

{$APPTYPE CONSOLE}

uses
   SysUtils;

var
   Input  : String;
   Output : String;
begin
   writeln;
   write( 'Enter a string more than three characters long: ' );
   readln( Input );

   Output := Copy( Input, 1, 3 );
   writeln;
   writeln( Output );
   writeln;
end.

**** Output ****

Enter a string more than three characters long: February

Feb
Be a part of the DaniWeb community

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