Recent

Author Topic: ASCII Code: the push back of letters?  (Read 2967 times)

Maja199

  • Newbie
  • Posts: 1
ASCII Code: the push back of letters?
« on: November 25, 2015, 10:05:27 pm »
Good evening,

I'm a German student and at the moment we're working with Borland Pascal, an older version of Turbo Pascal.
Hopefully someone here can understand my problem and help me. I don't even know if I'm actual in the right forum.

So we're working on a program in school, with which you can encode letters. There is a screen where you can choose
the letters you want to encode. For example the word "hello". You can type it in and then the program asks if you want
to move the letters and you can type in a number like "4". And then the letters get moved for four positions.. or something
like that. Then, you can decode the word again and there appears a screen with many different symbols and letters and
anywhere in there, there is the word from the beginning.

It looks like this:
#, ', Ä, a, f, w, f, g, H, e, l, l, o, s, §, $, 
->But with much much more rows.

"Buchstabenfeld" means something like the whole ABC

And I have the code here:
Program Caesar1;
Uses WINCRT;
Const C=10;

Var Buchfeld : Array[1..C] of CHAR;
    a, ant : CHAR;
    ok: BOOLEAN;

 Procedure Ein(Var Buchfeld : Array of CHAR);
  Var i : Integer;
  Begin
    For i:=1 to C Do
    Begin
       Writeln('Enter letters.');
       Readln(a);
       Buchfeld:=a;
      End;
  End;
 
 Procedure Aus(Buchfeld: Array of CHAR);
  Var i : Integer;
  Begin
    For i:=1 to C Do
    Write(Buchfeld,', ');
     Write('             ');
  End;
 
Procedure Ver(Var Buchfeld : Array of CHAR);
  Var d, i, z: Integer;
  Begin
       Writeln('Move letters');
       Readln(d);
       For i:=1 To C Do
       Begin
         z:=Ord(Buchfeld);
         If z = 32 Then
         Else
         Begin
           z:=z+d;
           Buchfeld:=CHAR(z);
         End;
       End;
       Aus(Buchfeld);
       Writeln;
     End;
 
 Procedure Rue(Var Buchfeld : Array of CHAR);
  Var d, z, i : Integer;
  Begin
    For d:=1 to 26 Do
     Begin;
    For i:=1 To C Do
     Begin
       z:=Ord(Buchfeld);
       If z = 32 Then
       Else
       Begin
         z:=z-1;
         Buchfeld:=CHAR(z);
       End;
     End;
     Aus(Buchfeld);
    End;
    Writeln;
   End;
 
 Procedure Menue;
  Var ant : CHAR;
  Begin
     Writeln('Which action?');
     Writeln('Read in (E); Encode (V); Decode (R)');
     Readln(ant);
     Case ant of
       'E': Ein(Buchfeld);
       'V': Ver(Buchfeld);
       'R': Rue(Buchfeld);
     End;
   End;

Begin
   ok:=true;
   Repeat
     CLRSCR;
     Menue;
     Writeln('Repeat program? (j, n):');
     Readln(ant);
     If ant in ['j','n'] then ok:=false
     Else ok:=true
   Until ok;
End.


The problem is that this only works for lowercase letters like "a b c". And we need to to get it working with uppercase letters like "A B C" too.
Does anyone know, how to do it? I'm stumped.

Thx for helping :)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: ASCII Code: the push back of letters?
« Reply #1 on: November 25, 2015, 10:54:57 pm »
Your program crashes for me:

Code: [Select]
Which action?
Read in (E); Encode (V); Decode (R)
E
Enter letters.
0
Enter letters.
1
Enter letters.
2
Enter letters.
3
Enter letters.
4
Enter letters.
5
Enter letters.
6
Enter letters.
7
Enter letters.
8
Enter letters.
9
No heap dump by heaptrc unit
Exitcode = 201
Runtime error 201 at $004015B0
  $004015B0  EIN,  line 16 of caesar.lpr
  $00401930  MENUE,  line 74 of caesar.lpr
  $0040197A  main,  line 84 of caesar.lpr
  $00408E01

Line 16 reads:
Code: [Select]
  Buchfeld[i]:=a;

B.t.w. Please place code between code-tags.
Also rather not us "i" as an index variable inside brackets "[""]", because BB code (used by this forum) will see this as "start Italics".

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: ASCII Code: the push back of letters?
« Reply #2 on: November 25, 2015, 11:03:38 pm »
This is odd

Code: [Select]
Begin
   ok:=true;
   Repeat
     CLRSCR;
     Menue;
     Writeln('Repeat program? (j, n):');
     Readln(ant);
     If ant in ['j','n'] then ok:=false
     Else ok:=true
   Until ok;
