[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

I got a problem with Array

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
lewis_loo
Asked:
lewis_loo
  • 7
  • 5
1 Solution
 
lewis_looAuthor Commented:
Please help immediately, because I want to know how can array, file and record join together, because now I am learning Pascal language.
0
 
scrapdogCommented:
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
 
scrapdogCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
scrapdogCommented:
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
 
scrapdogCommented:
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
 
scrapdogCommented:
Oops, I submitted the same comment twice in a row by accident.  Hopefully this didn't confuse you :)
0
 
scrapdogCommented:
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
 
lewis_looAuthor Commented:
Can you compress it into a really short program, but do the same task with my program?
0
 
lewis_looAuthor Commented:
Can you compress it into a really short program, but do the same task with my program?
0
 
scrapdogCommented:
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
 
lewis_looAuthor Commented:
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
 
lewis_looAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now