Recent

Author Topic: Need to create a text file to save data in...  (Read 1409 times)

PascalIsHard

  • Newbie
  • Posts: 3
Need to create a text file to save data in...
« on: January 15, 2020, 12:05:34 am »
I tried writing and programming a pascal program to work on the pascal compiler. So, basically I need to create a survey and save the data to a text file and the program should be able to sort, search and edit a field in the survey... This is so hard.


jamie

  • Hero Member
  • *****
  • Posts: 2309
Re: Need to create a text file to save data in...
« Reply #1 on: January 15, 2020, 12:57:52 am »
First you need a menu screen..

This menu screen will call a procedure in a CASE statement to which will be executed via the user's selection.
 
 Each selection will have a Procedure that could ask even more questions from the user to forfill the task.

 WRITELN, READLN would be those you use to query the users MIND!..

 Next would be such things as talking to your file system or Cloud etc..

 
 How much of your assignment have you gotten done thus far ?

 I am sure your class studies covered most of this, that is if you were awake in class!


 Please report back when you can show some compilable code!
Number 1 at blue screen app creations!

trev

  • Sr. Member
  • ****
  • Posts: 398
  • Former Delphi 7 and Delphi 10.2 User
Re: Need to create a text file to save data in...
« Reply #2 on: January 15, 2020, 03:21:50 am »
So, basically I need to create a survey and save the data to a text file and the program should be able to sort, search and edit a field in the survey... This is so hard.

You can conquer any complex program by breaking it down into smaller parts (functions, procedures). While that may seem like a slow process, I often create the stub functions and procedures that do nothing but write something to the terminal or screen, and then go back and implement the required functionality, function/procedure by function/procedure. In no time you have a compete working complex program... and this approach often means little or no debugging because you have created and tested each function/procedure as you wrote it.

In this case "create a survey" is a complex task that needs to be broken down into smaller tasks. For example: (1) Ask a question; (2) Store the answer; (3) Ask the next question depending on the answer to the previous question, etc.

(1) Ask a question may need to be broken down into smaller tasks too. Is the question (A) open ended requiring the user to write an answer in a TMemo or is the question (B) multiple choice requiring the user to select an answer from a list.

Hopefully you get the idea now. No task is so complex that it cannot be broken down into smaller, more manageable tasks.
o Lazarus v2.1.0 r62452, FPC v3.3.1 r42644, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

PascalIsHard

  • Newbie
  • Posts: 3
Re: Need to create a text file to save data in...
« Reply #3 on: January 16, 2020, 12:20:22 am »
Here is the code for it... so basically I have to save all the records of the answers into a text file... then sort them alphabetically or by size. i also have to be able to search and edit a record through the compiler

program ShoeInventoryRecords;
type
Shoes = record
   tag: packed array [1..50] of char;
   brand: packed array [1..50] of char;
   category: packed array [1..50] of char;
   price: longint;
end;

var
   shoe1, shoe2: Shoes; (* Declare shoe1 and shoe2 of type Shoes *)

(* procedure declaration *)
procedure printshoe( var shoe: Shoes );

begin
   (* print shoe info *)
   writeln ('shoe  tag : ', shoe.tag);
   writeln('shoe  brand : ', shoe.brand);
   writeln( 'shoe  category : ', shoe.category);
   writeln( 'shoe price : ', shoe.price);
end;

begin
   (* shoe 1 specification *)
   shoe1.tag  := 'Jordan One';
   shoe1.brand := 'Nike ';
   shoe1.category := 'Basketball';
   shoe1.price := 110;
   
   (* shoe 2 specification *)
   shoe2.tag := 'Superstar';
   shoe2.brand := 'Adidas';
   shoe2.category := 'Casual';
   shoe2.price := 75;
   
   (* print shoe1 info *)
   printshoe(shoe1);
   writeln;

   (* print shoe2 info *)
   printshoe(shoe2);
end.

Bart

  • Hero Member
  • *****
  • Posts: 3627
    • Bart en Mariska's Webstek
