Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
A procedure that won't work Posted by DirtyK on 5 Jan 2005 at 4:58 AM
I need to write a procedure:
procedure HextoDec
(const Hexstr: string; var DecOut : integer);
I need to write this without using 'read' or 'write' within it.

I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers

procedure HexToDec (const hexstr: string ; DecOut: integer);

        const
            HexValues : string [16] ='0123456789ABCDEF';

        var
          i : integer;


begin
  DecOut := 0;
    case Length(Hexstr) of
      0: DecOut := 0;
       1..8: for i:=1 to Length(Hexstr) do
        DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
     else for i:=1 to 8 do
      DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;


  end;
end;

Report
Re: A procedure that won't work Posted by zibadian on 5 Jan 2005 at 5:15 AM
: I need to write a procedure:
: procedure HextoDec
: (const Hexstr: string; var DecOut : integer);
: I need to write this without using 'read' or 'write' within it.
:
: I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
:
:
: procedure HexToDec (const hexstr: string ; DecOut: integer);
: 
:         const
:             HexValues : string [16] ='0123456789ABCDEF';
: 
:         var
:           i : integer;
: 
: 
: begin
:   DecOut := 0;
:     case Length(Hexstr) of
:       0: DecOut := 0;
:        1..8: for i:=1 to Length(Hexstr) do
:         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
:      else for i:=1 to 8 do
:       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: 
: 
:   end;
: end;
: 

:
Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
Report
Re: A procedure that won't work Posted by DirtyK on 5 Jan 2005 at 1:18 PM
: : I need to write a procedure:
: : procedure HextoDec
: : (const Hexstr: string; var DecOut : integer);
: : I need to write this without using 'read' or 'write' within it.
: :
: : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: :
: :
: : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : 
: :         const
: :             HexValues : string [16] ='0123456789ABCDEF';
: : 
: :         var
: :           i : integer;
: : 
: : 
: : begin
: :   DecOut := 0;
: :     case Length(Hexstr) of
: :       0: DecOut := 0;
: :        1..8: for i:=1 to Length(Hexstr) do
: :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: :      else for i:=1 to 8 do
: :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : 
: : 
: :   end;
: : end;
: : 

: :
: Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.

Right, changed that...
Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
cheers
:

Report
Re: A procedure that won't work Posted by zibadian on 5 Jan 2005 at 2:58 PM
: : : I need to write a procedure:
: : : procedure HextoDec
: : : (const Hexstr: string; var DecOut : integer);
: : : I need to write this without using 'read' or 'write' within it.
: : :
: : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : :
: : :
: : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : 
: : :         const
: : :             HexValues : string [16] ='0123456789ABCDEF';
: : : 
: : :         var
: : :           i : integer;
: : : 
: : : 
: : : begin
: : :   DecOut := 0;
: : :     case Length(Hexstr) of
: : :       0: DecOut := 0;
: : :        1..8: for i:=1 to Length(Hexstr) do
: : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : :      else for i:=1 to 8 do
: : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : 
: : : 
: : :   end;
: : : end;
: : : 

: : :
: : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
:
: Right, changed that...
: Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: cheers
: :
:
:
The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
Report
Re: A procedure that won't work Posted by DirtyK on 6 Jan 2005 at 3:10 AM
: : : : I need to write a procedure:
: : : : procedure HextoDec
: : : : (const Hexstr: string; var DecOut : integer);
: : : : I need to write this without using 'read' or 'write' within it.
: : : :
: : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : :
: : : :
: : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : 
: : : :         const
: : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : 
: : : :         var
: : : :           i : integer;
: : : : 
: : : : 
: : : : begin
: : : :   DecOut := 0;
: : : :     case Length(Hexstr) of
: : : :       0: DecOut := 0;
: : : :        1..8: for i:=1 to Length(Hexstr) do
: : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : :      else for i:=1 to 8 do
: : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : 
: : : : 
: : : :   end;
: : : : end;
: : : : 

