oxygen adsorbates on Pt(111) surface

C/C++ Tutorials - Lesson 1
Getting Started


Getting started writing a program is actually quite simple. The most difficult part, if you are running Windows, is selecting and installing a c/c++ compiler. Microsoft has a free c/c++ compiler called Visual C++ Express which you can download and install for free. After downloading and installing Visual C++ Express, open Visual C++ Express and select File->New Project... and then choose the C++ Console Application. For this first program name it hello.


If you are using a Linux machine, the GNU compiler should come with your operating system. Simply open your favorite text editor and you are ready to start programming. Name the text file hello.cpp.

Hello World!

The prototypical first program is called hello world. This program demonstrates how to output a simple line saying "Hello World!" to the terminal or command prompt. In your editor type the following program:

// libraries and header files
// iostream library is for reading/writing to the terminal/command prompt
// if you are Visual C++ you need to include the line '#include "stdafx.h"'
#include <iostream>

// The following line tells us to use the standard (std) namespace
// don't worry about what a namespace is for now,
// that is an advanced topic
using namespace std;

// The main function is where the program starts
int main()
// cout stands for c output, cout outputs to the terminal/command prompt
// endl stands for end line and will put an extra line after Hello World!
           cout << "Hello World!" << endl;
// the main function must return an integer
           return 0;

*Note that C/C++ are case sensitive:
cout << "Hello World!" << endl; will work,
Cout << "Hello World!" << endl; will not work.

The next step is to compile the program and then to run it.

Windows users: Go to Debug->Debug. A black command prompt will pop up and run the program if it compiles successfully, but the command prompt may disappear before you've noticed it. If this is the case, add the line 'cin.get()'. 'cin.get()' will cause the command prompt to wait for you to give it input before it exits.

Linux users: Type 'g++ -Wall hello.cpp -o hello'. g++ is the GNU c++ compiler, the -Wall options will cause the compiler to show all warnings, hello.cpp is the program you just wrote, and -o hello tells the compiler to write the program and call it hello. Assuming the program compiles without any errors, type hello and you should see 'Hello World!' appear in the terminal.

Now you should know how to write a program that writes output to the command prompt, but often times it is desirable to write output to a file instead. Below is a modified code that will write 'Hello World!' into a file called hello.txt. Call this progrm fhello.cpp.

// fstream library is for reading/writing to the file system
#include <fstream>

using namespace std;

int main()
// ofstream stands for output file stream
// we are creating an output file called
// hello.txt in the file system, but oFile in our program
           ofstream oFile("hello.txt");
           oFile << "Hello World!" << endl;
// closing oFile, it is good practice to close files that are not in use
           return 0;

After you compile and run fhello you should have a file called 'hello.txt' in the same directory from which you ran the program. Open hello.txt and verify that it says 'Hello World!'.

© 2013 Dave Schmidt

Web design: Dave Schmidt