Recent

Author Topic: Unit error  (Read 8929 times)

captian jaster

  • Guest
Unit error
« on: March 03, 2010, 08:19:19 pm »
Code: [Select]
unit Wallet;//By Adam N.Andujar

interface


uses CRT,SysUtils,Classes,Windows;


const
 Error1 = 'File Not Found!';

Procedure WalletProcedures;

type
  Number = Integer;
  Walletrec = Record
  Balance,Cost : Number;
  END;

var
 WalletFile : Text;
 INWallet : Walletrec;
 Readstr : String;
implementation

Procedure WalletProcedures;
BEGIN
  assignfile(WalletFile,'Wallet.dat');
  while NOT  Fileexists('Wallet.dat') do
  begin//Make wallet
    writeln('Input your current Balance.');
    Readln(INWallet.Balance);
    rewrite(WalletFile);
    writeln(WalletFile,INWallet.Balance);
    Closefile(WalletFile);
  end;
  while NOT Eof(WalletFile) Do
  begin//Display Balance
  writeln('Current Balance:');
  reset(WalletFile);
  readln(WalletFile,ReadStr);
  writeln(ReadStr);
  end;

  Writeln('What would you like to do?');
  writeln('Add to current balance.');
  writeln('Remove from current balance');
  writeln('Re-Enter a Balance');
END;

Procedure RemoveFrom;

type
  Number = Integer;
  Walletrec = Record
  Balance,Cost : Number;
  END;

var
 WalletFile : Text;
 INWallet : Walletrec;
 Readstr : Integer;


Procedure RemoveFrom;
BEGIN
  assignfile(WalletFile,'Wallet.dat');
  writeln('How Much(Numbers Only)?');
  readln(INWallet.Cost);
  while NOT Eof(WalletFile) Do
  begin//Reads the file
  reset(WalletFile);
  readln(WalletFile,ReadStr);
  rewrite(WalletFile);
  Erase(WalletFile);
  writeln(WalletFile,INWallet.Cost - ReadStr);
  CloseFile(WalletFile);
  writeln('Saved!');
  end;
END;


end.
my compiler says begin expected but end. found

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Unit error
« Reply #1 on: March 04, 2010, 05:59:57 am »
Code: [Select]
Procedure RemoveFrom; // <-- you miss forward modifier here

type
  Number = Integer;
  Walletrec = Record
  Balance,Cost : Number;
  END;

var
 WalletFile : Text;
 INWallet : Walletrec;
 Readstr : Integer;
Without it, the next type and variable declaration will be considered as local declaration in the procedure scope. And following the syntax rules, after local declaration, procedure body is expected.

There are still compile errors though, due to duplicate identifiers. Remember that what you declare in interface section are available in implementation as well, but not the other way around.

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Unit error
« Reply #2 on: March 04, 2010, 11:48:36 am »
Alternatively.

Code: [Select]
unit Wallet;//By Adam N.Andujar

Procedure RemoveFrom;

type
  Number = Integer;
  Walletrec = Record
  Balance,Cost : Number;
  END;

var
 WalletFile : Text;
 INWallet : Walletrec;
 Readstr : Integer;


Procedure RemoveFrom;   <--- this one is one too much. remove the line
BEGIN

NB. Even after that it will not compile.
It needs
Code: [Select]
{$mode objfpc}   //or {$mode delphi}

or it will stumble upon the assignfile, closefile statements.

More C&C.

Code: [Select]
Procedure WalletProcedures;
BEGIN
  assignfile(WalletFile,'Wallet.dat');
  while NOT  Fileexists('Wallet.dat') do

  //no need for a while loop here.
  //a simple if NOT  FileExists('Wallet.dat')  then would suffice

  begin//Make wallet
    writeln('Input your current Balance.');
    Readln(INWallet.Balance);
    rewrite(WalletFile);
    writeln(WalletFile,INWallet.Balance);
    Closefile(WalletFile);
  end;

  //you just closed the file, so the next line will give a runtime error

  while NOT Eof(WalletFile) Do
  //using a while loop would cause an infinitive loop here if the file contains more than 1 line
  //because you reset the file before each read and you wull never reach the end of the file
  begin//Display Balance
    writeln('Current Balance:');
    reset(WalletFile);
    readln(WalletFile,ReadStr);
    writeln(ReadStr);
  end;


  <snip>

Procedure RemoveFrom;

type
  Number = Integer;
  Walletrec = Record
  Balance,Cost : Number;
  END;

var
 WalletFile : Text;
 INWallet : Walletrec;
 Readstr : Integer;


BEGIN
  assignfile(WalletFile,'Wallet.dat');
  writeln('How Much(Numbers Only)?');
  readln(INWallet.Cost);
  //Q: What happens if user enters 'Bla' (or anything else that is NOT an Integer) ?


  //You'll have to open the file somehow first to test for EOF
  while NOT Eof(WalletFile) Do
  //Again, why do you need a loop here?
  begin//Reads the file
      reset(WalletFile);
      readln(WalletFile,ReadStr);
      rewrite(WalletFile);
      Erase(WalletFile);
      //Now you try to write to a file you have just ERASED !!
      // even so, should that be ReadStr - INWallet.Cost ??
      writeln(WalletFile,INWallet.Cost - ReadStr); 
     
      //How is this gonna work: the file no longer existst
      CloseFile(WalletFile);
      writeln('Saved!');  //Saved what? You just ERASED the file (and crashed the program)
  end;
END;

Bart

captian jaster

  • Guest
Re: Unit error
« Reply #3 on: March 04, 2010, 07:32:35 pm »
you keep saying i dont need a loop there and i dont get it.. explain

Peter_Vadasz

  • New Member
  • *
  • Posts: 35
Re: Unit error
« Reply #4 on: March 04, 2010, 08:12:40 pm »
my compiler says begin expected but end. found

I think you need a pascal book, download this (http://www.marcocantu.com/epascal/). I hope it will help you to understand the pascal programming.

ps. Sorry for my english.
OS: Ubuntu 12.04.2 32 bit
Lazarus: 1.0.8
FPC: 2.6.2

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Unit error
« Reply #5 on: March 05, 2010, 12:19:33 am »
you keep saying i dont need a loop there and i dont get it.. explain

Because in both cases you only want to do one read from, or write to, the walletfile. (At least that is what I guessed from the context of your unit.)
Loops are intended for doing things zero or more times. You just want it done once, so do not use a loop.

Do as Peter_Vadasz says!

Bart

captian jaster

  • Guest
Re: Unit error
« Reply #6 on: March 05, 2010, 09:10:20 pm »
Thanks

 

TinyPortal © 2005-2018