Hello, World! (first variation)
It's a time-honored tradition for beginning programmers to start with a program like this one, that prints a polite greeting to the world. Follow these directions to create yours. A computer follows directions more closely than anyone, so we have to be very accurate.
- Open a blank file in Sublime Text. Use File...Save As to save the file with the name
HelloWorld.cpp. For now, it doesn't matter where you put this file. Organization will be important later. Notice that in the lower right-hand corner of the Sublime Text window, the text "C++" appears. The
.cpp file extension tells Sublime Text we are editing a C++ source code file.
- Don't use copy/paste yet. You need to practice typing programs by hand.
- Make sure to pay attention to all the spelling, capitalization, punctuation, and spacing/indents.
- Use TAB to indent, not the space bar.
- Pay attention while you type. Sublime Text has some features that may surprise you if you haven't played with it yet.
Here is the code you should type into Sublime Text:
using namespace std;
cout << "Hello, world!" << endl;
When you are sure you have entered all the code correctly, including the blank line at the end, save your file. Keep Sublime Text open, just in case you have made a typo. Now it's time to compile your program. To do this, we need to use a shell (an interactive command-line interface) and the shell has to be steered to the right location on the disk.
- Open the MinGW Shell (Windows) or a Terminal window (Mac/Linux; on Mac, find the Terminal application in Applications/Utilities). You should see a dollar-sign and a flashing cursor. This is called a prompt. The shell is waiting for your typed instructions.
- We need to navigate the shell to the directory where you saved your HelloWorld.cpp file. This is a little tricky if you are not familiar with file systems. On Windows, the directory location might be something like
- The corresponding navigation command is
cd /c/Users/drosoff/Documents/csc150. Pay close attention to the punctuation. There are reasons why it is so strange, but we don't need to go into them now.
- On a Mac or Linux machine, the directory location will look different, maybe something like
- The corresponding navigation command is
cd /home/Dave/csc150. Here the punctuation is identical.
- When you have typed the navigation command and hit Enter, you are hoping to just see another prompt. If you see any message at all, it is probably an error.
- Type the command
ls to see a listing of the files in the directory you navigated to. You should see your HelloWorld.cpp file.
- If you do, go to the next step.
- Otherwise, try navigating again, or ask for help if you are stuck.
When you see a directory listing including your file, you're ready to compile. Use the command
g++ -o HelloWorld HelloWorld.cpp.
- Remember: no news is good news. Did you get an error? Switch back to Sublime Text, check for typos in your program, save, and try again.
- Check the directory listing again (use
- Do you see the executable file created by the
g++ compiler? It's called
HelloWorld.exe on Windows and plain
HelloWorld on Mac/Linux.
If you see your file, run it! Type
HelloWorld at the shell prompt (you can ignore the
.exe suffix) and hit Enter. You should see your polite greeting!
Congratulations! You have written your first program!
Hello, World! (second variation)
You can pass more than one thing at a time to
cout, and it doesn't have to be just quoted text (a string, in programmer-speak). Return to the text editor and change your code to look like this:
- Underneath the
cout statement (which should be on line 7), add another statement as follows. Remember to pay attention to the punctuation and the spacing.
cout << "7 times 13 is " << 7*13 << "!" << endl;
Now use File...Save As to save your modified code with the name
HelloWorld2.cpp. Make sure you don't save over anything. Return to the shell window and compile, using a
g++ command like before (change both filenames!). Ask for help if you get stuck.
Hello, World! (third variation)
cout has a buddy named
cin, who can get input from the user. Delete your two
and replace them (remember: never copy and paste! type everything in yourself) with:
cout << "What is your first name?" << endl;
cin >> myName;
cout << "Hello, " << myName << "!" << endl;
Use File...Save As to save your modified program with the name
HelloWorld3.cpp. Get someone to check your indent/spacing before you compile. Make sure you don't save over anything, and change the filenames in your
g++ command. What happens when you run the third variation? (Answer the question the program asks you.)
Submit this workshop
When your three variations are done and run properly, use the command-line shell to run each of them in turn:
Take a screenshot of the shell window that shows these commands and the output. Upload four files: the screenshot and your three
.cpp files. You're done!