# Tokenizing Example: Square

Well, I did it. I created a tokenizing program, which tokeninzes a square. I had a reference file, but the thing was coded so poorly that I didn’t even bother reading or understanding the darn thing. Here is the source code to the terribly written file: http://cplusplus.com/files/fraction.zip

This program took me about 30 minutes to write, and 2 hours to debug. I had multiple bugs, and wrote the loop that creates the “middle” of the square about 4 times before I finally created something clean looking and easy to read. A common error I made, was during the draw function, I would often reset the value of a or b to the incorrect corresponding variables width and height. If I were to label my variables as something extremely intuitive, I could have saved myself a lot of trouble :P

Here is the code for my square tokenizing program, it should look nothing like my reference (which I didn’t ever reference), and be easier to read:

/*Tokenizing Square*/

#include
using namespace std;

#define n ‘\n’;

void InputData (int &height, int &width)
{
//Stores data input by the user.
cout << "Enter in the height of your square: ";
cin >> height;
cout << n;
cout << "Enter in the width of your square: ";
cin >> width;
cout << n;
}

void Draw (int &height, int &width)
{
/*Draws the square. This function operates using the c variable to control wich
of the loops fires.*/
int a, b, c = 0;

a = height;
b = width;
c = 3;

/*The following two if statements check to make sure the dimensions aren’t too
high.*/

if(height > 30)
{
cout << "Too large of a height, enter in something smaller!";
cout << n;
cout << n;
c = 0;
}

if(width > 30)
{
cout << "Too large of a width, enter in something smaller!";
cout << n;
cout << b;
c = 0;
}

while (b > 0, c == 3)
{
if (b == width)
{
cout << " ";
}

cout << "__";
b–;

if (b == 0)
{
cout << n;
c = 2;
b = width;
}
}

while (a > 0, c == 2)
{
cout << "|";

for (b = width; b > 0; b–)
{
cout << "__";
}

cout << "|";
cout << n;
a–;
b = width;

if (a == 0)
{
c = 1;
a = height;
}
}

while (b > 0, c == 1)
{
if (b == width)
{
cout << "|";
}

cout << "__";
b–;

if (b == 0)
{
c = 0;
b = width;
cout << "|";
cout << n;
cout << n;
}
}
}

int main()
{
//This is the main loop, which is a do while loop.

do
{
int height, width, a, b;

InputData(height, width);
Draw(height, width);

cout << "Would you like to draw another square (y/n) ";
cout << n;

return 0;
}

I also read quite a few different blogs and featured articles at http://www.gamasutra.com/, none of which are directly relevant to basic programming, such as what I am currently doing.

That is a podclass made by DigiPen. It is an interview of two programmers, and is extremely interesting and relevant to anyone aspiring to become a professional programmer for the games industry. Right click on the link, and select “Save target as”, or “Save link as”.

Tomorrow, I will be going back to school. I plan to still do a little coding, so expect to have another blog post to read.

Share

## 6 thoughts on “Tokenizing Example: Square”

1. Patrick

A few things.

1) NEVER have unneccessary function calls. Calling a function is actually pretty slow. You should have put the code to input data and draw the square in your main function.

2) Wtf is with the “define n ‘/n’? I see what you’re doing, but not why you’re doing it.

Sorry to be so critical. >.> You’re doing really well for the amount of experience you have.

2. CecilSunkure

About number 1, thanks. I just assumed that multiple function calls would be better coding practice for while I’m learning.

And for two, it just makes writing out the code a bit simpler -that’s all.

3. scwizard

“NEVER have unneccessary function calls. Calling a function is actually pretty slow. You should have put the code to input data and draw the square in your main function.”
You have no idea what you’re talking about.

We have these things called compilers, they’re not stupid. They do this thing called inlining. Learn what it means before you teach poor Cecil stuff he’ll have to unlearn later.

4. Patrick

@scwizard: Weird, my dad said that they were slow, and he works for IBM. And I also heard the same thing from a profesional programming teacher at summer camp. I’ll talk to dad and ask what he meant. (Not saying I don’t believe you, of course.)

5. scwizard

There’s two possibilities:
1. Your dad works on low level stuff, that isn’t compiler optimized. This is what I’d guess, considering that IBM does some of that stuff, and he’s worrying about speed.