Solved

I got a problem with Array

Posted on 1998-10-12
12
178 Views
Last Modified: 2011-09-20
I have a problem with my pascal programming
I want this program to save record to a file and Array contain record for instance Array[1] is contain record 1, Array [2] is contain record 2 and so on so forth.

My program has an error in line 36 count from "Program SD", Error 88 : "(" expected.

Please help me Ok!!!

Program SD;
uses crt;
type customer=record
               order_id:string;
               product_id:string;
               unit_price:integer;
               quantity:integer;
               amount:integer;
               discount:integer;
               total_pay:integer;
             end;
    cust = array [1..20] of customer;
var cus : cust;
    fv : file of cust;
    con1,ans,opt,yn:char;
    rec,n,i:integer;
    open: string;

Procedure input;
begin
    clrscr;
    for i:=1 to n do begin
    with cus[i] do begin
      writeln('Record #',i);
      write('Order id   : ');readln(order_id);
      write('Product id : ');readln(product_id);
      write('Unit Price : ');readln(unit_price);
      write('Quantity   : ');readln(quantity);
      amount:=unit_price*quantity;
      write('Amount     : ',amount);
      writeln;
      write('Discount   : ');readln(discount);
      total_pay:=amount-discount;
      writeln('Total pay  : ',total_pay);
    end;
    write(fv,cust[i]);
    end;
end;

Procedure list;
begin
    clrscr;
    reset(fv);
    writeln('Record no':2,'Product id':12,'Quantity':10,'Amount':8,
            'Discount':10,'Total pay':12);
    while not eof(fv) do begin
     read(fv,cus);
{     for i:=1 to n do}
     with cus do
        writeln(i:2,order_id:12,product_id:12,quantity:10,amount:8,
               discount:10,total_pay:12);
        i:=i+1;
    end;
end;

Procedure search;
begin
  clrscr;
  write('Wanna search for record number :');readln(rec);
  reset(fv);
  n:=filesize(fv);
  if rec<=n then begin
      seek(fv,n-1);
      with cus do begin
       writeln('Record no':2,'Order id':12,'Product id':12,'Quantity':10,'Amount':8,
               'Discount':10,'Total pay':12);
       writeln(rec:2,order_id:12,product_id:12,quantity:10,amount:8,
                  discount:10,total_pay:12);
     end;
  end;
end;

function FileExists(FileName: String): Boolean;
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0; {set default file mode to read only mode
                 while the default is 2 write and read, this function
                 is used while we want to reset a file}
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

begin
  clrscr;
  write('Open a File : ');readln(open);
  if FileExists(open) then begin
     write('Want to rewrite a file : ');readln(ans);
     if upcase(ans)='Y' then begin assign(fv,open);rewrite(fv); end
     else begin
        write('Enter new file : ');readln(open);
        assign(fv,open);
     end;
  end else begin
     write('Create a new file : ');readln(con1);
     if upcase(con1)='Y' then begin
          write('Enter new file : ');readln(open);
          assign(fv,open);
     end else exit;
  end;
  n:=1;
  repeat
    clrscr;
    writeln('Filename : ',open);
    writeln('1. Input your record ');
    writeln('2. List your record  ');
    writeln('3. Search record     ');
    writeln('4. Exit              ');
    write('Choose your option : ');
    opt:=readkey;
    yn:='Y';
    case opt of
      '1' : begin
               while upcase(yn)='Y' do begin
                 input;
                 write('Want to continue ? ');readln(yn);
                 n:=n+1;
               end;
             end;
       '2' : begin
               i:=1;
               list;
               yn:='Y';
               readkey;
             end;
       '3' : begin
               search;
               yn:='Y';
               readkey;
             end;
       '4' : exit;
    end;
  until (yn<>'Y') and (yn='N');
  close(fv);
end.ant
0
Comment
Question by:lewis_loo
  • 7
  • 5
12 Comments
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215780
Please help immediately, because I want to know how can array, file and record join together, because now I am learning Pascal language.
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1215781
Your problem in line 36 can be changed to

write(fv,cus[i]);

cust was your TYPE, not your array.

Also, you need to change your file declaration to

fv :file of customer;

