Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 26th, 2006, 03:42 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
Pascal app. feedback

I started programming two weeks ago, and begun with pascal since I've heard that it is a good language to start with. Anyway I made this encryption program and would like some feedback on it. Like are there better ways of doing this and such. Any ideas are appreciated!
/lingon
Attached Files
File Type: zip Encryptor.zip (38.5 KB, 171 views)

Reply With Quote
  #2  
Old April 30th, 2006, 04:41 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,038 Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 8 h 32 m 33 sec
Reputation Power: 330
To begin with, you generally don't want to post code as an attachment unless it is actually too large to post in the text of a message; more importantly, you should not post an attachment containing an executable, period. First off, the executable is of little if any help, as you really can't debug it; unless you are having problems with the executable itself, the important material is the source. Second, there have been more than a few jerks who have tried to get people to run viruses or backdoors that way, and most of the regulars, upon seeing a zipped file, will just ignore the discussion thread rather than deal with that. Finally, the zipped executable takes up a lot more space than the source, and you have to go out of your way to download it; even aside from the virus problem, most readers simply would want to hassle with it.

Second, you generally want to indent you code to match the nesting of the expressions, like so:

Pascal Code:
Original - Pascal Code
  1. program Encryptor;              {Krypterar och avkrypterar meddelanden.}
  2. uses
  3.     crt;
  4. var
  5.    s : string;
  6.    val, tecken : char;
  7.    a, tal, l : byte;
  8.    x : integer;
  9.    f, fi, fis : text;
  10.  
  11.    procedure encode;
  12.    begin
  13.         clrscr;
  14.         assign(f, 'C:SDATA.DAT');
  15.         assign(fi, 'C:IDATA.DAT');
  16.         rewrite(fi);
  17.         rewrite(f);
  18.         writeln('Enter your message below.');
  19.         writeln('-------------------------');
  20.         readln(s);
  21.         l := length(s);
  22.         tal := 0;
  23.         writeln();
  24.         writeln('This is the encoded message.');
  25.         writeln('----------------------------');
  26.         for a := 1 to l do
  27.         begin
  28.             inc(tal);
  29.             tecken := s[tal];
  30.             write(ord(tecken),' ');
  31.             write(f,ord(tecken),' '){H„r skrivs den som string.}
  32.             write(fi,x,ord(tecken),' '); {H„r skrivs den som integer.}
  33.         end;
  34.         close(f);
  35.         close(fi);
  36.         writeln();
  37.         writeln();
  38.         writeln('The encoded message has been saved.');
  39.         writeln();
  40.         writeln('Press ENTER to return to main menu.');
  41.         readln();
  42.    end;
  43.  
  44.    {Man m†ste ha tv† text-filer, en f”r att visa upp som string och}
  45.    {en, fi, som „r en integer eftersom chr(x) kr„ver att x = integer.}
  46.  
  47.    procedure decode;
  48.    begin
  49.         clrscr;
  50.         writeln('Enter the numbers manually or read data from file.');
  51.         writeln('--------------------------------------------------');
  52.         writeln('1:Read from file.');
  53.         readln(val);
  54.         if val = '1' then
  55.         begin
  56.            assign(f, 'C:SDATA.DAT');
  57.            assign(fi, 'C:IDATA.DAT');
  58.            assign(fis, 'C:Message.txt');
  59.            rewrite(fis);
  60.            reset(fi);
  61.            reset(f);
  62.            read(f,s);
  63.            writeln();
  64.            writeln('This is the encoded message.');
  65.            writeln('----------------------------');
  66.            write(s);
  67.            l := length(s);
  68.            writeln();{vet inte varf”r jag m†ste ha tv† tomma :S}
  69.            writeln();
  70.            writeln('This is the decoded message.');
  71.            writeln('----------------------------');
  72.            for a := 1 to l div 2 do
  73.            begin
  74.                inc(tal);
  75.                read(fi,x);
  76.                write(chr(x));
  77.                write(fis,chr(x));
  78.            end;
  79.            close(f);
  80.            close(fi);
  81.            close(fis);
  82.            writeln();
  83.            writeln();
  84.            writeln('The decoded message has been saved.');
  85.            writeln();
  86.            writeln('Press ENTER to return to main menu.');
  87.            readln();
  88.         end;
  89.    end;
  90.  
  91.    procedure info;
  92.    begin
  93.         clrscr;
  94.         writeln('Info');
  95.         writeln('----');
  96.         writeln('Made by lingon');
  97.         writeln();
  98.         writeln('The purpose of this program is to make it easy for you to encrypt messages.');
  99.         writeln();
  100.         writeln('It encrypts by changing the letters into thier respective value using the ANCSIIchart.');
  101.         writeln();
  102.         writeln('I recommend keeping your messages small since strings canït hold more than 255  values.');
  103.         writeln();
  104.         writeln('To encode a message, simply enter the text and hit enter.');
  105.         writeln('Two .DAT files will automatically be created in the folder that the program runsfrom. These contain the encoded message and can be sent to anyone and opend and decoded using this program.');
  106.         writeln();
  107.         writeln('To decode a message, simply enter the numbers in the message. Be sure to enter  them as they are, in pairs or threes, otherwise the computer wont know wich number belongs to wich.');
  108.         Writeln('You can also open the .DAT files made when encoding a message, the program will then decode the data stored within and write it down in a .TXT file.');
  109.         writeln();
  110.         writeln('Press ENTER to return to main menu.');
  111.         readln(val);
  112.    end;
  113.  
  114.    procedure password;
  115.    begin
  116.         clrscr;
  117.         write('Enter password:');
  118.         readln(s);
  119.         if s = '1798' then
  120.         begin
  121.            writeln('Acses graanted. Welcome.');
  122.            delay(1500);
  123.            exit;
  124.         end
  125.         else
  126.         begin
  127.              writeln('Acses denied. Shuting down.');
  128.              delay(1500);
  129.              halt;
  130.         end;
  131.    end;
  132.  
  133.  
  134. begin
  135.      password;
  136.      repeat
  137.            clrscr;
  138.            Writeln('*****Encryptor by lingon*****');
  139.            Writeln('-----------------------------');
  140.            writeln('1:Encode Message');
  141.            writeln('2:Decode Message');
  142.            writeln('3:Info');
  143.            writeln('4:Exit');
  144.            readln(val);
  145.            if val = '1' then
  146.               Encode;
  147.            if val = '2' then
  148.               Decode;
  149.            if val = '3' then
  150.               info;
  151.            if val = '4' then
  152.               halt
  153.            else
  154.               writeln('Invalid input.');
  155.      until (val='3');
  156. end.


