Blog: Keogh Code - Hello World!
- — 17 April, 2012 22:00
I'm not a programmer. In fact, I'm about as far away from being a programmer as one can get. And yet I find myself attempting to learn how to create apps using C++.
Why on Earth are you doing this to yourself?!
People keep asking me why I've decided to learn to code all of a sudden. There are a bunch of answers to that question, but the simplest one is "for self-improvement". Having more skills is always a good thing. But I'm also just doing this for fun, because I like to accomplish things and I get an immense satisfaction out of seeing them work as they should. I also get extremely frustrated when things don't work as they should, so you should expect me to vent my nerdrage relatively often.
I'm also pretty lucky, because I have an Assistant at home learning to code C++ with me, who has some programming experience (although his experience is in Java). So while I'm new to this, I have a helper.
Others have asked why a complete beginner like myself would choose a difficult-to-learn language like C++. Well, it's partly for your entertainment, dear reader. How are code-junkies supposed to revel in my failures if I'm not, you know, failing? What if other newbies want to follow along and learn from my mistakes?
And besides a general thirst for knowledge, I also have a manic, probably unhealthy passion for video games and the game industry. Since most games are coded in C++, it helps to have a basic understanding of the way games are made when reviewing or talking to developers. And who knows? Maybe I'll eventually get to the point where I can make my own little games that other people will enjoy.
Lesson One - Hello World!
On Sunday, I decided to get started. I bought a book from the Kindle store, Sams Teach Yourself C++ in One Hour a Day (6th Edition). If you want to follow along, I've so far found it extremely helpful - not only does it tell me what to do, but it explicitly tells me what not to do, which is important. In C++, there are always a bunch of different ways you can write code to accomplish the same goal. Some of that code, however, is either regarded as a "bad habit" or "not legal C++" (which seems to be just a fancier way of saying "bad habit").
Over the next couple of days, I endeavoured to create the first program that almost every programmer ever creates - an app called 'Hello World', that simply prints the words 'Hello World' to the screen. It would get embarrassing if I counted how many times I attempted to create this and failed, so I won't tell you. Oh, what the hell. It was six. Six times I tried to create the simplest C++ program possible, and six times I failed.
At first I thought the problem was in the code itself, and spent hours in Visual C++ 2010 (protip: you can get the Express version for free for non-commercial use) diligently copying the code from my textbook, reading it carefully for mistakes, and deleting all the text and starting again. Every time I tried to build the project - a process called 'compiling', for those who are complete n00bs like me - it failed, and I couldn't figure out why. Then I saw a tutorial for compiling on Microsoft's website, and suddenly I realised - my C++ file, which has a .cpp extension and is one part of the overall project file, had to be named 'main.cpp' because of a strange quirk of Visual C++. My textbook told me I could name it anything I wanted, but apparently that wasn't true, because as soon as I changed the filename, everything magically fell together and started compiling.
I launched the application and crossed my fingers. The Command Prompt program flashed on-screen, and then disappeared. Because of yet another Visual Studio quirk, I had to use Ctrl + F5 to run the program or it wouldn't remain open. However, I figured out I could also code the program to pause no matter which way I ran it.
In the end, the code for my working program looked like this:
1. #include <iostream>
3. int main ()
5. std::cout << "Hello World!\n";
6. char response;
7. std::cin >> response;
8. return 0;
Line 5 of the code tells the computer to print the words "Hello World!" to the screen. Line 6 and 7 tell the computer to close the application only when the user enters a character on the keyboard.
Seeing my first program come to life after so many failures was deeply satisfying, and kind of addictive. Once, after realising I'd made an error in my code while working on another task, all I wanted to do was run home and fix my mistake.
Next week I'll be blogging again, and this time I'll be coding something just a wee bit more complex - how to set integers and use them to get your computer to calculate for you.