DeafGrandma program in PortablE

I have been using small programming exercises from a book about another language and coding them in PortablE. One of the programs was called DeafGrandma. I thought it sounded fun, so I chose it to code. This is a simple program, but you can learn some valuable things you will be using over and over. First, here is the description of what they wanted the program to do.

Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! You can’t stop talking to grandma until you shout BYE.

The most difficult obstacle I had writing this program was how to use E-strings. This is a special feature of the Amiga E language. I thought I understood them, but I found out I wasn’t at all sure how to compare them with each other or normal strings. For a good explanation of E-strings check out this page of the Amiga E Beginner’s Guide.
Ok, so here is the code for DeafGrandma.e

/* DeafGrandma program 5-27-09 */

PROC main()
    DEF say[80]:STRING, upperSay[80]:STRING

    REPEAT
      Print('What would you like to say to Grandma?\n')
      ReadStr(stdin,say)
      SetStr(say, EstrLen(say) - 1)

      StrCopy(upperSay, say)
      UpperStr(upperSay)		->make uppercase version of "say"

      IF StrCmp(say, upperSay) = FALSE
        Print('HUH?! SPEAK UP, SONNY!\n')
      ELSE
        Print('NO, NOT SINCE 1938!\n')
      ENDIF
    UNTIL StrCmp(say, 'BYE') = TRUE
ENDPROC

To test this just go to the command line and use PortablE to compile it first.
PortablE DeafGrandma.e
This will give you a .cpp file that you can use a c++ compiler to compile to give you an executable. Like so.
g++ DeafGrandma.cpp -o DeafGrandma
Now you can just type the name DeafGrandma to run the program. On to the code.
/* DeafGrandma program 5-27-09 */
This first line is just a comment. I wanted to show what they look like, so here is one type. I have the other type down a few lines.
PROC main()
This next line is the main procedure of our program. Every PortablE program must have a main() procedure.
DEF say[80]:STRING, upperSay[80]:STRING
Now things start getting interesting. Here we define our E-strings. We have two fixed E-strings here named say and upperSay. Both have a max length of 80.
REPEAT
This is the start of our loop. A WHILE loop could have worked in this instance, but I went with REPEAT.
Print('What would you like to say to Grandma?\n')
Of course this displays the text. The \n adds a return on the end and sends the cursor to the next line.
ReadStr(stdin,say)
SetStr(say, EstrLen(say) - 1)

The first line here is waiting for input from the keyboard or user. It places what is typed into the “say” E-string we created earlier. It also saves the return that is pressed. The next line removes this return from the E-string.
StrCopy(upperSay, say)
Basically, this copies the content of the say E-string to UpperSay.
UpperStr(upperSay) ->make uppercase version of "say"
The UpperStr() function here makes all the characters in the upperSay E-string all caps. The -> out to the side is a way to place a comment on the same line as your code.
IF StrCmp(say, upperSay) = FALSE
This is the start of your IF statement. It is using the StrCmp() function to compare the two E-Strings. This is so we can test whether someone entered something in all caps or not. Because the only way Grandma is going to hear you is by yelling using all caps. The ELSE that follows is what happens if something is typed in lower case or mixed case.
UNTIL StrCmp(say, 'BYE') = TRUE
This is the condition test of our REPEAT loop. It just tests to see if BYE is typed in all caps. If it is, the program stops right here.
Finally the ENDPROC just finishes off our main PROC() that was stated earlier.

Since this was a very small program, I thought it would be good to go through each line of the program to explain everything. Hopefully beginners will find it useful. When I first tried to write this program I tried to compare E-strings using operators like and :=. This will not work, so this is why the functions are used. I think it makes it nice and easy to read as well.

1 comment so far

  1. Chris H on

    The following line:
    UNTIL StrCmp(say, ‘BYE’) = TRUE

    Can actually be written as just this:
    UNTIL StrCmp(say, ‘BYE’)

    It has the same meaning, since UNTIL stops looping when it has a non-zero value, and TRUE is a non-zero value. However, as this may be a bit more cryptic, the original line is probably best for beginners.


Leave a reply