* * *

Author Topic: I did something wrong here i think  (Read 3542 times)

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
I did something wrong here i think
« on: March 08, 2010, 10:43:10 pm »
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Menus, CRT;

type

  { TForm1 }

  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    BWallet: TMenuItem;
    OpenWallet: TMenuItem;
    EditWallet: TMenuItem;

    procedure OpenWalletClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

Type
 STR = string;
 Walletrec = Record
 Balance,Addto,Remove : STR;
 WFile : Text;
 end;

var
  Form1: TForm1;
  WalletV : WalletRec;
  ReadSTR : string;
implementation

{ TForm1 }



procedure TForm1.OpenWalletClick(Sender: TObject);
BEGIN
  ClrScr;
  Writeln('Please Wait...');
  AssignFile(WalletV.WFile,'Wallet.dat');
  While NOT FIleexists('Wallet.Dat') Do
  begin
    ClrScr;
    Writeln('You Do Not Have a wallet yet!');
    writeln('Input your current wallet balance');
    Readln(WalletV.Balance);
    Rewrite(WalletV.WFile);
    writeln(WalletV.WFile,WalletV.Balance);
    Closefile(WalletV.WFile);
    writeln('Your Wallet Has been saved!');
    Delay(1000);
    ClrScr;
  end;
  ClrScr;
  Reset(WalletV.WFile);
  writeln('Current Balance:');
  While NOT EoF(WalletV.WFile) Do
  begin
    Readln(WalletV.WFile,ReadSTR);
    writeln(ReadSTR);
  end;
END;

initialization
  {$I unit1.lrs}

end.   

If i click the Open wallet windows fails to respond
-Captain Jaster

Martin_fr

  • Hero Member
  • *****
  • Posts: 1116
Re: I did something wrong here i think
« Reply #1 on: March 08, 2010, 11:11:28 pm »
writeln and readeln (unless given a file descriptor) goes to the console (the dos like window).

Since I see Form1 => you have a GUI (Graphical User Interface) application.
By default a GUI app does not have a console, so your readln/writeln can not work.

You can go to the project or compiler option, and enable a console => but that is pointless.

