Bookstore

Recent

Author Topic: Program not outputting contents of variables  (Read 245 times)

flyingfish360

  • Newbie
  • Posts: 2
Program not outputting contents of variables
« on: February 08, 2020, 03:12:01 pm »
Hi, I have this program as an assignment. It compiles and runs but is does not show the output for the title first name and last name. can someone help? if not please direct me to the appropriate help section. Thanks

Program employee_deductions;

{This algorithm prompts the user to input the Employee ID, title, first name,
last name, salary and number of dependents, then calculates the NIC deductions, paye, medical insurance,
total deductions, net pay of each employee and outputs their title, first name, last name and net pay.}

const
flat_rate = 50;

Var
employeeid:      string;
title:                 string;
firstname:              string;
lastname:              string;
salary:         real;
dependents:      integer;
med_insurance:      real;
nic_ded:              real;
tot_deduction:      real;
netpay:         real;
paye:                        real;

Begin
    Writeln('Please enter your employee ID title, first name, last name');
    Readln(employeeid, title, firstname, lastname);
    Writeln('please enter your salary and number of dependents');
    Readln(salary, dependents);

    While employeeid<>'end' do
       Begin
      If salary <= 5000 then
      Nic_ded:= salary*0.05
      Else
      Nic_ded:=250;


      If salary<10000 then
      paye:=salary * 0.05
      Else
      paye:=salary * 0.1;

      Med_insurance:=(dependents*20) + flat_rate;

      tot_deduction:=med_insurance + Nic_ded + paye;

           netpay:= salary - tot_deduction;
           writeln;
           writeln;
           Writeln('Title: ');
           Writeln('First name: ',firstname);
           Writeln('Last name: ',lastname);
           Writeln('Your netpay is: ',netpay:5:2);

   Writeln('Please enter your employee ID, title, first name, last name');
   Readln(employeeid, title, firstname, lastname);
   Writeln('please enter your salary and number of dependents');
   Readln(salary, dependents);
End;

End.
                           

lucamar

  • Hero Member
  • *****
  • Posts: 2561
Re: Program not outputting contents of variables
« Reply #1 on: February 08, 2020, 06:03:31 pm »
You can easily see what's wrong by just adding a line before the other WriteLn's:
Code: Pascal  [Select]
  1. WriteLn(employeeid)

The full code (after some other minor changes) would then be:

Code: Pascal  [Select]
  1. Program employee_deductions;
  2.  
  3. {This algorithm prompts the user to input the Employee ID, title, first name,
  4. last name, salary and number of dependents, then calculates the NIC deductions, paye, medical insurance,
  5. total deductions, net pay of each employee and outputs their title, first name, last name and net pay.}
  6.  
  7. const
  8.   flat_rate = 50;
  9.  
  10. Var
  11.   employeeid:      string;
  12.   title:                 string;
  13.   firstname:              string;
  14.   lastname:              string;
  15.   salary:         real;
  16.   dependents:      integer;
  17.   med_insurance:      real;
  18.   nic_ded:              real;
  19.   tot_deduction:      real;
  20.   netpay:         real;
  21.   paye:                        real;
  22.  
  23. Begin
  24.   employeeid := '';
  25.   While employeeid<>'end' do
  26.   Begin
  27.     Writeln('Please enter your employee ID title, first name, last name');
  28.     Readln(employeeid, title, firstname, lastname);
  29.     Writeln('please enter your salary and number of dependents');
  30.     Readln(salary, dependents);
  31.  
  32.     If salary <= 5000 then
  33.       Nic_ded:= salary*0.05
  34.     Else
  35.       Nic_ded:=250;
  36.  
  37.  
  38.     If salary<10000 then
  39.       paye:=salary * 0.05
  40.     Else
  41.       paye:=salary * 0.1;
  42.  
  43.     Med_insurance:=(dependents*20) + flat_rate;
  44.  
  45.     tot_deduction:=med_insurance + Nic_ded + paye;
  46.  
  47.     netpay:= salary - tot_deduction;
  48.     writeln;
  49.     writeln;
  50. {Added for debugging: }
  51.     WriteLn('employeeid :', employeeid);
  52.     Writeln('Title: ');
  53.     Writeln('First name: ',firstname);
  54.     Writeln('Last name: ',lastname);
  55.     Writeln('Your netpay is: ',netpay:5:2);
  56.   end;
  57. End.

and running it produces this result:

Code: [Select]
Please enter your employee ID title, first name, last name
7530 Title Luis Caballero
please enter your salary and number of dependents
1230 0


employeeid :7530 Title Luis Caballero
Title:
First name:
Last name:
Your netpay is: 1057.00
Please enter your employee ID title, first name, last name

As you can see, the problem is that ReadLn reads the whole line into the first variable, as it should. Instead you should just Read() each variable but the last, which you can ReadLn()

BTW, for future reference you might wnat to  read this wiki page: Forum::Use Code tags :D
« Last Edit: February 08, 2020, 06:09:40 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

flyingfish360

  • Newbie
  • Posts: 2
Re: Program not outputting contents of variables
« Reply #2 on: February 08, 2020, 06:42:50 pm »
thanks