PDA

View Full Version : Unexpected output with Pascal

erjorgito
11-16-2011, 12:17 AM
Hello, im currently teaching myself Pascal and C++ and am going over a few off the problems people have left over the internet for pascal. The idea of this program is too take a date in the format xx/yy/zz and put in into a format like this, 16th October 1953. Before i even reached the harder part, i can't see the make this weird output go away and work out whats causing it, so decieded to post here for some help.

day:= copy(date, 1, 2);
month:= copy(date, 4, 5);
year:= copy(date, 7, 8);
writeln(day);
writeln(month);
writeln(year);

the line (month:= copy(date, 4, 5) which should output yy, is outputting yy/zz, and i cannot work out why.

Any help is good, thank you.

tangoforce
11-16-2011, 08:26 PM
The 3rd parameter is the LENGTH of the string you want to copy so you're actually telling it to start at the 4ths character and 5 characters from that point. Thats how I remember it anyway.

You might want to look at using this function though:

function Parse(Char, Str: string; Count: Integer): string;
//Parse a string to obtain a particular section of it
var
I: Integer;
T: string;
begin
//If Str is blank
If Str = '' then
begin
//Result is also blank (Nothing to parse)
Result := '';
//Get out of here
exit;
end;

//If Last character of Str is not Char ('This,is,a,string')
if Str[Length(Str)] <> Char then
begin
//Str is Str + Char ('This,is,a,string,')
Str := Str + Char;
end;

{Count could be section 1 so we start loop at 1 NOT zero
Start from 1 to Count (3 for example: the word 'a')}
for I := 1 to Count do
begin
{T is Str[1] to 1st Char (',')-1 in Str PLUS Char
EG T := '' NOW T := 'This,' THEN T := 'This,is,' etc}
T := Copy(Str, 1, Pos(Char, Str) -1);

{For next loop, we need next section of Str ('the') for above
Str now Length from 1st Char (',')+1 EG 'the,string,'
Now loop again}

//Think the use of length() might be wrong but its always worked!
Str := Copy(Str, Pos(Char, Str) +1, Length(Str));
end;

//Result is now T
Result := T;
end;

To use it in your scenario:

date = parse('/', date, 2);

erjorgito
11-17-2011, 12:07 PM
Oh a silly mistake!

Thank you for correcting this for me, much appreicated!

Ben