You want to but some TEdit and TLabel on your form, where the user must input.... (or better pop up a 2nd form, but that's for later)

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #2 on: March 08, 2010, 11:50:56 pm »
Could you explain how to use the TEdit and Tlabel
Thanks
-Captain Jaster

Troodon

  • Sr. Member
  • ****
  • Posts: 483
Lazarus/FPC on Linux

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #4 on: March 09, 2010, 12:44:25 am »
what im having trouble understanding is how do i put a text in the program with that code? can i have an example...
-Captain Jaster

pik33

  • New member
  • *
  • Posts: 37
Re: I did something wrong here i think
« Reply #5 on: March 09, 2010, 08:21:43 am »
Put an Edit control on your form (Edit1)
Put a Label control above Edit (Label1)
Add a button with caption "Ok" (Button1)

then instead
   writeln('Input your current wallet balance');

use: label1.caption:='Input your current wallet balance';

Then write code for  Button1.click:

Walletv.Balance:=Edit1.text;

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

Now, user have to input a string in Edit 1 and click "Ok" button

Ñuño_Martínez

  • Sr. Member
  • ****
  • Posts: 297
    • Burdjia
Re: I did something wrong here i think
« Reply #6 on: March 09, 2010, 10:49:07 am »
I'd said you in the other thread, but I'll post it here also.

I think it is a good idea you read some of these tutorials. They'll teach you a lot of things. :)

Bart

  • Hero Member
  • *****
  • Posts: 752
    • Bart en Mariska's Webstek
Re: I did something wrong here i think
« Reply #7 on: March 09, 2010, 12:23:11 pm »
Code: [Select]
procedure TForm1.OpenWalletClick(Sender: TObject);
BEGIN
  ClrScr;
  Writeln('Please Wait...');
  AssignFile(WalletV.WFile,'Wallet.dat');
  While NOT FIleexists('Wallet.Dat') Do
  begin
    ClrScr;
    Writeln('You Do Not Have a wallet yet!');
    writeln('Input your current wallet balance');
    Readln(WalletV.Balance);
    Rewrite(WalletV.WFile);
    writeln(WalletV.WFile,WalletV.Balance);
    Closefile(WalletV.WFile);
    writeln('Your Wallet Has been saved!');
    Delay(1000);
    ClrScr;
  end;
  ClrScr;
  Reset(WalletV.WFile);
  writeln('Current Balance:');
  While NOT EoF(WalletV.WFile) Do
  begin
    Readln(WalletV.WFile,ReadSTR);
    writeln(ReadSTR);
  end;
END;

As I have stated before, in another thread by you, commenting on almost the same code: there is no need to use loops (while..do) here.
You said you understood, but you give us no signs at all that you actually do anything with the advice we gave you.

So, for the last time.
Get a good (e-)book on Pascal and/or Delphi.
Start with the very basics of the language, then learn your way up to more difficult tasks.
Show us that you can progress, and we will gladly help.

For now, helping you seems to be a dead end.

Bart

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #8 on: March 09, 2010, 01:27:17 pm »
I dont recall saying i understand what you mean and i really dont get it
Thats how i learned to read from files and it works fine. and thanks you guys for being a big help!
-Captain Jaster

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #9 on: March 09, 2010, 01:51:27 pm »
Code: [Select]
unit Mantrixs;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Menus, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    MainMenu1: TMainMenu;
    WalletN: TMenuItem;
    OpenWallet: TMenuItem;
    EditWallet: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure OpenWalletClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

Type
 Str = String;
 Walletrec = Record
 WFile,Balance,Addto,Remove : Str;
 end;

var
  Form1: TForm1;
  WalletV : Walletrec;
implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

procedure TForm1.OpenWalletClick(Sender: TObject);
begin
  Label1.Caption := 'Input your current balance';
  Button1.Click;
  walletv.balance := 'Edit1.txt';
end;

initialization
  {$I Mantrixs.lrs}

end.
       
Thanks and all, but i have this problem... I Compiled this loaded it and the button caption and everything else came up on the Main menu, not the Open wallet... And it was all crowded together >.<.
-Captain Jaster

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #10 on: March 09, 2010, 03:00:03 pm »
Never mind
-Captain Jaster

Troodon

  • Sr. Member
  • ****
  • Posts: 483
Re: I did something wrong here i think
« Reply #11 on: March 09, 2010, 03:35:15 pm »
I doubt you can learn ObjectPascal from scratch just by posting questions to this forum. Reading those tutorials is actually a very good suggestion.
Lazarus/FPC on Linux

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: I did something wrong here i think
« Reply #12 on: March 09, 2010, 06:28:04 pm »
I have been reading from the wiki, so thanks and i have a pdf about essential pascal or something like that...
Im going to do little by little programs and post screen shots has i go along.
-Captain Jaster

Troodon

  • Sr. Member
  • ****
  • Posts: 483
Re: I did something wrong here i think
« Reply #13 on: March 09, 2010, 08:10:26 pm »
Since you mentioned you already have a copy of Marco Cantu's "Essential Pascal", have a look at "Essential Delphi" (http://groups.google.com/group/marco_cantu/files). Then I would suggest you google up the PDF of the Delphi 5/7 Developers's guide. Good luck!
Lazarus/FPC on Linux

Bart

  • Hero Member
  • *****
  • Posts: 752
    • Bart en Mariska's Webstek
Re: I did something wrong here i think
« Reply #14 on: March 09, 2010, 11:38:27 pm »
Thats how i learned to read from files and it works fine.

How you read/write files depends on the context: what are actually wanting to achieve.

Code: [Select]
var
  F: TextFile;
  S: String;

begin
  AssignFile(F,'test.txt');
  Reset(F); //Open F for reading only
  while not EOF(F) do //as long as we are not at the end of the file do:
  begin
    readln(F,S); //read line from file
    writeln(S); //display line on the console
  end;
  CloseFile(F);
  ...
  AssignFile(F,'test2.txt');
  //the rewrite() wil create the file and ope it for writing.
  //If the file already existed it will erase and then overwrite it's contents
  Rewrite(F); 
  repeat
    write('Enter new line (empty line quits): ');
    readln(S);  //read input from user
    writeln(F,S);  //write input from user to file
  until S = '';  //until user enters an empty string
end;

First I read and display all lines in the file 'test.txt'.
For this I need a loop, since I do not know how many lines, if at all, there are in the file.

Second I create a file 'test2.txt' and ask the user what to write in it.
Again I need a loop, to give the user the opportunity to enter as few or much lines as he wants.

So in both cases I need a loop.

Now take a look at this (slightly simplified) piece of code from you:

Code: [Select]
  While NOT FIleexists('Wallet.Dat') Do
  begin
    Writeln('You Do Not Have a wallet yet!');
    writeln('Input your current wallet balance');
    Readln(WalletV.Balance);
    Rewrite(WalletV.WFile);
    writeln(WalletV.WFile,WalletV.Balance);
    Closefile(WalletV.WFile);
    writeln('Your Wallet Has been saved!');
  end;

Using the while loop here implies that you expect the the value of FileExists('Wallet.dat') to change to true, but you do not know when this will happen.
By itself that is not always illogical, but in this case it is.

When you do
Code: [Select]
  rewrite(WalletV.WFile);

You will create the file 'Wallet.dat'. If that would not be true you have an IO Error that you must deal with (possibly the file already exists and is read-only).

Let's say the rewrite() is succesfull. Now we know that FileExists('Wallet.Dat') is True in the second evaluation of the loopcondition and the code will not be run again. And all will be fine.

But if rewrite() fails, FileExists() will be False in the second evaluation of FileExists() and you will again run the exact same code that failed. If rewrite() failed the frist time, it will also fail the second time and so on. You potentially created an endless loop here.
(Not in practice though, since the IO Error that made rewrite() fail, will also crash your program with a runtime error)

A more logical approach would be in this case:

Code: [Select]
  ...
  if not FileExists('Wallet.Dat') then
  begin
    //code here to create the file and put initial content in it
    //if an error occurred display it and abandon
  end;
  ...


Also in the situation where you want to read only the first line of a text file you did (in another related thread):

Code: [Select]
  ...
  Reset(WalletFile);
  while not EOF(WalletFile) do
    readln(WalletFile,S);
    //some more code
  end;
  ...

And you expected S to contain the first line in the file.
However if the file holds more than 1 line, S will have the last line in it.

If you only want to read the first line do it like this:

Code: [Select]
  ...
  Reset(WalletFile);
  // EOF might be true, and in that case we will never do a readln, so assign
  //empty value to S
  S := '';
  if not EOF(WalletFile) do
    readln(WalletFile,S);
    //some more code
  end;
  ...
end;

Can you see the difference here between the approaches that require a loop and the approaches that make more sense without a loop? And do you understand why?

Bart

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads