Recent

Author Topic: need help with creating a file like this  (Read 13471 times)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: need help with creating a file like this
« Reply #30 on: October 13, 2015, 01:13:32 am »
Yes, finally, you found the missing bits :)

Now look at this
for C := 1 to Camp do

Do you think you're writing the correct fields with this loop? Like I said before, the loops need to almost be identical.

Otherwise, write the fields you're about to write to display and you'll see you might have the wrong fields if one of then is not filled.

Did your show.pas also crash for a file created with 4 fields but 0 contacts?

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #31 on: October 13, 2015, 01:27:01 am »
Code: Pascal  [Select][+][-]
  1. Writeln ('Ingrese los ',NR,' contactos');    //datos contactos concretos
  2.      for J := 1 to NR do
  3.      begin
  4.           for I := 1 to N do
  5.           begin
  6.                Writeln ('Ingrese:', Fields[I].Fieldname);
  7.                ReadLn (Rec[I].FieldText);
  8.                if Length (Rec[I].FieldText) <> 0 then begin
  9.                  Camp:=(Camp+1);
  10.                  end;
  11.           end;
  12.  
  13.           myWriteWord(Camp);
  14.  
  15.           for C := 1 to Camp do
  16.           begin
  17.                Rec[C].Fieldnr := C;
  18.                myWriteWord   ( Rec[C].Fieldnr );
  19.                if Length (Rec[C].FieldText) <> 0 then begin
  20.                myWriteString ( Rec[C].FieldText );
  21.                end;
  22.           end;
  23.           Camp := 0 ;
  24.      end;

Quote
Did your show.pas also crash for a file created with 4 fields but 0 contacts?

did not test that yet.
still cant show this .dat. I'm missing something.
« Last Edit: October 13, 2015, 01:38:42 am by GMP_47 »

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: need help with creating a file like this
« Reply #32 on: October 13, 2015, 10:29:16 am »
still cant show this .dat. I'm missing something.

You're almost there :)
Now look at this
for C := 1 to Camp do

Do you think you're writing the correct fields with this loop? Like I said before, the loops need to almost be identical.

Otherwise, write the fields you're about to write to display and you'll see you might have the wrong fields if one of then is not filled.


Also, look closely what you're doing here:
Code: Pascal  [Select][+][-]
  1.    myWriteWord   ( Rec[C].Fieldnr );
  2.    if Length (Rec[C].FieldText) <> 0 then begin
  3.      myWriteString ( Rec[C].FieldText );
  4.    end;
Do you think this is correct, if Length(FieldText) <> 0, that you're writing Fieldnr ??


Maybe another observation. You make use of lots of whitespace in front of the lines. Normally indenting 2 spaces per block is sufficient. You're also not indenting everywhere correctly (like in the part I showed above). Indenting can be very important to help spot potential errors.
« Last Edit: October 13, 2015, 10:40:19 am by rvk »

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #33 on: October 13, 2015, 06:06:00 pm »
Code: Pascal  [Select][+][-]
  1. for I := 1 to N do
  2.           begin
  3.                Writeln ('Ingrese:', Fields[I].Fieldname);
  4.                ReadLn (Rec[I].FieldText);
  5.                if Length (Rec[I].FieldText) <> 0 then begin
  6.                  Camp:=(Camp+1);
  7.                  end;
  8.           end;
  9.  
  10.           myWriteWord(Camp);
  11.  
  12.           for C := 1 to Camp do
  13.           begin
  14.                Rec[C].Fieldnr := C;
  15.                myWriteWord   ( Rec[C].Fieldnr );
  16.                if (Length(Rec[C].FieldText)) <> 0 then begin
  17.                myWriteString ( Rec[C].FieldText );
  18.                end;
  19.           end;
  20.           Camp := 0 ;

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: need help with creating a file like this
« Reply #34 on: October 13, 2015, 06:11:45 pm »
Code: Pascal  [Select][+][-]
  1.  
And what did you change from your previous code.
If I compare the code from your last post and the one before that, YOU DIDN'T CHANGE A THING.

(You just put ( and ) around one statement, which does nothing)

Read my last post.
I won't reply to code-only posts because I'm starting to think you're just throwing code at us hoping we fix it.