Re: Need to create a text file to save data in...
« Reply #4 on: January 16, 2020, 06:15:45 pm »
Some observations:

  • A plain text file doesn't sound like the most logical fil-type to store this kind of information.
  • price: longint; how expensive are shows going to be?  O:-)  type Currency would make more sense.
  • packed array of char; You're not allowed to use string?
  • Any thougt on how you would implement sorting? Can you give some sort of algorithm for that?

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 2380
Re: Need to create a text file to save data in...
« Reply #5 on: January 16, 2020, 06:57:55 pm »
  • price: longint; how expensive are shows going to be?  O:-)  type Currency would make more sense.

Currency is not shorter than LongInt, is it? ;D
Also, since it's (treated as) a Real type, it may be costly in time.

It should be used (or some oter Real) because prices can have decimals, not because it's "smaller" ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 3344
  • My goal: build my own game engine using Lazarus
Re: Need to create a text file to save data in...
« Reply #6 on: January 16, 2020, 07:18:39 pm »
So, basically I need to create a survey and save the data to a text file and the program should be able to sort, search and edit a field in the survey... This is so hard.

I think it will be not so hard if you use database and data aware controls:
https://lazarus-ccr.sourceforge.io/docs/lcl/dbctrls/howtousedataawarecontrols.html

But if you want to learn the basic of accessing the classic text and binary file method, I ever wrote some demos, maybe you can learn something from there:
https://forum.lazarus.freepascal.org/index.php/topic,45911.msg325205.html#msg325205

Bart

  • Hero Member
  • *****
  • Posts: 3627
    • Bart en Mariska's Webstek
Re: Need to create a text file to save data in...
« Reply #7 on: January 16, 2020, 07:19:03 pm »
It should be used (or some oter Real) because prices can have decimals, not because it's "smaller" ;)

Exactly, and I should have stated that.
At least with Currency shoes can become really expensive.

Bart

PascalIsHard

  • Newbie
  • Posts: 3
Re: Need to create a text file to save data in...
« Reply #8 on: January 17, 2020, 02:13:27 am »
I changed the code a little by putting real instead of longit...

program ShoeInventoryRecords;
type
Shoes = record
   tag: packed array [1..50] of char;
   brand: packed array [1..50] of char;
   category: packed array [1..50] of char;
   material: packed array [1..50] of char;
   classification: packed array [1..50] of char;
   colour: packed array [1..50] of char;
   size: longint;
   price: real;
end;

var
   shoe1, shoe2,shoe3, shoe4: Shoes; (* Declare shoe1 and shoe2 of type Shoes *)

(* procedure declaration *)
procedure printshoe( var shoe: Shoes );

begin
   (* print shoe info *)
   writeln ('shoe  tag : ', shoe.tag);
   writeln('shoe  brand : ', shoe.brand);
   writeln( 'shoe  category : ', shoe.category);
   writeln( 'shoe  material : ', shoe.material);
   writeln( 'shoe  classification : ', shoe.classification);
   writeln( 'shoe  colour : ', shoe.colour);
   writeln( 'shoe  size : ', shoe.size);
   writeln( 'shoe price : ', shoe.price);
end;

begin
   (* shoe 1 specification *)
   shoe1.tag  := 'Jordan One';
   shoe1.brand := 'Nike ';
   shoe1.category := 'Basketball';
   shoe1.material := 'leather';
   shoe1.classification := 'light';
   shoe1.colour := 'Red';
   shoe1.size := 8;
   shoe1.price := 110;
   
   (* shoe 2 specification *)
   shoe2.tag := 'Superstar';
   shoe2.brand := 'Adidas';
   shoe2.category := 'Casual';
   shoe2.material := 'leather';
   shoe2.classification := 'light';
   shoe2.colour := 'white';
   shoe2.size := 7;
   shoe2.price := 75;

   (* shoe 3 specification *)
   shoe3.tag := 'LeBron Soldiers';
   shoe3.brand := 'Nike';
   shoe3.category := 'Basketball';
   shoe3.material := 'Leather and cotton';
   shoe3.classification := 'light';
   shoe3.colour := 'white';
   shoe3.size := 11;
   shoe3.price := 140;

   (* shoe 4 specification *)
   shoe4.tag := 'Superstar';
   shoe4.brand := 'Adidas';
   shoe4.category := 'Casual';
   shoe4.material := 'leather';
   shoe4.classification := 'light';
   shoe4.colour := 'white';
   shoe4.size := 7;
   shoe4.price := 75;




   
   (* print shoe1 info *)
   printshoe(shoe1);
   writeln;

   (* print shoe2 info *)
   printshoe(shoe2);
   writeln;

   printshoe(shoe3);
   writeln;

   printshoe(shoe4);
   writeln;