While it may seem a bit odd at first, you'll find that it makes a tremendous difference in the readability of the code once you get used to it. If you use an editor or IDE that recognizes Pascal code (e.g., Emacs, Scite, vim, Zeus Edit, EditPad Pro, Delphi, Dev-Pascal, Dr Pascal, etc.), it should indent the code for you, or even allow you to indent a section of existing code automatically.

Third, for the menu part in the main program block, since val is a simple scalar variable whose value you are testing against a specific list of possible values, you might want to use a case statement instead of the successive ifs:
Pascal Code:
Original - Pascal Code
  1.            case val of
  2.                '1':
  3.                   Encode;
  4.                '2':
  5.                   Decode;
  6.                '3':
  7.                   info;
  8.                '4':
  9.                   halt;
  10.                default:
  11.                   writeln('Invalid input.');
  12.            end;


This might make it clearer and easier to change later.

Fourth, you are using global variables for everything, rather than local ones; while it is not wrong per se, this is usually considered a bad practice. You should try to have the variable you are using visible in only the parts of the program that use them. You can also use arguments to further reduce the need for globals, by allowing you to pass values to a procedure or function without making them visible elsewhere. Using the scoping rules for nested procedures can also let you share variables between procedures without making them global in scope.

(On a side note: using lowercase letter 'l' as a variable name is a Bad Thing, since in some fonts it can too easily be mistaken for either the capitalized 'I' or the numeral '1'; the latter in particular has considerable potential for confusion.)

Lastly (for now), you might want to decompose the program into smaller pieces; specifically, it is a good idea to separate the user interface code from the actual computation code. In Pascal, you specifically can nest procedures and functions inside of each other, which makes it easy to break down a single procedure into many sub-components. You could, for example, do something like:
Pascal Code:
Original - Pascal Code
  1.    procedure password;
  2.         var
  3.            s: string;
  4.  
  5.         function confirmed(s : string): boolean;
  6.         begin
  7.             confirm := (s = '1798');
  8.         end;
  9.  
  10.     begin
  11.         clrscr;
  12.         write('Enter password:');
  13.         readln(s);
  14.         if confirmed(s) then
  15.         begin
  16.             writeln('Access granted. Welcome.');
  17.             delay(1500);
  18.             exit;
  19.         end
  20.         else
  21.         begin
  22.             writeln('Access denied. Shutting down.');
  23.             delay(1500);
  24.             halt;
  25.         end;
  26.    end;


This particular example is trivial, but with some thought you'll see how this can be applied more generally.

I'll try to take a closer look at things later.
Comments on this post
Yegg` agrees!
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : April 30th, 2006 at 10:39 AM.

Reply With Quote
  #3  
Old April 30th, 2006, 10:35 AM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 12
Okay, thanks alot dude. I will definetly follow those tips.