: : : :
: : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: :
: : Right, changed that...
: : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : cheers
: : :
: :
: :
: The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.

Sorry I meant to put the code in with the last message.... here you go...
uses
  SysUtils;
  var
  user: string;
  prop : integer;



procedure HexToDec (const hexstr: string ;var DecOut: integer);

        const
            HexValues : string [16] ='0123456789ABCDEF';

        var
          i : integer;


begin
  DecOut := 0;
    case Length(Hexstr) of
      0: DecOut := 0;
       1..8: for i:=1 to Length(Hexstr) do
        DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
     else for i:=1 to 8 do
      DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;


  end;
end;
 begin
  writeln ('Please enter the Hex number you wish to convert...');
   readln (user);
    begin
    HexToDec(User);

   readln (prop) ;
end;
end.

:

Report
Re: A procedure that won't work Posted by zibadian on 6 Jan 2005 at 3:20 AM
: : : : : I need to write a procedure:
: : : : : procedure HextoDec
: : : : : (const Hexstr: string; var DecOut : integer);
: : : : : I need to write this without using 'read' or 'write' within it.
: : : : :
: : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : :
: : : : :
: : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : 
: : : : :         const
: : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : 
: : : : :         var
: : : : :           i : integer;
: : : : : 
: : : : : 
: : : : : begin
: : : : :   DecOut := 0;
: : : : :     case Length(Hexstr) of
: : : : :       0: DecOut := 0;
: : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : :      else for i:=1 to 8 do
: : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : 
: : : : : 
: : : : :   end;
: : : : : end;
: : : : : 

: : : : :
: : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : :
: : : Right, changed that...
: : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : cheers
: : : :
: : :
: : :
: : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
:
: Sorry I meant to put the code in with the last message.... here you go...
:
: uses
:   SysUtils;
:   var
:   user: string;
:   prop : integer;
: 
: 
: 
: procedure HexToDec (const hexstr: string ;var DecOut: integer);
: 
:         const
:             HexValues : string [16] ='0123456789ABCDEF';
: 
:         var
:           i : integer;
: 
: 
: begin
:   DecOut := 0;
:     case Length(Hexstr) of
:       0: DecOut := 0;
:        1..8: for i:=1 to Length(Hexstr) do
:         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
:      else for i:=1 to 8 do
:       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: 
: 
:   end;
: end;
:  begin
:   writeln ('Please enter the Hex number you wish to convert...');
:    readln (user);
:     begin
:     HexToDec(User);
: 
:    readln (prop) ;
: end;
: end.
: 

: :
:
:
You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
Report
Re: A procedure that won't work Posted by DirtyK on 6 Jan 2005 at 6:30 AM
: : : : : : I need to write a procedure:
: : : : : : procedure HextoDec
: : : : : : (const Hexstr: string; var DecOut : integer);
: : : : : : I need to write this without using 'read' or 'write' within it.
: : : : : :
: : : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : : :
: : : : : :
: : : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : : 
: : : : : :         const
: : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : 
: : : : : :         var
: : : : : :           i : integer;
: : : : : : 
: : : : : : 
: : : : : : begin
: : : : : :   DecOut := 0;
: : : : : :     case Length(Hexstr) of
: : : : : :       0: DecOut := 0;
: : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : :      else for i:=1 to 8 do
: : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : 
: : : : : : 
: : : : : :   end;
: : : : : : end;
: : : : : : 

