## Beginning Programming 4 - Section One

### by Brad Moore

Newsletter Home | Beginning Programming 4 Home | Section One | Section Two | Section Three | Section Four | Glossary

### Responding to the Challenge

The Challenge

As you may remember from last time, we left off with a challenge to develop a simple little program that converts temperatures from Celsius to Fahrenheit and visa Vera. (On a side note - wouldn't science be much easier if only people with names like "Smith" and "Davis" had inventions and discoveries named after them - I doubt the Fehrenheits had any idea the difficulty that would befall millions of school children as well as dyslexic spellers.) But I digress - this program was to have a simple interface using the Prompt, Confirm and Notice widgets for exclusive input and output. There are several forms this program could take, but let me offer you my interpretation.

As we develop this program we will be getting our temperature values from the user using the Prompt widget. Prompt is a simple widget that displays a short text message and a text entry field where the user can type in a value. Prompt will only accept string variables in version 3.02 of Liberty Basic (the most common version in use, although this is corrected in version 3.03 - which has recently been released). Even when using a version of Liberty Basic that supports both numeric and string variables as an argument to the PROMPT function, let me suggest that you continue to use strings. Here is why: The PROMPT dialog window has a cancel button. When pressed the value returned to a string is an empty (or NULL) string (a string with no length or value), but the value returned to a numeric variable is zero. Using a string argument you will be able to determine whether the user actually entered the numeric value of zero, as it will contain the ASCII equivalent of zero (the value 48), or pressed the cancel button. Do this using the LEN function (explained a bit later).

LEN is one of the many string functions; a special sets of commands and functions designed to manipulate and determine information about string variables. LEN is of particular interest because it will return a numeric value that tells us exactly how many letters (both those that can be displayed and those that can not) are in the string. We call this the "length" of the string. The usage is simple. You have a string of interest, lets say A$. We can assign its length to a numeric variable using the following syntax:

    a = len(A$)

or we could simply print the length (or use its value in many other ways as well) by using the function in conjunction with the PRINT statement. Here is an example:

    print len(A$)

If the length value returned by the LEN function is zero, then we would say that the string being evaluated is a NULL string, or it is a zero length string.

Having decided to use a string variable as our return value from the PROMPT function, we are faced with a little dilemma - we need numbers to perform math on. This will require us to convert our string value to a numeric variable. This is done using the VAL() function. The VAL function is yet another of the string functions provided by Liberty Basic. It has some resemblance to the LEN function, but instead of returning the length of the string, it returns the numeric value of any leading numbers that are part of the string. In other words, if the string were the following:

    A$ = "1234adbc"

Then the leading numbers ("1234") would be returned as the numeric value one thousand two hundred thirty four. However, consider the following string:

    A$ = "my dog is 15"

This string will not return the 15, but rather a zero. This is because the string does not begin with a numeric value. Liberty Basic terminates its effort to evaluate the value of the string as soon as it encounters a non-numeric character.

Here is what the Liberty Basic Help file says:

VAL(stringExpression)

Description:

This function returns a numeric value for stringExpression is stringExpression represents a valid numeric value or if it starts out as one. If not, then zero is returned. This function lets your program take string input from the user and carefully analyze it before turning it into a numeric value if and when appropriate.

Knowing how to go from a string containing a number to a numeric variable is only part of puzzle, it is also important to know how to go from numeric variable to the string representation of that value. This is done with the STR$() function. Yes, this is another of the many sting functions. It will convert a numeric variable to a string equivalent and allow us to store it in a string variable. This is useful for converting your values into strings so they can be used in functions that require string values only. Some such functions that require their value to be displayed to the user as a string are the CONFIRM, NOTICE and PROMPT functions. You will see in the Temperature Conversion program how we use the STR$ function to display a numeric value in these types of functions.

There are many other string functions, and they all work together like an orchestra to turn out a beautiful piece of music. We will not be covering these functions at this time, but I have written another article on String Functions which may be of interest to you. Find it in the newsletter, or on my website [http://www.freewebz.com/lb-connection].

The Temperature Conversion Program -

As always, I begin my programs with a brief explanation of the program, the file name, the author and the legal status of the program. In the case of this program, it has been placed into the public domain (although the article is still copyrighted!). I also like to mention which version of Liberty Basic this was written for.

    'Temperature conversion utility
    'By Brad Moore
    'Placed in the public domain - May 2003
    '
    'Written for Liberty Basic 3

I knew as I considered the program that I would be offering to convert as many temperatures as the user desired, so I planned to branch back to the top of the code. I added a label to support this. Next I used the PROMPT function to get a numeric value from the user. I put this into a string both for compatibility reasons (with 3.02) and to allow me to detect the difference between a cancel and a zero entry.

    'A label at the top to loop back to...
    [begin]
    Prompt "Please enter the temperature to be converted";a$

Now test the result string (a$) to see what its length is. A zero length string indicates that the user pressed the cancel button. If the user pressed cancel, lets assume they want to quit the program, and simply END.

    'if length of a$ is zero then user pressed cancel
    if len(a$) = 0 then END

We will need to convert the value contained in a$ to a numeric value so that we can perform math on it. We did not actually test to see that it is a number, so if the user entered alpha characters, they will get a value of zero from our evaluation. A more advanced program might test zero values against the actual string value - if it was not a zero the user could be warned or requested to reenter a value. In our case we simply want to get a numeric from the string a$.

    'Convert the value input into a numeric variable
    temp1 = val(a$)

So far all we have is a temperature. We have not determined whether this is a Celsius or Fahrenheit temperature. We do this by asking a simple yes or no question. In this case we want to know if this is a Fahrenheit value, if the user responds "yes" we will know we are converting Fahrenheit to Celsius, other wise it will be the other way around. One interesting thing to note is how we integrated the numeric value of the temperature the user supplied back into the message to be displayed in the confirm dialog window. We used the STR$() function explained previously to cast the numeric value as a string value.

    'Now ask if this is a Fahrenheit value (if no - we assume Celsius)
    confirm "Is the temperature " + str$(temp1) + " degrees in fahrenheit?";yesno$

Now we do the heavy lifting. I have coded the two conditions (conversion F to C and C to F) into a IF - THEN - ELSE - END IF statement. (We covered these several installments back.) If the user responded "no" to the query above then we convert Celsius to Fahrenheit, other wise we skip down to the ELSE clause and convert the other way. Let's look at the conversion for Celsius to Fahrenheit.

    if yesno$ = "no" then

OK, so we know we are going C to F. We start by doing the math. The exact equations were supplied in installment three. We assign the result to a numeric variable.

        'I guess we are converting Celsius to Fahrenheit
        temp2 = ((temp1 * 9)/5) + 32

Now, one of the neat things about this approach - we display the answer along with the query about converting another temperature yet again. This allows us to use a CONFIRM statement which will open a dialog window without issuing a beep, which the NOTICE dialog window does do. (Heavy use of the NOTICE function can become annoying, due to the beeping, if there are too many NOTICES chained one after another.) It also allows us to compress our output without requiring too many windows. In order to get enough information into the "message" potion of the dialog window, we found it necessary to chain the string across several lines. This is accomplished using the "continuation character" (an underscore "_") as the very last character on the line of code. It tells Liberty Basic that the current line of code extends on into the next line. In the case of a string literal, we are required to concatenate the component of the string between line wraps. Take a look at the code. We do this by terminating the string (with a quotation mark) and them using a plus sign ("+") to combine that string with the next. It simply tacks the next string to the end of the current string.

Notice that we did a couple other unique things. We displayed the user's own temperature value they input along with the new converted value in the string. We did this by using the STR$() function again. We needed to concatenate these strings, so we did this with the addition operator (the plus sign "+"). We also wanted to force part of the text on to a new line in the dialog window. We do this by sending a carriage return embedded in our string. You can't actually type that in, so you send the character equivalent using the CHR$() function. This is a special string function that returns a character as a string that is equivalent to the ASCII value supplied. It is covered further in the glossary under ASCII.

The CONFIRM function returns the user selection in the string b$. It is evaluated a bit further down.

       'to simplify - display the answer and prompt for another conversion
       confirm "Celsius to Fahrenheit conversion: " + str$(temp1) + _
               " degrees Celsius equals " + str$(temp2) + " degrees Fahrenheit." + _
               chr$(13) + "Do you wish to convert another?";b$
    else

Here after the ELSE clause we evaluate the conversion F to C. This is done the same way as above. The code is basically the same.

       'I guess we are converting Fahrenheit to Celsius
       temp2 = ((temp1 - 32)*5)/9
       'to simplify - display the answer and prompt for another conversion
       confirm "Fahrenheit to Celsius conversion: " + str$(temp1) + _
               " degrees Fahrenheit equals " + str$(temp2) + " degrees Celsius."+ _
               chr$(13) + "Do you wish to convert another?";b$
    end if

Now we evaluate that response to the query to convert another temperature. The query and resulting value is the same for either path above. If b$ actually equaled the value "yes" (it can only return "yes" or "no") then we branch back up to the top to the label [begin] and start all over again.

    if b$ = "yes" then [begin]

If we chose to not convert another number then we simply fall through the IF-THEN statement and shoot a simple notice up on the screen thanking the user for using the program. Then we END.

    'Thank the user and we are done...
    notice "Thank you for using the simple temperature converter..."
    end

The Program -

The program really is fairly simple and straight forward. Just a few windows displayed, but the whole program uses the dialog widgets and only the dialog widgets for input and output to the user as defined in the challenge. If you are interested in the code without all the commentary (so that you can copy it and paste it into your own Liberty Basic editor window and run it) - here it is:

    'Temperature conversion utility
    'By Brad Moore
    'Placed in the public domain - May 2003
    '
    'Written for Liberty Basic 3

    'A label at the top to loop back to...
    [begin]
    Prompt "Please enter the temperature to be converted";a$

    'if length of a$ is zero then user pressed cancel
    if len(a$) = 0 then END

    'Convert the value input into a numeric variable
    temp1 = val(a$)

    'Now ask if this is a Fahrenheit value (if no - we assume Celsius)
    confirm "Is the temperature " + str$(temp1) + _
            " degrees in fahrenheit?";yesno$

    if yesno$ = "no" then
       'I guess we are converting Celsius to Fahrenheit
       temp2 = ((temp1 * 9)/5) + 32
       'to simplify - display the answer and prompt for another conversion
       confirm "Celsius to Fahrenheit conversion: " + str$(temp1) + _
               " degrees Celsius equals " + str$(temp2) + _
               " degrees Fahrenheit." + chr$(13) + _
               "Do you wish to convert another?";b$
    else
       'I guess we are converting Fahrenheit to Celsius
       temp2 = ((temp1 - 32)*5)/9
       'to simplify - display the answer and prompt for another conversion
       confirm "Fahrenheit to Celsius conversion: " + str$(temp1) + _
               " degrees Fahrenheit equals " + str$(temp2) + _
               " degrees Celsius."+ chr$(13) + _
               "Do you wish to convert another?";b$
    end if

    if b$ = "yes" then [begin]

    'Thank the user and we are done...
    notice "Thank you for using the simple temperature converter..."
    end

Newsletter Home | Beginning Programming 4 Home | Section One | Section Two | Section Three | Section Four | Glossary