Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unexpected output with Pascal

    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.

    Code:
    writeln('Please enter your date in the format xx/yy/zz');
    readln(date);
    day:= copy(date, 1, 2);
    month:= copy(date, 4, 5);
    year:= copy(date, 7, 8);
    writeln(day);
    writeln(month);
    writeln(year);
    readln();
    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.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    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:
    Code:
    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:
    Code:
    date = parse('/', date, 2);
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh a silly mistake!

    Thank you for correcting this for me, much appreicated!

    Ben


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •