Solved

I got a problem with Array

Posted on 1998-10-12
12
173 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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