Board index » delphi » Can anyone tell me what's wrong?

Can anyone tell me what's wrong?

The function below is supposed to seperate Comma seperated values, for
example:

     Name, char
          becomes
    Name

I have been trying to solve this problem for the last 2 days but without
success.

Can anyone help please?

Thank you,

David

Function Filter(Stg:string):String;

Var
  S,st:String;
  I:Integer;
  c:char;

Begin
st:=stg;
S:='';
I:=1;
 Repeat
   c:=St[I];
   if c <> ',' then S:=S+c;
   gotoxy(2,22);
   write(c);
   inc(I);
 until St[I-1]=',';

 readln;
 Filter:=S;

end;

 

Re:Can anyone tell me what's wrong?


Quote
David <da...@fastnet.net.mt> wrote in message

news:38a4a0ee@news.waldonet.ent.mt...
Quote

> The function below is supposed to seperate Comma seperated values, for
> example:

>      Name, char
>           becomes
>     Name

> I have been trying to solve this problem for the last 2 days but without
> success.

> Can anyone help please?

> Thank you,

> David

> Function Filter(Stg:string):String;

> Var
>   S,st:String;
>   I:Integer;
>   c:char;

> Begin
> st:=stg;
> S:='';
> I:=1;
>  Repeat
>    c:=St[I];
>    if c <> ',' then S:=S+c;
>    gotoxy(2,22);
>    write(c);
>    inc(I);
>  until St[I-1]=',';

>  readln;
>  Filter:=S;

> end;

=====================================================

I'm not sure if this is exactly what you are trying to do but try it:

Var
  S,st:String;
  I:Integer;
  c:char;

Begin
st:=stg;
S:='';
I:=1;
 gotoxy(2,22);
 Repeat
   c:=St[I];
 if c <> ',' then
  begin
  write(c);
  S:=S+c;
  end;
   inc(I);
 until St[I-1]=',';

end.

Re:Can anyone tell me what's wrong?


Quote
David wrote:

> The function below is supposed to seperate Comma seperated values, for
> example:

>      Name, char
>           becomes
>     Name

> I have been trying to solve this problem for the last 2 days but without
> success.

> Can anyone help please?

> Thank you,

> David

> Function Filter(Stg:string):String;

> Var
>   S,st:String;
>   I:Integer;
>   c:char;

> Begin
> st:=stg;
> S:='';
> I:=1;
>  Repeat
>    c:=St[I];
>    if c <> ',' then S:=S+c;
>    gotoxy(2,22);
>    write(c);
>    inc(I);
>  until St[I-1]=',';

>  readln;
>  Filter:=S;

> end;

I tested your function with TP5.5 like this:
        something:=filter(Name, char);
The function works as far as "Name, char" becomes "Name". I think you
want to show the whole word "Name" on the screen. If I am right, try
this way:
...
REPEAT
        c:=st[i];
        IF c <> , THEN BEGIN
                        s:=s+c;
                        GOTOXY(1+i,22);
                        WRITE(c);
                        END;
        INC(i);
UNTIL st[i-1]=,;
...
The way I would prefer is this:
...
REPEAT
        c:=st[i];
        IF c <> , THEN s:=s+c;
        INC(i);
UNTIL st[i-1]=,;
GOTOXY(2,22);
WRITE(s);
...
If I am not right, please tell what the exact problem is.

regards

Martin
--
 Martin Stahl
 e-mail: kram...@uplink.de
 http://www.user.uplink.de/krampus

Re:Can anyone tell me what's wrong?


Do you want a function like this one?

function Filter(stg:string):string;
var i:integer;
  begin
    i:=pos(',',stg);
    if i=0 then i:=length(stg)+1;
    Filter:=copy(stg,1,i-1);
  end;

begin
  gotoxy(2,22);
  write(Filter('Name, char'));
end.

Alex
--
** Pepsi Cola and animal protection **
**  http://www.pepsi{*word*76}bath.com/  **

Author of the free Chatsystem PINO! || Website: http://www.alcomp.net
Available at http://pino.cjb.net    || Chat   : pinochat.dhs.org:8080

Re:Can anyone tell me what's wrong?


Quote
On Sat, 12 Feb 2000 01:01:22 +0100, David wrote:
>  Repeat
>    c:=St[I];
>    if c <> ',' then S:=S+c;
>    gotoxy(2,22);
>    write(c);
>    inc(I);
>  until St[I-1]=',';

Since you put the gotoxy() within the loop, you're returning to the exact
same position before printing each character, and each one just overwrites
the one before it.

--
William McBrine * wmcbr...@clark.net * http://www.clark.net/~wmcbrine/

Other Threads