2

I am new in pascal I have a program which gives result .... I need to pass command line input in in given variable ip1 and ip2. Its can be achieved by ParamStr[1] but it doesn't work.

  program main;
      var 
        output : integer;
      var 
        ip1 : integer;
      var 
        ip2 : integer;
    
    function add(input1,input2:integer) : integer;
       var
       result: integer;
    begin
       if (input1 > input2) then
          result := input1
       else
          result := input2;
       add := result;
    end;
    
    begin
      ip1 := 2533;**{ command line input}**
      ip2 := 555;**{ command line input}**
    
      output := add(ip1,ip2);
      writeln( ' output : ', output );
    end.K
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nirbhav Gupta
  • 106
  • 2
  • 9
  • 1
    What exactly do you mean by `its not works`? Are you having trouble converting the ParamStr values to integer, or what? – 500 - Internal Server Error Sep 17 '14 at 13:23
  • Your function `add` should be called `max` because it does not add the arguments but returns the maximum. – gammatester Sep 17 '14 at 13:45
  • Hello all, Its Not matter what my function name my problem is how can i pass ip1 and ip2 value using command line. is there any function like args or argv in C or something else.... .pls help... – Nirbhav Gupta Sep 18 '14 at 06:04

2 Answers2

3

As the other answer says, you use ParamCount and ParamStr to access command line parameters.

ParamCount returns the number of parameters passed on the command line, so you should check it first to see if you've received enough information.

ParamStr allows you to access each of the parameters passed. ParamStr(0) always gives you the full name of the executing program (including the path). Additional parameters are retrieved using the ordinal order in which they were passed, with ParamStr(1) being the first, and ParamStr(ParamCount) being the last. Each value received using ParamStr is a string value, and therefore has to be converted into the appropriate type before you can use it.

Here's a working example (pretty trivial, and all error checking omitted - you should, for instance, protect the code using StrToInt to handle errors if something is provided that won't convert to an integer).

program TestParams;

uses
  SysUtils;

var
  Input1, Input2, Output: Integer;

begin
  if ParamCount > 1 then
  begin
    Input1 := StrToInt(ParamStr(1));
    Input2 := StrToInt(ParamStr(2));
    Output := Input1 + Input2;
    WriteLn(Format('%d + %d = %d', [Input1, Input2, Output]));
  end
  else
  begin
    WriteLn('Syntax: ', ParamStr(0));  { Just to demonstrate ParamStr(0) }
    WriteLn('There are two parameters required. You provided ', ParamCount);
  end;
  WriteLn('Press ENTER to exit...');
  ReadLn;
end.

Calling it without parameters (or only one) displays the following:

C:\Temp>TestParams
Syntax: C:\Temp\TestParams.exe
There are two parameters required. You provided 0
Press ENTER to exit...

C:\Temp>TestParams 2
Syntax: C:\Temp>TestParams.exe 2
There are two parameters required. You provided 1
Press ENTER to exit...

Calling it with two parameters displays

C:\Temp\TestParams 2 2
2 + 2 = 4
Press ENTER to exit...
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Facing this error...!!! Free Pascal Compiler version 2.6.2 [2013/02/16] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others main.pas(22,22) Error: Identifier not found "StrToInt" main.pas(23,22) Error: Identifier not found "StrToInt" main.pas(30) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled). Is there any pascal version error....? – Nirbhav Gupta Sep 22 '14 at 07:24
  • You need to add `SysUtils` to your uses clause, just like mine does. – Ken White Sep 22 '14 at 12:40
  • 1
    I'd certainly like to hear from the downvoter what the reason was for doing so, so I can improve my answer by addressing the deficiency. – Ken White Sep 25 '14 at 22:28
1

You need to understand the difference between a string and an integer.

To convert betwwen an integer such as 123 and the string of characters 1 2 3 you need to use a function. strtoint is one such function, converting a string to an integer. inttostr is another, converting from an integer to a string.

The command-line data is supplied via paramstr(n) as a string.

intvar := strtoint(paramstr(n));

assigns the value of the string to the integer variable intvar.

Whereas writeln has facilities to convert an integer argument to a formatted string, the way you have used it is attempting to output a string, so you need to convert the integer output to a string.

writeln(' output : ', inttostr(output) );

should do that very nicely.


var
  x : string;
  pcnt : integer;

begin
  writeln('Parameter count=',inttostr(paramcount));
  for pcnt := 1 to paramcount do
    writeln('Parameter ',pcnt, ' of ',paramcount,' = ',paramstr(pcnt));
  readln(x);
end.

should display the parameter list.

Indeed it is the case that the writeln procedure will recognise the variable type and take steps to format the value appropriately as a string, as has arrogantly been pointed out.

The issue to me is the difference between a string and an integer. paramstr returns a string which must be converted to an integer. After more than forty years' experience in Pascal, it's my opinion that it's better for a beginner to go through the exercise of converting each way and then use the conversion facility inbuilt in writeln.

First walk, then run. You first need to understand the steps in the procedure. Then you can start using the shortcuts - once you've mastered the basics.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    Can you give me example how can use ParamStr and paramCount using command line in above mention example....its very helpful. – Nirbhav Gupta Sep 18 '14 at 06:08