Tuesday 23 June 2015

Web Tutorial: QBasic Baby Name Generator

Hello, World!

It's clearly a time for nostalgia since we'll be doing something light today - a QBasic Baby Name Generator I wrote back when I was an impressionable teenager.

Why a Name Generator?

OK, some backstory here. I was this idealistic young thing, see, and my head was filled with old-fashioned ideals like getting married and having kids. So my then-girlfriend and I would think of nice baby names. It wasn't long before I decided to use my newfound programming skills to do the work and calculate all the permutations.

Plus, I really, really wanted to test my understanding of array and nested loops.

What's in a name?

Here's the thing: I'm Singaporean Chinese. Therefore, to generate a Chinese name, we need a surname, and a given name made up of two Chinese syllables.

So fire up your QBasic Editor, let's do this thing! We begin by asking the user for his surname.
PRINT "Welcome to T___T Name Generator"
INPUT "What is your surname"; surname$


Next. we ask how many different words the user wants, to be run through the generator. You could get away with 1 or 2, but that wouldn't yield a very good sample size
PRINT "Welcome to T___T Name Generator"
INPUT "What is your surname"; surname$
INPUT "How many options do you want"; names


After we get the number of words, stored in the variable names (it's an integer, you can tell by how it doesn't have the "$" symbol after it, unlike the surname$ variable, which is a string.), we will create an array of strings middlenames$ of that size.
PRINT "Welcome to T___T Name Generator"
INPUT "What is your surname"; surname$
INPUT "How many options do you want"; names

DIM middlename$(names)


Now, we need to gather input. Get as many options for middle name syllables as the user indicated earlier. This asks the user, over and over, what syllables to use, saves each syllable into the array middlenames$ and then prints out each one as it is entered.
DIM middlename$(names)

FOR i = 0 TO (names - 1) STEP 1
    INPUT "Please enter Option:", middlenames$(i)
    record$ = "Option " + STR$(i + 1) + ": " + middlenames$(i)
    PRINT record$
NEXT i


So now we're ready to process the output! This is going to take the form of a nested For loop. The nested For loop creates names by combining the surname$ and the contents of the array middlenames$, and adding the entire generated name to the variable fullnames$.

The QBasic function LCASE$() converts the second middlename$ in the combination to all lowercase.
DIM middlename$(names)

FOR i = 0 TO (names - 1) STEP 1
    INPUT "Please enter Option:", middlenames$(i)
    record$ = "Option " + STR$(i + 1) + ": " + middlenames$(i)
    PRINT record$
NEXT i

FOR i = 0 TO (names - 1) STEP 1
    FOR j = 0 TO (names - 1) STEP 1
        fullname$ = fullname$ + surname$ + " " + middlenames$(i) + LCASE$(middlenames$(j)) + " | "
    NEXT j
NEXT i


And finally, we print out the results!
FOR i = 0 TO (names - 1) STEP 1
    FOR j = 0 TO (names - 1) STEP 1
        fullname$ = fullname$ + surname$ + " " + middlenames$(i) + LCASE$(middlenames$(j)) + " | "
    NEXT j
NEXT i

PRINT "Your Generated Results:"
PRINT fullname$


Time to test!

Compile and run. Let's see what this sucker does!

First, the program asks for your input. Enter your surname right here. Then it asks for the number of syllables you want to play with. 1 or 2 will still get you results, but let's go with 5 for more!



Now let's enter 5 different Chinese syllables.


And voila!


This works as well for Japanese names.




Just a little trip down memory lane. Here's another Chinese name for you - See Yew Soon!
T___T


No comments:

Post a Comment