End.

When I am asked "Repeat program?" and I answer "n", I expect the program to end, but reguardless of wether I say "j" or "n" it repeats the program.

You need to fix that (and the other flaws) before handing in your assignment  >:D

And a suggestion: Do not force the user to enter a character in the case you expect , like in the beginning; "Read in (E); Encode (V); Decode (R)", and then ony handle uppercase letters E,V and R as correct ones.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: ASCII Code: the push back of letters?
« Reply #3 on: November 25, 2015, 11:28:28 pm »
Am I correct in assuming that the "Ver" procedure is supposed to do a ROT-encoding?
If so, then this seems to work (sort of: you skip rotating space characters, by design or a bug?).

The meaning of the "Rue" procedure however is unclear to me.
If it is supposed to decode the ROT encode Buchfeld variable, just use the "Ver" procedure again with the negative value of the original input to "Ver" (or 256 - original input).


B.t.w.

The logic of the menu's escapes me.
To me a more logical approach would be to not (separately) ask me to quit the program after each step (Ein, Ver, Rue).
Why not simply integrate that question into the first Menu:
"Read in (E); Encode (V); Decode (R) or Quit the program (Q)",

The "Ein" procedure could do with a little more explanation to the user (tell him how much letters he's supposed to give in?).

If you ask a question and want the user to input something, most of the time it is better to not go to the next line before reading in the answer, e.g. see the difference on the screen between:

Code: [Select]
  writeln('What is your name?');
  readl(Name);

  //versus

  write('What is your name? ')  //notice also the space at the end
  readln(Name);

Bart
« Last Edit: November 25, 2015, 11:36:48 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: ASCII Code: the push back of letters?
« Reply #4 on: November 26, 2015, 12:06:42 am »
A final suggestion (at least for the day).

Extract the routine the does the actual rotation from the "Ver" procedure.
Makte this procedure accept a variable that controls the amount of rotation (this must be able to be negative).

Inside "Rue" just call this routine, then do "Out" (and put a writeln() after calling "Out", it makes for nicer output.

With the apprpriate modifications I can make the proram output this:

Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten\bugs\Caesar>caesar
This is program Caesar, which performs ROT encoding.
Which action do you want to perform?
Read in (E); Encode (V); Decode (R): e

Enter letter  1 of 10: A
Enter letter  2 of 10: B
Enter letter  3 of 10: C
Enter letter  4 of 10: D
Enter letter  5 of 10: E
Enter letter  6 of 10: F
Enter letter  7 of 10: G
Enter letter  8 of 10: H
Enter letter  9 of 10: I
Enter letter 10 of 10: J
Which action do you want to perform?
Read in (E); Encode (V); Decode (R): v

Move letters how many positions? 5
F, G, H, I, J, K, L, M, N, O,
Which action do you want to perform?
Read in (E); Encode (V); Decode (R): r

E, F, G, H, I, J, K, L, M, N,
D, E, F, G, H, I, J, K, L, M,
C, D, E, F, G, H, I, J, K, L,
B, C, D, E, F, G, H, I, J, K,
A, B, C, D, E, F, G, H, I, J,              <<== the oriinal input
@, A, B, C, D, E, F, G, H, I,
?, @, A, B, C, D, E, F, G, H,
>, ?, @, A, B, C, D, E, F, G,
=, >, ?, @, A, B, C, D, E, F,
<, =, >, ?, @, A, B, C, D, E,
;, <, =, >, ?, @, A, B, C, D,
:, ;, <, =, >, ?, @, A, B, C,
9, :, ;, <, =, >, ?, @, A, B,
8, 9, :, ;, <, =, >, ?, @, A,
7, 8, 9, :, ;, <, =, >, ?, @,
6, 7, 8, 9, :, ;, <, =, >, ?,
5, 6, 7, 8, 9, :, ;, <, =, >,
4, 5, 6, 7, 8, 9, :, ;, <, =,
3, 4, 5, 6, 7, 8, 9, :, ;, <,
2, 3, 4, 5, 6, 7, 8, 9, :, ;,
1, 2, 3, 4, 5, 6, 7, 8, 9, :,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
/, 0, 1, 2, 3, 4, 5, 6, 7, 8,
., /, 0, 1, 2, 3, 4, 5, 6, 7,
-, ., /, 0, 1, 2, 3, 4, 5, 6,
,, -, ., /, 0, 1, 2, 3, 4, 5,

Not sure if this is what you want it to do, but if so, see that it also works with uppercase input.

(It might be nice to remove the last "," from the output of the "Out" procedure.)

Bart

 

TinyPortal © 2005-2018