readln;
end.



----------------------------------------------------------------------------------------------------------------------------------------------------
this is the outcome, i cant remember how to only get 2 or 3 decimal places only.


shoe  tag : Jordan One
shoe  brand : Nike
shoe  category : Basketball
shoe  material : leather
shoe  classification : light
shoe  colour : Red
shoe  size : 8
shoe price :  1.100000000000000E+002

shoe  tag : Superstar
shoe  brand : Adidas
shoe  category : Casual
shoe  material : leather
shoe  classification : light
shoe  colour : white
shoe  size : 7
shoe price :  7.500000000000000E+001

shoe  tag : LeBron Soldiers
shoe  brand : Nike
shoe  category : Basketball
shoe  material : Leather and cotton
shoe  classification : light
shoe  colour : white
shoe  size : 11
shoe price :  1.400000000000000E+002

shoe  tag : Superstar
shoe  brand : Adidas
shoe  category : Casual
shoe  material : leather
shoe  classification : light
shoe  colour : white
shoe  size : 7
shoe price :  7.500000000000000E+001
----------------------------------------------------------------------------------------------------------------------------------------------------

btw, this would be the sorting algorithm from another code i had written previously, i wanted to somehow implement it into the code. Unfortunately don't know-how.

program sort1;

var
sort:array[1..5] of integer;
i,y,x,irand1,irand2,irand3,irand4,irand5,temp:integer;

begin

randomize;
irand1:= random(10);
irand2:= random(10);
irand3:= random(10);
irand4:= random(10);
irand5:= random(10);
sort[1]:=irand1;
sort[2]:=irand2;
sort[3]:=irand3;
sort[4]:=irand4;
sort[5]:=irand5;

for x:= 1 to 5 do
begin
write (sort
  • );

end;
writeln;


for i:=1 to 5 do
begin
    for y:= 1 to 4 do
    begin
     if sort[y]>sort[y+1] then
     begin
        temp:=sort[y];
        sort[y]:=sort[y+1];
        sort[y+1]:=temp;
       
     end; 
    end;   
   
for x:= 1 to 5 do
begin
write (sort
  • );

end;
writeln;       
   

end;
readln;
end.

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


winni

  • Hero Member
  • *****
  • Posts: 887
Re: Need to create a text file to save data in...
« Reply #9 on: January 17, 2020, 02:57:21 am »

 i cant remember how to only get 2 or 3 decimal places only.


Hi!

Have a look here:

https://wiki.freepascal.org/Formatting_output


Winni

jamie

  • Hero Member
  • *****
  • Posts: 2309
Re: Need to create a text file to save data in...
« Reply #10 on: January 17, 2020, 03:40:13 am »
if you are allowed, there is a Currency Type
Number 1 at blue screen app creations!

Hartmut

  • Sr. Member
  • ****
  • Posts: 360
Re: Need to create a text file to save data in...
« Reply #11 on: January 17, 2020, 10:53:19 am »
Please first format all your code snippets as "code" as described in the 2nd paragraph of https://wiki.lazarus.freepascal.org/Lazarus_Faq#Using_the_Forum. That makes your code examples much easier for everybody to read.

I will give you some hints for your sorting routine:
 - make it a procedure, not a program
 - declare an array type for your shoes which is big enough for your needs, not [1..5]
 - make your for-loops dependent of this array size. Either use a const for declaring that array type or use High() to determine the highest index of your array
 - don't sort an array of integer, but sort an array of your shoes
 - there is a bug in your sorting algorithm: you compare neighbors and swap them, if they have a bad order. But if you did this once, you must repeat your sorting main loop. So you need a flag, which contains if you ever swapped 2 neighbors, and when this flag is true, you must always repeat your main loop.
« Last Edit: January 17, 2020, 10:55:07 am by Hartmut »

jamie

  • Hero Member
  • *****
  • Posts: 2309
