0

Ok i wrote this function to print as many characters as needed but it doesnt work can anyone help me? It only fails in loops where after every cycle the number of times gets multiplied.

Function TfrmDeliveries.WriteChar (letter : char; times : Integer): String;
var
i_count : Integer;
s_temp : string;
begin
  s_temp := '';

   for i_count := 0 to times - 1 do
   begin
     s_temp := s_temp + letter ;   //Function used to write a char many times
   end;                            //it takes in 2 parameters it adds the letter

   result := s_temp;                                //to the string as many times as needed
end;
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Ionelul
1

Listen this little pascal program :D

program write_chars;

var main_st:string;


function characters(letter:char; times:integer):string;
var i_count:integer;
    s_temp:string;
begin
  s_temp:='';
  for i_count:=0 to times do begin
      s_temp:=s_temp+letter;
  end;
  characters:=s_temp; (*right reference!!*)
end;(*characters*)

begin

  writeln(characters('@',10)); (*call it within writeln*)

  main_st:=characters('$',4);   (*main_st's value is: $$$$*)

end.
(*created by FlamingClaw.2010.04.15.Hungary.*)
0

I don't see any error in your code, but try change something:
- assign the times' value to a local variable
- replace that 'for' with a 'while'
- instead of this function, use Delphi's build-in function DupeString( from StrUtils) - it has the same signature as yours.

Cheers,
Ionut

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.