: : : : : :
: : : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : : :
: : : : Right, changed that...
: : : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : : cheers
: : : : :
: : : :
: : : :
: : : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
: :
: : Sorry I meant to put the code in with the last message.... here you go...
: :
: : uses
: :   SysUtils;
: :   var
: :   user: string;
: :   prop : integer;
: : 
: : 
: : 
: : procedure HexToDec (const hexstr: string ;var DecOut: integer);
: : 
: :         const
: :             HexValues : string [16] ='0123456789ABCDEF';
: : 
: :         var
: :           i : integer;
: : 
: : 
: : begin
: :   DecOut := 0;
: :     case Length(Hexstr) of
: :       0: DecOut := 0;
: :        1..8: for i:=1 to Length(Hexstr) do
: :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: :      else for i:=1 to 8 do
: :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : 
: : 
: :   end;
: : end;
: :  begin
: :   writeln ('Please enter the Hex number you wish to convert...');
: :    readln (user);
: :     begin
: :     HexToDec(User);
: : 
: :    readln (prop) ;
: : end;
: : end.
: : 

: : :
: :
: :
: You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
:
I've added a writeln after calling the procedure... is this what you meant by writing it to screen? but i don't understand which integer parameter i have forgotten or where to put it...?
Sorry...
Report
Re: A procedure that won't work Posted by zibadian on 7 Jan 2005 at 5:07 AM
: : : : : : : I need to write a procedure:
: : : : : : : procedure HextoDec
: : : : : : : (const Hexstr: string; var DecOut : integer);
: : : : : : : I need to write this without using 'read' or 'write' within it.
: : : : : : :
: : : : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : : : :
: : : : : : :
: : : : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : : : 
: : : : : : :         const
: : : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : : 
: : : : : : :         var
: : : : : : :           i : integer;
: : : : : : : 
: : : : : : : 
: : : : : : : begin
: : : : : : :   DecOut := 0;
: : : : : : :     case Length(Hexstr) of
: : : : : : :       0: DecOut := 0;
: : : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : :      else for i:=1 to 8 do
: : : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : 
: : : : : : : 
: : : : : : :   end;
: : : : : : : end;
: : : : : : : 

: : : : : : :
: : : : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : : : :
: : : : : Right, changed that...
: : : : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : : : cheers
: : : : : :
: : : : :
: : : : :
: : : : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
: : :
: : : Sorry I meant to put the code in with the last message.... here you go...
: : :
: : : uses
: : :   SysUtils;
: : :   var
: : :   user: string;
: : :   prop : integer;
: : : 
: : : 
: : : 
: : : procedure HexToDec (const hexstr: string ;var DecOut: integer);
: : : 
: : :         const
: : :             HexValues : string [16] ='0123456789ABCDEF';
: : : 
: : :         var
: : :           i : integer;
: : : 
: : : 
: : : begin
: : :   DecOut := 0;
: : :     case Length(Hexstr) of
: : :       0: DecOut := 0;
: : :        1..8: for i:=1 to Length(Hexstr) do
: : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : :      else for i:=1 to 8 do
: : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : 
: : : 
: : :   end;
: : : end;
: : :  begin
: : :   writeln ('Please enter the Hex number you wish to convert...');
: : :    readln (user);
: : :     begin
: : :     HexToDec(User);
: : : 
: : :    readln (prop) ;
: : : end;
: : : end.
: : : 

: : : :
: : :
: : :
: : You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
: :
: I've added a writeln after calling the procedure... is this what you meant by writing it to screen? but i don't understand which integer parameter i have forgotten or where to put it...?
: Sorry...
:
If you look at the procedure declaration (red in the code), you will see 2 parameters: hexstr and DecOut. In your procedure call (blue) there is only 1: user. This means that the code doesn't compile. And if it does, it does not know what to do with the DecOut parameter. The call code must include more code to tell the computer what to do with the DecOut value. In other words, you need to add a second parameter to the calling code, which is the variable, into which the result of the conversion needs to be stored.
Report
Re: A procedure that won't work Posted by DirtyK on 7 Jan 2005 at 5:16 AM
: : : : : : : : I need to write a procedure:
: : : : : : : : procedure HextoDec
: : : : : : : : (const Hexstr: string; var DecOut : integer);
: : : : : : : : I need to write this without using 'read' or 'write' within it.
: : : : : : : :
: : : : : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : : : : :
: : : : : : : :
: : : : : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : : : : 
: : : : : : : :         const
: : : : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : : : 
: : : : : : : :         var
: : : : : : : :           i : integer;
: : : : : : : : 
: : : : : : : : 
: : : : : : : : begin
: : : : : : : :   DecOut := 0;
: : : : : : : :     case Length(Hexstr) of
: : : : : : : :       0: DecOut := 0;
: : : : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : :      else for i:=1 to 8 do
: : : : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : : 
: : : : : : : : 
: : : : : : : :   end;
: : : : : : : : end;
: : : : : : : : 

