1. Re: QBasic to Euphoria - Reply

Lucius wrote:

>Euphoria doesn't have a way to input numbers
>so I had to create a loop to do so.
>I should have made the loop a function so
>that the could would not have exploded so
>large.

>Most Euphoria code converted from QBasic does
>NOT become 5 times the original QBasic code.

>Give me some short QBasic code to convert and
>I will convert it so you can see the result.


>It took me over a year to learn most of the nicknacks
>of QBasic.   BUT I have only been using Euphoria
>for about 3 weeks and I already know how to
>use most of it.

>        EUPHORIA is easier.
>Lucius L. Hilley III

Hi guys!

Of course, Euphoria is easier, the 'learning curve' is steeper. That means
that 'all beginning is difficult', but if you master the fundamentals, the
more difficult parts come almost by themselves.
But the beginning, that's what matters in this case. For that reason I will
give some tips to beginners, out of my own experience.

Input functions: they are not standard to Euphoria, but I'll give you
something that I also ripped from others programs.

1. Input of a string with a prompt:

global function prompt_in(sequence prompt)
-- make this functions global to all your programs
-- by putting it in your own 'myheader.e' file
sequence in_string              -- to read in the input
    puts(1, prompt)             -- put prompt (question) on the screen
    in_string = gets(0) -- read string input from the keyboard
    puts(1, '\n')               -- to start on a new line when back from
function
        -- return the input string now, but strip off the last character,
        -- since that is a 'new line' character (ASCII 10)
    return in_string[1..length(in_string) - 1]
end function    -- prompt_in()

This function can be used to input a string from the keyboard, for
instance:

-- snip
---------------------------------------------------------------------------
-----------------
sequence name

name = prompt_in("What is your name? ")
-- the sequence (string variable) name will now have your name in it,
-- and can be output to the screen using the puts() or printf() commands
---------------------------------------------------------------------------
--------------------------

2. The function for input of a number is a bit more complicated, since we
want to check if the input is a valid number. It also needs the help of
some standard include files, 'get.e' and 'graphics.e'. If the include
statements are forgotten, the program comes back with an error message,
telling you that it does not know the function get() and/or get_position().
So we have to start our 'myheader.e' file with:

include get.e
include graphics.e

global function input(sequence prompt)
-- print a message and read in a number
-- returns the number, that can either be an integer or an atom(float)
sequence input, curpos
    input = {1, 0}              -- this initializes the variable input with
                                        -- values that are *not* valid
    puts(1, prompt)             -- put message on screen
    while input[1] do           -- repeat until valid number
        curpos = get_position() -- read current cursor position
        input = get(0)          -- read from the keyboard
        position(curpos[1], curpos[2])  -- back to position if not valid
    end while
    puts(1, '\n')               -- to start on a new line when back from
function
    return input[2]             -- the second element of input is returned
and                             -- should contain a valid number
end function    -- input()

As I said before, these functions are not entirely mine, but partly
contributed by others, and I suppose I have their permission for using
them.

If you put these functions, and other useful things to come into a file
that you call 'myheader.e', or something like that, than a program like the
QBasic example is not hard to code anymore:

---------------------------------------------------------------------------
--------------------------------------
-- hockey.ex
-- reads in and prints team results.

include myheader.e

sequence team
integer won, lost, tie, points

team = prompt_in("\t Team: ")
won = input("\t Won : ")
lost = input("\t Lost: ")
tie = input("\t Tie : ")
points = won * 2 + tie
printf(1,"\t Team \t \t Won Lost Tie Points \n\n")
printf(1,"\t %-14s %3d %4d %3d %6d \n", {team} & won & lost & tie & points)

The last two lines still look quite complicated, especially the last one.
The first of the last two lines simply prints a heading. I am not sure if
everything will align properly, but if you read on you can make it align to
your own liking.
If you are familiar with the 'C' language, you will understand it. If you
are not, here's a quick explanation of the symbols used.
printf() stands for print formatted, and the junk you see in the last line
is called a 'format string'. '\t' means a tab, which is usually four spaces
in Euphoria. '\n' is a newline character, but in our case it is a new line
*and* carriage return, to begin printing at the start of the next line.
In between the tabs and newlines you see '%'-symbols. These symbols will in
printing be replaced by the contents of variables. '%s' prints a string and
'%d' an integer. The formats(lengths) used in printing can be set by a
number preceding the s and/or d, and you can left align text by a preceding
minus. So '%-14s' prints the teamname using 14 positions, left aligned.

I hope this explanation will encourage newcomers to start experimenting
with Euphoria, and that we'll see some programs of you in the near future.
If there is still something you don't understand, first read the helpfiles,
and then come back eventually with your questions. There are certainly a
lot of Euphoria people willing to help you out!

CU!

Ad.

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu