Sunday, June 30, 2013

My First True Program: JerkMachine

Today I wrote a program. Truly, it was nothing significant. I tossed together bits of code to create a simple, useless construct. But it was rather enjoyable (when I was not frustrated beyond all reason), and I found it fairly useful (to a degree).
The program I wrote is fondly titled "JerkMachine". I built it for one function: to be rude and menacing to humans. There are no computations involved: my program does not count backward from 30 or give you integers in increments of two. It merely provides dialogue and accepts two responses from a user (two very simple responses). I wrote it for a fun little venture into how coding works and what tools are at one's disposal.
All of the code is based around character data types. I made a number of character date types (i.e name, greeting, firstinsult) as statements. For instance:

char greeting[] = "Hello, puny mortal! Cower before me! ";
char firstquestion[] = "Tell me- what is your name? ";

Some characters existed to serve as place-holders for a user's response:

char name[30];

Why did I give some of the characters values within the brackets and not others? That is a good question, and I am not so sure myself. From what I gathered in my reading, the numbers (as in char name[30]) give a certain number of places that the to-be-implemented name may take up (in this case, 30 spaces). The other characters (like char greeting[]) were not given values as they were directly equated to statements and thus had specific place values. Now, bear in mind that none of this may be entirely accurate. It will take me time to get proficient with all the terms and concepts behind even the base-level programming.

The remainder of the program is centered around input and output configurations. A "cout" marks where the program should spit out a certain something (string, integer, etc), and a "cin"marks where the user may enter a certain something. For "JerkMachine", the first of two user-input instances is when the user may enter his or her name, as seen below:

cin >> name;

Earlier, I had established "name" as a character. In addition to making name a valid character, I also enabled "name" to take up 30 spaces. In theory, anyone can enter their first name into the program and it will run properly.

However, the great kicker in this rousing example of pointless coding is the conditional. That is right, ladies and gentlemen: a conditional!
Conditionals are based on "if", "else", or "else if". In essence, they break down into true-or-false checks. For "JerkMachine", I wanted the user to be given two options for a reply: yes or no. One reply would beget a response from the program that differs from the other. I envisioned that it would be a fun little section to finish off the process. I was mistaken. For the longest time, I struggled to make the program function based on replies of "Yes" or "No". I will not go into details, but after establishing both "Yes" and "No" as characters and integers, and sometimes nothing at all, I found I could never get the "JerkMachine" to run properly. With experience I will be more comfortable with the various data types and control structures, but for today I was thoroughly confused.

The entire text-file for "JerkMachine" can be read below. Believe me when I say it functions. After a couple grueling hours of stupidity, it finally functions.
And I smile with senseless joy every time I run it.


"JerkMachine"
//Attempt at making a simple interactive program

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{

char name[30];
char greeting[] = "Hello, puny mortal! Cower before me! ";
char firstquestion[] = "Tell me- what is your name? ";
char firstinsult[] = "What a foolish name! I truly pity the incompetence of your species. ";
char secondquestion[] = "Now, answer me this- do you like computers? (Y/N) \n";
char response;

cout << greeting << "\n";
cout << firstquestion;
cin >> name;
cout << name << "! " << firstinsult << "\n";
cout << secondquestion;
cin >> response;
if (response == 'Y')
{cout << "\n" << "Well played, mortal. You have appeased me. You live a day longer." << "\n";}
else if (response == 'N')
{cout << "\n" << "Insolent mammal! You dare insult me in this fashion! Sleep with one eye open!" << "\n";}

return 0;
}

1 comment:

  1. I had to play this in the background while running your program... http://youtu.be/qv9VKKXwVxU

    ReplyDelete