We help IT Professionals succeed at work.

Pascal Helo

seraph_matrix_631
seraph_matrix_631 used Ask the Experts™
on
I am new to pascal and i have been asked to make a program that willa llow the user to type the amount of miles they have driven.
then to take this number and multiply it by 0.20  (they get 20p per mile driven) then output the information to the user as:


you have driven  xxx miles
your travelling allowance is £xx.xx

press any key to continue...



this is what i ahve done so far. please bear in mind i am a total beginner so comments in the code would be excellent so i understand it and so i can apply the knowledge to other programs.




program TASK1(INPUT,OUTPUT);
USES CRT;

var      miles              :integer;
         pay            :integer;
         allowance            :INTEGER;

begin

pay:=0.20;

     WRITELN ('ENTER NUMBER OF MILES DRIVEN:  ');
     READ(miles); {take input from user}
     WRITELN(' ');


     WRITELN(miles , '*', pay , '= ', allowance );
     WRITE('PRESS ANY KEY TO CONTINUE');

     READKEY;  {THIS WAITS FOR YOU TO PRESS A KEY}

end.



thank you in advance.
the compiler says there are 3 errors:

1. 17 / 50 assignment1.pas
 Warning: Variable ALLOWANCE does not seem to be initialized

2. 10 / 6 assignment1.pas
 Error: Incompatible types: got "S80REAL" expected "LONGINT"

3. 23 assignment1.pas
 Fatal: There were 1 errors compiling module, stopping



from mr skulls
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
updating the point allocation as this is something i need to sort out quickly.
thank you.
make these changes:

var     miles               :integer;
         pay                  :REAL;
         allowance         :REAL;

WRITELN ('ENTER NUMBER OF MILES DRIVEN:  ');
     READ(miles); {take input from user}
     WRITELN(' ');

     allowance:= miles * pay;
     WRITELN('Allowance = ' + allowance);
     WRITE('PRESS ANY KEY TO CONTINUE');


AW
<note>

    If you need it 'quickly' then offer a bit more than 50 points

</note>

Author

Commented:
Arthur_wood thank you for your help. i was wondering if you would mind debugging this code. i wil of course up the point allocation.

the user prompts must remain the same but instead of allowing the user to type in their mileage and have their allowance dislayed i am trying to make a system whereby one person can do it.

the program should be able to get input from the user (how many records do you want to add?) then get it to loop this many times. the code should be easy to follow. i have not done anything this complex but am trying to understand the programming language.

====
1. the user add the records to process
2. this number is stored.
3. the "empnum is set to one for the first employee. (enter details for employee 1: )
4. the user selects what method of travel was used (different cost per option)
5. the information is output as follows:


Employee 1 uses motorcycle
their week's mileage is xxx miles
their travelling allowance is £xx.xx

press any key to continue...
[clear screen using   CLRSCR;]

[[shows menu]]
"Enter transport for employee 2: "
please enter mileage for employee 2: "

Same output as above until you reach the total record to be entered. (set by user input)


=======================================================
CODE START
=======================================================

PROGRAM USERINPUTRECORDS (INPUT,OUTPUT);
USES CRT;

LABEL RETURN;

CONST
   ENDMARKER=-1;

VAR      
   TOTALREC      :INTEGER;
   MILES                      :INTEGER;
   ALLOWANCE      :REAL;
   SELECTION      :INTEGER;
   EMPNUM                :INTEGER;