(And that while you're just 3 keystrokes from completing your code  ;D)
« Last Edit: October 13, 2015, 06:33:43 pm by rvk »

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: need help with creating a file like this
« Reply #35 on: October 13, 2015, 07:07:30 pm »
Following the code from day one, In general either this code don't belong to you or you really don't know what you want to achieve. Firstly, get some basic info about pascal. No one will solve the entire code. Everyone here is to show the path to someone, not to take anyone to destination.

Sorry for being harsh, But be precise.
Thank you, Happy Coding. 
Holiday season is online now. :-)

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #36 on: October 13, 2015, 09:35:25 pm »
Code: Pascal  [Select][+][-]
  1. for C := 1 to Camp do
  2.           begin
  3.                Rec[C].Fieldnr := C;
  4.                myWriteWord   ( Rec[C].Fieldnr );
  5.                if Length(Rec[C].FieldText) <> '0' then begin
  6.                myWriteString ( Rec[C].FieldText );
  7.                end;
  8.           end;
I dont know.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: need help with creating a file like this
« Reply #37 on: October 13, 2015, 09:51:33 pm »
Code: Pascal  [Select][+][-]
  1. if Length(Rec[C].FieldText) <> '0' then begin
This won't even compile.
You're comparing Length(), which results in a number, with a character.
If you think this is correct you don't understand Pascal.
And if you don't see that this is an error you don't understand Pascal.
Didn't you try compiling this? Didn't you see it gave an error?
Couldn't you solve that error before posting this code?????

The only thing, other then not completing this assignment, is remove the complete "for Camp" loop and start that part over.

Look at what you've got at that point (at the point of "for Camp").
You have the following data:
Code: [Select]
Rec[I].Fieldnr
Rec[I].FieldText
(where I = 1 to N)

So potentially you have (in case of N=4)
Code: [Select]
Rec[1].Fieldnr
Rec[1].FieldText
Rec[2].Fieldnr
Rec[2].FieldText
Rec[3].Fieldnr
Rec[3].FieldText
Rec[4].Fieldnr
Rec[4].FieldText
You need to write all Rec[ x ].Fieldnr and Rec[ x ].FieldText to disk IF (and only IF) Rec[ x ].FieldText <> '' (or Length(Rec[ x ].FieldText) <> 0 which is the same).

So you need to construct a loop where you do that.
After that your assignment is complete. You're so close.

You should be able to create this loop.
« Last Edit: October 13, 2015, 09:53:46 pm by rvk »

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with creating a file like this
« Reply #38 on: October 14, 2015, 04:45:25 am »
Code: Pascal  [Select][+][-]
  1. for C := 1 to Camp do
  2.           begin
  3.                Rec[C].Fieldnr := C;
  4.                if Length(Rec[C].FieldText) <> 0 then begin
  5.                myWriteWord   ( Rec[C].Fieldnr );
  6.                myWriteString ( Rec[C].FieldText );
  7.                end;
the file goes bananas in SHOW.pas (show works fine).
notice that the first contact "juan" and "aaaa" were just fine.
« Last Edit: October 14, 2015, 04:48:09 am by GMP_47 »

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: need help with creating a file like this
« Reply #39 on: October 14, 2015, 04:54:28 am »
Code: Pascal  [Select][+][-]
  1. for C := 1 to Camp do
You're still not looping all N records. You keep using Camp here but Camp is only filled with the number of filled in records (which are not empty). In this loop you need to loop all the records again because you leave some out with the if statement.

Look at that loop. If Camp is 2 (for instance if field 1 and 3 are filled in) you loop through record 1 and 2. You don't even reach record 3 and 4. So you need to loop through all the records again here.

That's why I suggested you removed the complete loop and start over, in the hopes you would have seen that yourself.
« Last Edit: October 14, 2015, 04:57:45 am by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with creating a file like this
« Reply #40 on: October 14, 2015, 05:00:19 am »
What's wrong with the simple use of writing out the values of the variables in case you don't understand the program flow.

It's the best learning school (well you better use stepping inside a debugger, but since OP keeps insisting on using devpas).

just 2 cents

 

TinyPortal © 2005-2018