: : : : : : : :
: : : : : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : : : : :
: : : : : : Right, changed that...
: : : : : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : : : : cheers
: : : : : : :
: : : : : :
: : : : : :
: : : : : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
: : : :
: : : : Sorry I meant to put the code in with the last message.... here you go...
: : : :
: : : : uses
: : : :   SysUtils;
: : : :   var
: : : :   user: string;
: : : :   prop : integer;
: : : : 
: : : : 
: : : : 
: : : : procedure HexToDec (const hexstr: string ;var DecOut: integer);
: : : : 
: : : :         const
: : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : 
: : : :         var
: : : :           i : integer;
: : : : 
: : : : 
: : : : begin
: : : :   DecOut := 0;
: : : :     case Length(Hexstr) of
: : : :       0: DecOut := 0;
: : : :        1..8: for i:=1 to Length(Hexstr) do
: : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : :      else for i:=1 to 8 do
: : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : 
: : : : 
: : : :   end;
: : : : end;
: : : :  begin
: : : :   writeln ('Please enter the Hex number you wish to convert...');
: : : :    readln (user);
: : : :     begin
: : : :     HexToDec(User);
: : : : 
: : : :    readln (prop) ;
: : : : end;
: : : : end.
: : : : 

: : : : :
: : : :
: : : :
: : : You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
: : :
: : I've added a writeln after calling the procedure... is this what you meant by writing it to screen? but i don't understand which integer parameter i have forgotten or where to put it...?
: : Sorry...
: :
: If you look at the procedure declaration (red in the code), you will see 2 parameters: hexstr and DecOut. In your procedure call (blue) there is only 1: user. This means that the code doesn't compile. And if it does, it does not know what to do with the DecOut parameter. The call code must include more code to tell the computer what to do with the DecOut value. In other words, you need to add a second parameter to the calling code, which is the variable, into which the result of the conversion needs to be stored.
:
Excellent working now!!!! Thank-you!!!

Report
Re: A procedure that won't work Posted by DirtyK on 7 Jan 2005 at 5:24 AM
: : : : : : : : : I need to write a procedure:
: : : : : : : : : procedure HextoDec
: : : : : : : : : (const Hexstr: string; var DecOut : integer);
: : : : : : : : : I need to write this without using 'read' or 'write' within it.
: : : : : : : : :
: : : : : : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : : : : : :
: : : : : : : : :
: : : : : : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : : : : : 
: : : : : : : : :         const
: : : : : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : : : : 
: : : : : : : : :         var
: : : : : : : : :           i : integer;
: : : : : : : : : 
: : : : : : : : : 
: : : : : : : : : begin
: : : : : : : : :   DecOut := 0;
: : : : : : : : :     case Length(Hexstr) of
: : : : : : : : :       0: DecOut := 0;
: : : : : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : : :      else for i:=1 to 8 do
: : : : : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : : : 
: : : : : : : : : 
: : : : : : : : :   end;
: : : : : : : : : end;
: : : : : : : : : 