(rather than file of cust)

You want to write individual records to the file, not whole arrays.


0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215782
I just loaded up your file and made the necessary changes.  You no longer get the error message, but it looks as if there are errors in other parts of your code.

I will check them out and try to fix them.  I'll get back to you in a bit.

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1215783
This program will compile correctly.  I made a note of lines I added and lines I changed.

---------------------------------

Program SD;
uses crt;
type customer=record
               order_id:string;
               product_id:string;
               unit_price:integer;
               quantity:integer;
               amount:integer;
               discount:integer;
               total_pay:integer;
             end;
    cust = array [1..20] of customer;
var cus : cust;
    fv : file of cust;
    con1,ans,opt,yn:char;
    rec,n,i:integer;
    open: string;

Procedure input;
begin
    clrscr;
    for i:=1 to n do begin
    with cus[i] do begin
      writeln('Record #',i);
      write('Order id   : ');readln(order_id);
      write('Product id : ');readln(product_id);
      write('Unit Price : ');readln(unit_price);
      write('Quantity   : ');readln(quantity);
      amount:=unit_price*quantity;
      write('Amount     : ',amount);
      writeln;
      write('Discount   : ');readln(discount);
      total_pay:=amount-discount;
      writeln('Total pay  : ',total_pay);
    end;
    write(fv,cust[i]);
    end;
end;

Procedure list;
begin
    clrscr;
    reset(fv);
    writeln('Record no':2,'Product id':12,'Quantity':10,'Amount':8,
            'Discount':10,'Total pay':12);
    while not eof(fv) do begin
     read(fv,cus);
{     for i:=1 to n do}
     with cus do
        writeln(i:2,order_id:12,product_id:12,quantity:10,amount:8,
               discount:10,total_pay:12);
        i:=i+1;
    end;
end;

Procedure search;
begin
  clrscr;
  write('Wanna search for record number :');readln(rec);
  reset(fv);
  n:=filesize(fv);
  if rec<=n then begin
      seek(fv,n-1);
      with cus do begin
       writeln('Record no':2,'Order id':12,'Product id':12,'Quantity':10,'Amount':8,
               'Discount':10,'Total pay':12);
       writeln(rec:2,order_id:12,product_id:12,quantity:10,amount:8,
                  discount:10,total_pay:12);
     end;
  end;
end;

function FileExists(FileName: String): Boolean;
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0; {set default file mode to read only mode
                 while the default is 2 write and read, this function
                 is used while we want to reset a file}
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

begin
  clrscr;
  write('Open a File : ');readln(open);
  if FileExists(open) then begin
     write('Want to rewrite a file : ');readln(ans);
     if upcase(ans)='Y' then begin assign(fv,open);rewrite(fv); end
     else begin
        write('Enter new file : ');readln(open);
        assign(fv,open);
     end;
  end else begin
     write('Create a new file : ');readln(con1);
     if upcase(con1)='Y' then begin
          write('Enter new file : ');readln(open);
          assign(fv,open);
     end else exit;
  end;
  n:=1;
  repeat
    clrscr;
    writeln('Filename : ',open);
    writeln('1. Input your record ');
    writeln('2. List your record  ');
    writeln('3. Search record     ');
    writeln('4. Exit              ');
    write('Choose your option : ');
    opt:=readkey;
    yn:='Y';
    case opt of
      '1' : begin
               while upcase(yn)='Y' do begin
                 input;
                 write('Want to continue ? ');readln(yn);
                 n:=n+1;
               end;
             end;
       '2' : begin
               i:=1;
               list;
               yn:='Y';
               readkey;
             end;
       '3' : begin
               search;
               yn:='Y';
               readkey;
             end;
       '4' : exit;
    end;
  until (yn<>'Y') and (yn='N');
  close(fv);
end.ant  
 

------------------------------------------

In your search procedure, you were trying to read in a whole array, when you really wanted to read just one customer record.  So that's why I added cust_in :customer, for this purpose.

The program compiles correctly, but you will have to see if it runs exactly what you want it to.  I would suggest using local variables to keep things less confusing.  

Scrapdog
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215784
This program will compile correctly.  I made a note of lines I added and lines I changed.