BEGIN
   EMPNUM=1;
   CLRSCR;

   WRITELN('HOW MANY EMPLOYEES DO YOU WISH TO PROCESS? ');
   READLN(TOTALREC)
   CLRSCR;

        REPEAT      

   WRITELN('1     MOTORCYCLE');
   WRITELN('2     CAR WITH BELOW 1500 CC CAPACITY');
   WRITELN('3     CAR WITH MORE THAN 1500 CC CAPACITY');
   WRITELN(' ');
  WRITELN('PLEASE ENTER TRANSPORT FOR EMPLOYEE ',EMPNUM: ');
               
   
   If (selection)=1 Then

      BEGIN
      writeln('Please Enter your mileage :> ');
      readLn(M);
               
      ALLOWANCE:=M * 0.15;
               
      WriteLn('You have selected Motorcycle as your means of transport');
      writeLn('Your weeks mileage is ,'M);
      WriteLn('Your travelling allowance is ,' A);
      WriteLN(' ');
      WriteLN('Press Any Key To Continue...');
                                    
      Delay(2000);
      Goto Return;
      End;



      if (selection)=2 then
      BEGIN
      writein('Please Enter your mileage :> ');
      readLn(M);
                 
      allowance:=M * 0.20;

               
      WriteLn('You have selected a car with below 1500 cc capacity as your means of transport');
      writeLn('Your weeks mileage is ,'M);
      WriteLn('Your travelling allowance is ,'A);
      WriteLN(' ');
      WriteLN('Press Any Key To Continue...');
                                    
      Delay(2000);
      Goto Return;
      End;


      if (selection)=3
      Begin
      writein('Please Enter your mileage :> ');
      readLn(M);
                 
      allowance:=M * 0.30;

          
      WriteLn('You have selected a car with more than 1500 cc capacity as your means of transport');
      writeLn('Your weeks mileage is ,'M);
      WriteLn('Your travelling allowance is ,'A);
      WriteLN(' ');
      WriteLN('Press Any Key To Continue...');
            
      Delay(2000);
      End;
     READKEY;
       
           
       EMPNUM=EMPNUM+1
      UNTIL (EMPNUM=TOTALREC);
END.


=========================================================
CODE END
=========================================================



Answer needed to this fairly quickly. so will up the pont allocation.
thank you in advance!

Mr Skulls.



=======================================================
CODE START
=======================================================

PROGRAM USERINPUTRECORDS (INPUT,OUTPUT);
USES CRT;

CONST
   ENDMARKER=-1;

VAR    
   TOTALREC     :INTEGER;
   MILES                     :INTEGER;
   ALLOWANCE     :REAL;
   SELECTION     :INTEGER;
   EMPNUM                :INTEGER;


BEGIN
   EMPNUM=1;
   CLRSCR;

   WRITELN('HOW MANY EMPLOYEES DO YOU WISH TO PROCESS? ');
   READLN(TOTALREC)
   CLRSCR;

       REPEAT    

   WRITELN('1     MOTORCYCLE');
   WRITELN('2     CAR WITH BELOW 1500 CC CAPACITY');
   WRITELN('3     CAR WITH MORE THAN 1500 CC CAPACITY');
   WRITELN(' ');
  WRITELN('PLEASE ENTER TRANSPORT FOR EMPLOYEE ',EMPNUM: ');
               
   
   If (selection)=1 Then

     BEGIN
     writeln('Please Enter your mileage :> ');
     readLn(M);
               
     ALLOWANCE:=M * 0.15;
             
     WriteLn('You have selected Motorcycle as your means of transport');
                               
     Goto Return;
     End;



     if (selection)=2 then
     BEGIN
     writein('Please Enter your mileage :> ');
     readLn(M);
                 
     allowance:=M * 0.20;

             
     WriteLn('You have selected a car with below 1500 cc capacity as your means of transport');
     Goto Return;
     End;


     if (selection)=3
     Begin
     writein('Please Enter your mileage :> ');
     readLn(M);
     WriteLn('You have selected a car with more than 1500 cc capacity as your means of transport');        
     allowance:=M * 0.30;
End;
  LABEL RETURN;          
     
     writeLn('Your weeks mileage is ,'M);
     WriteLn('Your travelling allowance is ,'ALLOWANCE);
     WriteLN(' ');
     WriteLN('Press Any Key To Continue...');
         
     Delay(2000);
     



     READKEY;
       
           
      EMPNUM=EMPNUM+1
     UNTIL (EMPNUM=TOTALREC);
END.


=========================================================
CODE END
=========================================================

AW
you reaaly should have posted this question in the PASCAL TA.  I am NOT a pascal developer.  I use VB6 and VB.NET, professionally, and this has been a best guess, from what little I recall from Pascal (which I used about 25 years ago.)

You might post a 'pointer' (20 points max) question in the PASCAL TA, point back to this question, and then whatever PASCAL developers there might be, might offer you some assistance.

AW
HonorGodSoftware Engineer
Commented:
1. You have some syntax errors in your code.
---------------------------------------------------------------------------------------------------------------------------------------------
PROGRAM USERINPUTRECORDS (INPUT,OUTPUT);
USES CRT;

LABEL RETURN;    /* << Unnecessary */

CONST
   ENDMARKER = -1;

VAR    
   TOTALREC  : INTEGER;
   MILES     : INTEGER;
   ALLOWANCE : REAL;
   SELECTION : INTEGER;
   EMPNUM    : INTEGER;


BEGIN
   EMPNUM = 1;
   CLRSCR;

   WRITELN( 'HOW MANY EMPLOYEES DO YOU WISH TO PROCESS? ' );
   READLN( TOTALREC )               /*       << Missing ';' */
   CLRSCR;

   REPEAT    

      WRITELN( '1     MOTORCYCLE' );
      WRITELN( '2     CAR WITH BELOW 1500 CC CAPACITY' );
      WRITELN( '3     CAR WITH MORE THAN 1500 CC CAPACITY' );
      WRITELN( ' ' );
      WRITELN( 'PLEASE ENTER TRANSPORT FOR EMPLOYEE ', EMPNUM: ');
/* ***************************************************         ^== extra ' */
   
      If (selection)=1 Then
/*                 ^ in wrong place! */
        BEGIN
          writeln('Please Enter your mileage :> ');
          readLn(M);
                   
          ALLOWANCE:=M * 0.15;
                 
          WriteLn('You have selected Motorcycle as your means of transport');
          writeLn('Your weeks mileage is ,'M);
          WriteLn('Your travelling allowance is ,' A);    /* A is not defined.  Use Allowance */
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
                                   
          Delay(2000);
/*        Goto Return;        << Unnecessary */
        End;



      if (selection)=2 then
/*                 ^ in wrong place! */
        BEGIN
          writein('Please Enter your mileage :> ');
          readLn(M);
                     
          allowance:=M * 0.20;

                 
          WriteLn('You have selected a car with below 1500 cc capacity as your means of transport');
          writeLn('Your weeks mileage is ,'M);
          WriteLn('Your travelling allowance is ,'A);    /* A is not defined.  Use Allowance */
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
                                   
          Delay(2000);
/*        Goto Return;        << Unnecessary */
        End;


      if (selection)=3
/*                 ^ in wrong place! */
        Begin
          writein('Please Enter your mileage :> ');
          readLn(M);
                     
          allowance:=M * 0.30;

             
          WriteLn('You have selected a car with more than 1500 cc capacity as your means of transport');
          writeLn('Your weeks mileage is ,'M);
          WriteLn('Your travelling allowance is ,'A);    /* A is not defined.  Use Allowance */
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
               
          Delay(2000);
        End;
        READKEY;
         
             
      EMPNUM=EMPNUM+1
   UNTIL (EMPNUM=TOTALREC);
END.
---------------------------------------------------------------------------------------------------------------------------------------------
2. You don't need, nor do you define your label.
3. Instead of using:

    REPEAT
      ...
         EMPNUM=EMPNUM+1
    UNTIL (EMPNUM=TOTALREC);

    The following is generally easier to read:

    FOR EMPNUM := 1 TO TOTALREC DO
      BEGIN
         ...
      END
4. When you use if statements to test the user input, you aren't checking for an invalid value.
    One way to do this is to use "nested" if statements

    if ( selection = 1 ) then
      begin
        ...
      end
    else if ( selection = 2 ) then
      begin
        ...
      end
    else if ( selection = 3 ) then
      begin
        ...
      end
    else
      begin
         ... user entered an invalid selection ...
         ... what do you want to do? ...
      end;

   /* Here */

5. When you find "common code" in each and every path, move the code to where the paths come together.
    For example, in your code, each "if" clause ends with "delay(2000);"
    If you chose to use the nested in statements, then this statement could/should be moved after the nested
    if statement.

    After any clause in the if statement is executed, the statement after the combined/nested if statement will be
    the next one executed.  See the "Here" comment above.

6. You declare "allowance", but then your output statements all try to use "A" instead.
   

Author

Commented:
OK. Thank you for your replies. the code then should be as follows i presume...

***********************
CODE START
***********************

PROGRAM USERINPUTRECORDS (INPUT,OUTPUT);
USES CRT;

LABEL RETURN;    /* << Unnecessary */

CONST
   ENDMARKER = -1;

VAR    
   TOTALREC  : INTEGER;
   MILES     : INTEGER;
   ALLOWANCE : REAL;
   SELECTION : INTEGER;
   EMPNUM    : INTEGER;


BEGIN
   EMPNUM = 1;
   CLRSCR;

   WRITELN( 'HOW MANY EMPLOYEES DO YOU WISH TO PROCESS? ' );
   READLN( TOTALREC )               /*       << Missing ';' */
   CLRSCR;


      FOR EMPNUM := 1 TO TOTALREC DO
      BEGIN  

      WRITELN( '1     MOTORCYCLE' );
      WRITELN( '2     CAR WITH BELOW 1500 CC CAPACITY' );
      WRITELN( '3     CAR WITH MORE THAN 1500 CC CAPACITY' );
      WRITELN( ' ' );
      WRITELN( 'PLEASE ENTER TRANSPORT FOR EMPLOYEE ', EMPNUM: ');




if ( selection = 1 ) then
      begin
          writeln('Please Enter your mileage :> ');
          readLn(Miles);
                   
          ALLOWANCE:=Miles * 0.15;
                 
          WriteLn('You have selected Motorcycle as your means of transport');
          writeLn('Your weeks mileage is ,'Miles);
          WriteLn('Your travelling allowance is ,' Allowance);
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
                                   
          Delay(2000);
End;



if ( selection = 2 ) then
      begin
          writeln('Please Enter your mileage :> ');
          readLn(Miles);
                   
          ALLOWANCE:=Miles * 0.20;
                 
          WriteLn('You have selected Motorcycle as your means of transport');
          writeLn('Your weeks mileage is ,'Miles);
          WriteLn('Your travelling allowance is ,' Allowance);
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
                                   
          Delay(2000);
End;




if ( selection = 3 ) then
      begin
          writeln('Please Enter your mileage :> ');
          readLn(Miles);
                   
          ALLOWANCE:=Miles * 0.30;
                 
          WriteLn('You have selected Motorcycle as your means of transport');
          writeLn('Your weeks mileage is ,'Miles);
          WriteLn('Your travelling allowance is ,' Allowance);
          WriteLN(' ');
          WriteLN('Press Any Key To Continue...');
                                   
          Delay(2000);
End;


else
    begin
          WRITELN('You Have Entered an Invalid Selecion!');
        WRITELN('Press Any Key To Exit...');
    delay(2000);

end.



*************************
CODE END
*************************

Author

Commented:
FIGURED OUT THE CODE TO GET THIS TO WORK 100%
----------------------------------------------------------------


PROGRAM TASK3(INPUT,OUTPUT);
USES CRT;

VAR            TOTALREC            :INTEGER;
            MILES                  :INTEGER;
            ALLOWANCE            :REAL;
            SELECTION            :INTEGER;
            EMPNUM                  :INTEGER;
            
BEGIN

      WRITE('HOW MANY RECORDS DO YOU WISH TO PROCESS:> ');
      READ(TOTALREC);

            FOR EMPNUM:=1 TO TOTALREC DO
            BEGIN
            
      CLRSCR;
      WRITELN('THE SYSTEM RECOGNISES THE FOLLOWING MEANS OF TRANSPORT:');
      WRITELN;
      WRITELN('1-MOTORCYCLE');
      WRITELN('2-CAR WITH LESS THAN 1500 CC CAPACITY');
      WRITELN('3-CAR WITH MORE THAN 1500 CC CAPACITY');
      WRITELN;
      WRITE('PLEASE ENTER TRANSPORT FOR EMPLOYEE ', EMPNUM);
    WRITE(': ');
    READ(SELECTION);

      
      
            IF (SELECTION=1) THEN
            BEGIN
        WRITE('PLEASE ENTER MILEAGE:> ');
            READLN(MILES);
            ALLOWANCE:=MILES*0.15;
        CLRSCR;
        WRITELN;
            WRITELN('YOU SELECTED MOTORCYCLE AS YOUR MEANS OF TRANSPORT');
            WRITELN('YOUR WEEKS MILEAGE IS ', MILES);
            WRITELN('YOUR TRAVELLING ALLOWANCE IS ', ALLOWANCE:4:2);
            WRITELN;
            WRITE('PRESS ANY KEY TO CONTINUE...');
            DELAY(2000);
            READKEY;
        CLRSCR;
            END;
            
            
            
            IF (SELECTION=2) THEN
            BEGIN
        WRITE('PLEASE ENTER YOUR MILEAGE:> ');
            READLN(MILES);
            ALLOWANCE:=MILES*0.20;
        CLRSCR;
        WRITELN;
            WRITELN('YOU SELECTED A CAR WITH LESS THAN 1500 CC CAPACITY AS YOUR MEANS OF TRANSPORT');
            WRITELN('YOUR WEEKS MILEAGE IS ', MILES);
            WRITELN('YOUR TRAVELLING ALLOWANCE IS ', ALLOWANCE:4:2);
            WRITELN;
            WRITE('PRESS ANY KEY TO CONTINUE...');
            DELAY(2000);
            READKEY;
        CLRSCR;
            END;
            
            
            
            IF (SELECTION=3) THEN
            BEGIN
        WRITE('PLEASE ENTER YOUR MILEAGE:> ');
            READLN(MILES);
            ALLOWANCE:=MILES*0.30;
        CLRSCR;
        WRITELN;
            WRITELN('YOU SELECTED A CAR WITH MORE THAN 1500 CC CAPACITY AS YOUR MEANS OF TRANSPORT');
            WRITELN('YOUR WEEKS MILEAGE IS ', MILES);
            WRITELN('YOUR TRAVELLING ALLOWANCE IS ', ALLOWANCE:4:2);
            WRITELN;
            WRITE('PRESS ANY KEY TO CONTINUE...');
            DELAY(2000);
            READKEY;
        CLRSCR;
            END;

IF (EMPNUM=TOTALREC+1) THEN
END;
end.


This works perfectly.