: : : : : : : : :
: : : : : : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : : : : : :
: : : : : : : Right, changed that...
: : : : : : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : : : : : cheers
: : : : : : : :
: : : : : : :
: : : : : : :
: : : : : : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
: : : : :
: : : : : Sorry I meant to put the code in with the last message.... here you go...
: : : : :
: : : : : uses
: : : : :   SysUtils;
: : : : :   var
: : : : :   user: string;
: : : : :   prop : integer;
: : : : : 
: : : : : 
: : : : : 
: : : : : procedure HexToDec (const hexstr: string ;var DecOut: integer);
: : : : : 
: : : : :         const
: : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : 
: : : : :         var
: : : : :           i : integer;
: : : : : 
: : : : : 
: : : : : begin
: : : : :   DecOut := 0;
: : : : :     case Length(Hexstr) of
: : : : :       0: DecOut := 0;
: : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : :      else for i:=1 to 8 do
: : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : 
: : : : : 
: : : : :   end;
: : : : : end;
: : : : :  begin
: : : : :   writeln ('Please enter the Hex number you wish to convert...');
: : : : :    readln (user);
: : : : :     begin
: : : : :     HexToDec(User);
: : : : : 
: : : : :    readln (prop) ;
: : : : : end;
: : : : : end.
: : : : : 

: : : : : :
: : : : :
: : : : :
: : : : You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
: : : :
: : : I've added a writeln after calling the procedure... is this what you meant by writing it to screen? but i don't understand which integer parameter i have forgotten or where to put it...?
: : : Sorry...
: : :
: : If you look at the procedure declaration (red in the code), you will see 2 parameters: hexstr and DecOut. In your procedure call (blue) there is only 1: user. This means that the code doesn't compile. And if it does, it does not know what to do with the DecOut parameter. The call code must include more code to tell the computer what to do with the DecOut value. In other words, you need to add a second parameter to the calling code, which is the variable, into which the result of the conversion needs to be stored.
: :
: Excellent working now!!!! Thank-you!!!

Any chance you could tell me how come this procedure doesn't work when it's pretty much in the same format?

uses
  SysUtils;
 var
 user, prop : integer;
 result : string;
  procedure DecToHex (Const Number : integer; var Hexstr : string);

  const
    HexValue : string [16] = '0123456789ABCDEF';

  var

    remainder : integer;

  begin
    HexStr := '';
  while Number > 0 do
  begin
  Remainder := Number mod 16;
  HexStr := HexValue[Remainder + 1] + HexStr;
  Number := Number div 16 ;



end;
end;
begin
  writeln (' please enter number');
  readln (user);
  DecToHex (user, result);
  writeln(result);
readln (prop);
end.
: 

:

Report
Re: A procedure that won't work Posted by zibadian on 7 Jan 2005 at 6:32 AM
: : : : : : : : : : I need to write a procedure:
: : : : : : : : : : procedure HextoDec
: : : : : : : : : : (const Hexstr: string; var DecOut : integer);
: : : : : : : : : : I need to write this without using 'read' or 'write' within it.
: : : : : : : : : :
: : : : : : : : : : I have earlier written a function which canverts from Hex to Dec successfully and have been trying to swap bits of it around so that it will work as this procedure but to know avail... I've included where I have got so far and would be very grateful if anyone could offer me some advice... Cheers
: : : : : : : : : :
: : : : : : : : : :
: : : : : : : : : : procedure HexToDec (const hexstr: string ; DecOut: integer);
: : : : : : : : : : 
: : : : : : : : : :         const
: : : : : : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : : : : : 
: : : : : : : : : :         var
: : : : : : : : : :           i : integer;
: : : : : : : : : : 
: : : : : : : : : : 
: : : : : : : : : : begin
: : : : : : : : : :   DecOut := 0;
: : : : : : : : : :     case Length(Hexstr) of
: : : : : : : : : :       0: DecOut := 0;
: : : : : : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : : : :      else for i:=1 to 8 do
: : : : : : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : : : : : 
: : : : : : : : : : 
: : : : : : : : : :   end;
: : : : : : : : : : end;
: : : : : : : : : : 