---------------------------------

Program SD;
uses crt;
type customer=record
               order_id:string;
               product_id:string;
               unit_price:integer;
               quantity:integer;
               amount:integer;
               discount:integer;
               total_pay:integer;
             end;
    cust = array [1..20] of customer;
var cus : cust;
    fv : file of cust;
    con1,ans,opt,yn:char;
    rec,n,i:integer;
    open: string;

Procedure input;
begin
    clrscr;
    for i:=1 to n do begin
    with cus[i] do begin
      writeln('Record #',i);
      write('Order id   : ');readln(order_id);
      write('Product id : ');readln(product_id);
      write('Unit Price : ');readln(unit_price);
      write('Quantity   : ');readln(quantity);
      amount:=unit_price*quantity;
      write('Amount     : ',amount);
      writeln;
      write('Discount   : ');readln(discount);
      total_pay:=amount-discount;
      writeln('Total pay  : ',total_pay);
    end;
    write(fv,cust[i]);
    end;
end;

Procedure list;
begin
    clrscr;
    reset(fv);
    writeln('Record no':2,'Product id':12,'Quantity':10,'Amount':8,
            'Discount':10,'Total pay':12);
    while not eof(fv) do begin
     read(fv,cus);
{     for i:=1 to n do}
     with cus do
        writeln(i:2,order_id:12,product_id:12,quantity:10,amount:8,
               discount:10,total_pay:12);
        i:=i+1;
    end;
end;

Procedure search;
begin
  clrscr;
  write('Wanna search for record number :');readln(rec);
  reset(fv);
  n:=filesize(fv);
  if rec<=n then begin
      seek(fv,n-1);
      with cus do begin
       writeln('Record no':2,'Order id':12,'Product id':12,'Quantity':10,'Amount':8,
               'Discount':10,'Total pay':12);
       writeln(rec:2,order_id:12,product_id:12,quantity:10,amount:8,
                  discount:10,total_pay:12);
     end;
  end;
end;

function FileExists(FileName: String): Boolean;
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0; {set default file mode to read only mode
                 while the default is 2 write and read, this function
                 is used while we want to reset a file}
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

begin
  clrscr;
  write('Open a File : ');readln(open);
  if FileExists(open) then begin
     write('Want to rewrite a file : ');readln(ans);
     if upcase(ans)='Y' then begin assign(fv,open);rewrite(fv); end
     else begin
        write('Enter new file : ');readln(open);
        assign(fv,open);
     end;
  end else begin
     write('Create a new file : ');readln(con1);
     if upcase(con1)='Y' then begin
          write('Enter new file : ');readln(open);
          assign(fv,open);
     end else exit;
  end;
  n:=1;
  repeat
    clrscr;
    writeln('Filename : ',open);
    writeln('1. Input your record ');
    writeln('2. List your record  ');
    writeln('3. Search record     ');
    writeln('4. Exit              ');
    write('Choose your option : ');
    opt:=readkey;
    yn:='Y';
    case opt of
      '1' : begin
               while upcase(yn)='Y' do begin
                 input;
                 write('Want to continue ? ');readln(yn);
                 n:=n+1;
               end;
             end;
       '2' : begin
               i:=1;
               list;
               yn:='Y';
               readkey;
             end;
       '3' : begin
               search;
               yn:='Y';
               readkey;
             end;
       '4' : exit;
    end;
  until (yn<>'Y') and (yn='N');
  close(fv);
end.ant  
 

------------------------------------------

In your search procedure, you were trying to read in a whole array, when you really wanted to read just one customer record.  So that's why I added cust_in :customer, for this purpose.

The program compiles correctly, but you will have to see if it runs exactly the way you want it to.  I would suggest using local variables to keep things less confusing.  

Scrapdog
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215785
Oops, I submitted the same comment twice in a row by accident.  Hopefully this didn't confuse you :)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215786
STUPID ME.  I pasted your original code back (twice!!)  Disregard them!  HERE is the revised code:
(THIS one WILL work. :)
------------------------------------------------

Program SD;
uses crt;
type customer=record
               order_id:string;
               product_id:string;
               unit_price:integer;
               quantity:integer;
               amount:integer;
               discount:integer;
               total_pay:integer;
             end;
    cust = array [1..20] of customer;
