The secret to life? Make it happen.
So, you’ve got a fancy compiler up and running. Get yourself a .cpp file up and running. I’ll be using XCode, so it’s already done for me. If it isn’t done for you, then just do the standard File->New and then name it something along the line of main.cpp.
This is what main.cpp as created by XCode will look like (although yours will probably have different colors.)
What is all of this?
Well, let’s take it line by line.
#include <iostream> is, believe it or not, an include statement. This simply tells the compiler, “Hey bro, make sure you remember to use all of this stuff. The thing within the < > is a file or library. Don’t worry about this too much. For now, I’ll tell you what to include.
After that is “int main (int argc, char * const argv).” This line can be a bit intimidating. WTF does all that mean? Well, the stuff inside the parentheses are command line arguments. For now, just delete everything so that it reads “int main()”. What does that mean though? Well, it’s the main function. It’s just the thing that holds all the stuff you want to do.
What about that crap inside of it?
Well, the two backslashes denote a comment. So, anything behind // isn’t compiled. The “std::cout « “Hello, World!\n”;” portion is just printing it to the screen. You can think of “cout” as the print function. The “std::” portion is just telling the compiler where to look. I personally think that’s an ugly way of doing it, so I’ll tell you how to fix it. Promise.
So, you know how to get the file running and you sort of understand what the photo above is. Now what? Let’s make a program to get your name and tell you hello.
We’ll need to start with somehow telling the user that the program needs to know the user’s name. We can do this with the “cin” command. It’s pronounced see-in. This is pretty much the opposite of “cout.” But wait, remember how we had to put “std::cout” back up there? Yeah, I don’t like that. I prefer to put a simple little line in that fixes that. Right under your include statement, put “using namespace std;”. This simply lets the program know that it can throw “std::” in front of whatever needs it. Thank me later.
So, real programming talk. Here’s what needs to happen. We can use the “cin” command to store input to a variable. The command would look like this: “cin » name;” You’ll notice that the « is now ». That’s just saying that we want to input the information instead of output it.
So, we now have stored the data into a variable named “name,” which you need to have declared beforehand.
Now we want to output this. We also want to include a message…hmm….how about, “Hey there [name]”. How would we do that? Well, it’s simple. Just remember how we did “cout” earlier. Here’s how the command would actually work out: “cout « “Hello ” « name;”
Bam. Now, as I’m sure you’ve figured out if you tried to run that, there are a few things not included. But you should understand it! Here’s the actual code that should run.
C++ is a “statically type, free-form, multi-paradigm, compiled, general-purpose, powerful programming language.” At least, that’s what Wikipedia tells you. As far as your’re probably concerned right now, it’s just a programming language. Don’t worry about the rest of that, for the most part, until you know what the words mean. Trust me, they’re cool.
But, how do you even get to the point of making something? It took me a while. Everyone seems to skip that step. So I’m going to devote lesson one to getting all the tools to make a program. No actual programming. Skip to lesson two for that. If you need a compiler, keep reading.
Firstly, if you have Windows, you can try Bloodshed Dev. It’s a free compiler. Download it and install it. Easy enough! You’re done. Just click around to find the right buttons. You can figure out installation for Windows out, I promise.
But what about if you’re on a Mac? Believe it or not, it took me a while to figure it out. At first, I wrote all of my programs in TextEdit and saved them to a flash drive. Then I would put it on my roommates laptop to test. That got old quick. Then I discovered XCode. Trust me, use XCode. XCode should be installed by default, I believe. Click on “Application” and choose “C++ stdc++” as your type. It’ll open up a nice window. One of the files in the center should be named “main.cpp.” Open that baby up. There’s your program.
If you’re using Linux, well, you’re probably an advanced enough user to figure out a compiler. The only one I have much experience with on Linux is Code Blocks. I can vouch for it being decent, and it’s as easy to figure out as Bloodshed up there is. If not, you can easily compile via command line. I won’t cover that here though, because I’d rather not scare people or anything.
So, you want to get started in programming a little C++, eh? Well you’re in the right place. I’m currently a rising junior at the University of Southern Mississippi, pursuing a degree in computer science. At USM, the introductory classes are taught via the C++ language. This can definitely seem pretty intimidating if you’ve never really done any programming before.
But I’m here to help!
This is intended as a bit of an introduction. Of course, I’m probably going to go a little faster than your professors. On that same hand, if yours are anything like mine were, you’ll need a bit of an intro to your intro course.
So here goes! The next few posts will be a series of tutorials. I’ll try and make them short and sweet.
I’m finally getting around to figuring out how those pesky iPhones work.
I’m in the midst of filling out paperwork for an internship.
The application has to be either scanned/emailed or faxed in. So I scanned it, seeing as I don’t have a fax machine. No biggie. It’s thirty five pages though, so it took a while on my little flatbed scanner.
Then I tried to email it. But the file was too large.
So I did a little devilry and got a link to the file. Once again, no biggie. I just threw the person helping me apply a link to it. All they have to do is click it.
Six emails later and the person still hasn’t clicked the damn link, and keeps suggesting that I split it or fax it. Really? Click the effing link, it’s big and blue.
You know the biggest problem with learning?
I can’t do it fast enough. I just want to know how to do stuff as quickly as I can. I understand that real knowledge takes a while to obtain, but I like when things are nice and intuitive. Like python. That makes sense.
I’m in the middle of trying to actually become not terrible at a bit of web design. And, as of yet, it’s about as intuitive as a monkey in space.