Re: Need to create a text file to save data in...
« Reply #12 on: January 17, 2020, 10:41:56 pm »
A little help for him..

Code: Pascal  [Select]
  1. (* procedure declaration *)
  2. procedure printshoe( var shoe: Shoes );
  3.  
  4. begin
  5.    (* print shoe info *)
  6.    writeln ('shoe  tag : ', shoe.tag);
  7.    writeln('shoe  brand : ', shoe.brand);
  8.    writeln( 'shoe  category : ', shoe.category);
  9.    writeln( 'shoe  material : ', shoe.material);
  10.    writeln( 'shoe  classification : ', shoe.classification);
  11.    writeln( 'shoe  colour : ', shoe.colour);
  12.    writeln( 'shoe  size : ', shoe.size);
  13.    writeln( 'shoe price : ', shoe.price);
  14. end;
  15.  
  16. begin
  17.    (* shoe 1 specification *)
  18.    shoe1.tag  := 'Jordan One';
  19.    shoe1.brand := 'Nike ';
  20.    shoe1.category := 'Basketball';
  21.    shoe1.material := 'leather';
  22.    shoe1.classification := 'light';
  23.    shoe1.colour := 'Red';
  24.    shoe1.size := 8;
  25.    shoe1.price := 110;
  26.    
  27.    (* shoe 2 specification *)
  28.    shoe2.tag := 'Superstar';
  29.    shoe2.brand := 'Adidas';
  30.    shoe2.category := 'Casual';
  31.    shoe2.material := 'leather';
  32.    shoe2.classification := 'light';
  33.    shoe2.colour := 'white';
  34.    shoe2.size := 7;
  35.    shoe2.price := 75;
  36.  
  37.    (* shoe 3 specification *)
  38.    shoe3.tag := 'LeBron Soldiers';
  39.    shoe3.brand := 'Nike';
  40.    shoe3.category := 'Basketball';
  41.    shoe3.material := 'Leather and cotton';
  42.    shoe3.classification := 'light';
  43.    shoe3.colour := 'white';
  44.    shoe3.size := 11;
  45.    shoe3.price := 140;
  46.  
  47.    (* shoe 4 specification *)
  48.    shoe4.tag := 'Superstar';
  49.    shoe4.brand := 'Adidas';
  50.    shoe4.category := 'Casual';
  51.    shoe4.material := 'leather';
  52.    shoe4.classification := 'light';
  53.    shoe4.colour := 'white';
  54.    shoe4.size := 7;
  55.    shoe4.price := 75;
  56.  
  57.  
  58.  
  59.  
  60.    
  61.    (* print shoe1 info *)
  62.    printshoe(shoe1);
  63.    writeln;
  64.  
  65.    (* print shoe2 info *)
  66.    printshoe(shoe2);
  67.    writeln;
  68.  
  69.    printshoe(shoe3);
  70.    writeln;
  71.  
  72.    printshoe(shoe4);
  73.    writeln;
  74. readln;
  75. end.
  76.  
  77.  

You need to select that little "#" pound symbol just above your edit window in the forum.
 
You will see two sets of backets, first set starts the block and the last set ends the block, PASTE your code between those two sets of [ // ] Paste code here [/];

Number 1 at blue screen app creations!

Bart

  • Hero Member
  • *****
  • Posts: 3627
    • Bart en Mariska's Webstek
Re: Need to create a text file to save data in...
« Reply #13 on: January 17, 2020, 10:54:22 pm »
Look up BubbleSort in Google (that's what you are trying to implement).
(Note that it possibly is the slowest sorting algorithm that exists.)

Bart

Bart

  • Hero Member
  • *****
  • Posts: 3627
    • Bart en Mariska's Webstek
Re: Need to create a text file to save data in...
« Reply #14 on: January 17, 2020, 11:01:51 pm »
Since this is obviously homework: some things that will make your teacher happy:
  • Typenames are often prepended with a capital T, also the typename Shoes, suggest the type itself can hold multiple shoes. So name the type TShoe
  • Have a type TShoes: array[1..somenumber] of TShoe
  • Declare somenumber (give it a meaningfull name) as a constant (above the typedefinition of TShoes).
  • Declare the variable Shoes of type TShoes, instead of having Shoes1, Shoes2 etc.

Bart