var cus : cust;
    cust_in  :customer;      {added}
    fv : file of customer;   {changed from cust}
    con1,ans,opt,yn:char;
    rec,n,i:integer;
    open: string;

Procedure input;
begin
    clrscr;
    for i:=1 to n do begin
    with cus[i] do begin
      writeln('Record #',i);
      write('Order id   : ');readln(order_id);
      write('Product id : ');readln(product_id);
      write('Unit Price : ');readln(unit_price);
      write('Quantity   : ');readln(quantity);
      amount:=unit_price*quantity;
      write('Amount     : ',amount);
      writeln;
      write('Discount   : ');readln(discount);
      total_pay:=amount-discount;
      writeln('Total pay  : ',total_pay);
    end;
    write(fv,cus[i]);               {changed}
    end;
end;

Procedure list;
begin
    clrscr;
    reset(fv);
    writeln('Record no':2,'Product id':12,'Quantity':10,'Amount':8,
            'Discount':10,'Total pay':12);
    i := 1;
    while not eof(fv) and (i<=n) do begin
     read(fv,cus[i]);       {changed}
     with cus[i] do         {changed}
        writeln(i:2,order_id:12,product_id:12,quantity:10,amount:8,
               discount:10,total_pay:12);
        i:=i+1;
    end;
end;

Procedure search;
begin
  clrscr;
  write('Wanna search for record number :');readln(rec);
  reset(fv);
  n:=filesize(fv);
  if rec<=n then begin
      seek(fv,rec-1);      {changed}
      read(fv,cust_in);    {added}
      with cust_in do begin      {changed}
       writeln('Record no':2,'Order id':12,'Product id':12,'Quantity':10,'Amount':8,
               'Discount':10,'Total pay':12);
       writeln(rec:2,order_id:12,product_id:12,quantity:10,amount:8,
                  discount:10,total_pay:12);
     end;
  end;
end;

function FileExists(FileName: String): Boolean;
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0; {set default file mode to read only mode
                 while the default is 2 write and read, this function
                 is used while we want to reset a file}
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

begin
  clrscr;
  write('Open a File : ');readln(open);
  if FileExists(open) then begin
     write('Want to rewrite a file : ');readln(ans);
     if upcase(ans)='Y' then begin assign(fv,open);rewrite(fv); end
     else begin
        write('Enter new file : ');readln(open);
        assign(fv,open);
     end;
  end else begin
     write('Create a new file : ');readln(con1);
     if upcase(con1)='Y' then begin
          write('Enter new file : ');readln(open);
          assign(fv,open);
     end else exit;
  end;
  n:=1;
  repeat
    clrscr;
    writeln('Filename : ',open);
    writeln('1. Input your record ');
    writeln('2. List your record  ');
    writeln('3. Search record     ');
    writeln('4. Exit              ');
    write('Choose your option : ');
    opt:=readkey;
    yn:='Y';
    case opt of
      '1' : begin
               while upcase(yn)='Y' do begin
                 input;
                 write('Want to continue ? ');readln(yn);
                 n:=n+1;
               end;
             end;
       '2' : begin
               i:=1;
               list;
               yn:='Y';
               readkey;
             end;
       '3' : begin
               search;
               yn:='Y';
               readkey;
             end;
       '4' : exit;
    end;
  until (yn<>'Y') and (yn='N');
  close(fv);
end.ant  

0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215787
Can you compress it into a really short program, but do the same task with my program?
0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215788
Can you compress it into a really short program, but do the same task with my program?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215789
Program SD;
uses crt;
type customer=record
               order_id:string;
               product_id:string;
               unit_price:integer;
               quantity:integer;
               amount:integer;
               discount:integer;
               total_pay:integer;
             end;
    customer_file = file of customer;

Procedure input(var fv :customer_file; i :integer);
var cust_out :customer;
begin
    clrscr;
    with cust_out do begin
      writeln('Record #',i);
      write('Order id   : ');readln(order_id);
      write('Product id : ');readln(product_id);
      write('Unit Price : ');readln(unit_price);
      write('Quantity   : ');readln(quantity);
      amount:=unit_price*quantity;
      write('Amount     : ',amount);
      writeln;
      write('Discount   : ');readln(discount);
      total_pay:=amount-discount;
      writeln('Total pay  : ',total_pay);
    end;
    write(fv,cust_out);               {changed}