: : : : : : : : : :
: : : : : : : : : Your code does not have a variable DecOut parameter. That is required for the actual variable in the calling code to be changed.
: : : : : : : :
: : : : : : : : Right, changed that...
: : : : : : : : Have put the procedure into a program to test it but can't get it to work... could you give me a clue as to what i'm missing now?
: : : : : : : : cheers
: : : : : : : : :
: : : : : : : :
: : : : : : : :
: : : : : : : The procedure itself works perfectly. Please, post your calling code. Perhaps that's where the error is.
: : : : : :
: : : : : : Sorry I meant to put the code in with the last message.... here you go...
: : : : : :
: : : : : : uses
: : : : : :   SysUtils;
: : : : : :   var
: : : : : :   user: string;
: : : : : :   prop : integer;
: : : : : : 
: : : : : : 
: : : : : : 
: : : : : : procedure HexToDec (const hexstr: string ;var DecOut: integer);
: : : : : : 
: : : : : :         const
: : : : : :             HexValues : string [16] ='0123456789ABCDEF';
: : : : : : 
: : : : : :         var
: : : : : :           i : integer;
: : : : : : 
: : : : : : 
: : : : : : begin
: : : : : :   DecOut := 0;
: : : : : :     case Length(Hexstr) of
: : : : : :       0: DecOut := 0;
: : : : : :        1..8: for i:=1 to Length(Hexstr) do
: : : : : :         DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : :      else for i:=1 to 8 do
: : : : : :       DecOut := 16*DecOut + Pos(Upcase(Hexstr[i]), HexValues)-1;
: : : : : : 
: : : : : : 
: : : : : :   end;
: : : : : : end;
: : : : : :  begin
: : : : : :   writeln ('Please enter the Hex number you wish to convert...');
: : : : : :    readln (user);
: : : : : :     begin
: : : : : :     HexToDec(User);
: : : : : : 
: : : : : :    readln (prop) ;
: : : : : : end;
: : : : : : end.
: : : : : : 

: : : : : : :
: : : : : :
: : : : : :
: : : : : You forgot the integer parameter in the call. Also you don't write the converted value to screen, so you don't see if it worked.
: : : : :
: : : : I've added a writeln after calling the procedure... is this what you meant by writing it to screen? but i don't understand which integer parameter i have forgotten or where to put it...?
: : : : Sorry...
: : : :
: : : If you look at the procedure declaration (red in the code), you will see 2 parameters: hexstr and DecOut. In your procedure call (blue) there is only 1: user. This means that the code doesn't compile. And if it does, it does not know what to do with the DecOut parameter. The call code must include more code to tell the computer what to do with the DecOut value. In other words, you need to add a second parameter to the calling code, which is the variable, into which the result of the conversion needs to be stored.
: : :
: : Excellent working now!!!! Thank-you!!!
:
: Any chance you could tell me how come this procedure doesn't work when it's pretty much in the same format?
:
:
: uses
:   SysUtils;
:  var
:  user, prop : integer;
:  result : string;
:   procedure DecToHex (Const Number : integer; var Hexstr : string);
: 
:   const
:     HexValue : string [16] = '0123456789ABCDEF';
: 
:   var
: 
:     remainder : integer;
: 
:   begin
:     HexStr := '';
:   while Number > 0 do
:   begin
:   Remainder := Number mod 16;
:   HexStr := HexValue[Remainder + 1] + HexStr;
:   Number := Number div 16 ;
: 
: 
: 
: end;
: end;
: begin
:   writeln (' please enter number');
:   readln (user);
:   DecToHex (user, result);
:   writeln(result);
: readln (prop);
: end.
: : 

: :
:
:
According to the procedure declaration, the number parameter is constant. This means that it cannot be changed within the procedure. The red line, however, tries to change it. The simplest solution would be to make the parameter changable, but not variable.
Explanation:
constant parameter: cannot be changed inside the procedure/function. Indicated by placing "const" before the parameter name
variable parameter: if changed, returns the change to the calling variable. Indicated by placing "var" before the parameter name
changable parameter: can be changed, but will not return the change back to the calling variable. Indicated by placing nothing before the parameter name



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.