end;

Procedure list(var fv :customer_file; n :integer);
var i :integer;
    cust_in :customer;
begin
    clrscr;
    writeln('Record no':2,'Product id':12,'Quantity':10,'Amount':8,
            'Discount':10,'Total pay':12);
    i := 1;
    while not eof(fv) and (i<=n) do begin
     read(fv,cust_in);       {changed}
     with cust_in do         {changed}
        writeln(i:2,order_id:12,product_id:12,quantity:10,amount:8,
               discount:10,total_pay:12);
        i:=i+1;
    end;
end;

Procedure search(var fv :customer_file);
var cust_in :customer;
    n,rec :integer;
begin
  clrscr;
  write('Wanna search for record number :');readln(rec);
  n:=filesize(fv);
  if rec<=n then begin
      seek(fv,rec-1);      {changed}
      read(fv,cust_in);    {added}
      with cust_in do begin      {changed}
       writeln('Record no':2,'Order id':12,'Product id':12,'Quantity':10,'Amount':8,
               'Discount':10,'Total pay':12);
       writeln(rec:2,order_id:12,product_id:12,quantity:10,amount:8,
                  discount:10,total_pay:12);
     end;
  end;
end;

function FileExists(FileName: String): Boolean;
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0; {set default file mode to read only mode
                 while the default is 2 write and read, this function
                 is used while we want to reset a file}
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

function UserAnswer :boolean;
var ans :string;
begin
  readln(ans);
  if (pos('Y',ans) = 0) and (pos('y',ans) = 0) then UserAnswer := false
  else UserAnswer := true;
end;

function GetFileToOpen(var fv :customer_file) :string;
var filename :string;
begin
  clrscr;
  write('Open a File : ');
  readln(filename);
  assign(fv,filename);
  GetFileToOpen := filename;
end;

function UserRewriteFile(var fv :customer_file) :boolean;
begin
  write('Want to rewrite a file : ');
  if UserAnswer then begin
    UserRewriteFile := true;
  end
  else UserRewriteFile := false;
end;




function UserCreateFile(var fv :customer_file;
                        var filename :string) :boolean;
begin
  write('Create a new file : ');
  if UserAnswer then begin write('Enter new file: '); readln(filename);
    Assign(fv,filename); UserCreateFile := true;
  end
  else UserCreateFile := false;
end;


function GetUserChoice(var filename :string) :char;
begin
  clrscr;
  writeln('Filename : ',filename);
  writeln('1. Input your record ');
  writeln('2. List your record  ');
  writeln('3. Search record     ');
  writeln('4. Exit              ');
  write('Choose your option : ');
  GetUserChoice:=readkey;
end;





var filename :string;
    fv :customer_file;
    n :integer;
    opt :char;

begin
  filename := GetFileToOpen(fv);
  if FileExists(filename) then
    if not UserRewriteFile(fv) then exit
  else if not UserCreateFile(fv, filename) then exit;
  n := 1;
  repeat
    opt := GetUserChoice(filename);
    case opt of
      '1': begin Rewrite(fv); repeat Input(fv,n); write('Want to continue? ');
           n:=n+1; until not UserAnswer; Close(fv); end;
      '2': begin Reset(fv); List(fv,n); Close(fv); readkey; end;
      '3': begin Reset(fv); Search(fv); Close(fv); end;
    end;
  until not (opt in ['1', '2', '3']);
end.


It is not necessarily shorter, but it is better organized.
0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215790
I got some problem in file, if I want to input again in the same filename and I want to continue to the next record, when I run the program again. In which I mean, I don't want to rewrite that file, I want to continue to write in the next record but at the same file. Is there any way to do this task?
0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215791
I got some problem in file, if I want to input again in the same filename and I want to continue to the next record, when I run the program again. In which I mean, I don't want to rewrite that file, I want to continue to write in the next record but at the same file. Is there any way to do this task?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question