In this lab we will write a simple C++ program.
You should be logged in.
Open a terminal window. Use the menu icon on the top left of the desktop, under "Accessories", "Terminal Emulator"; or right-click on the desktop, then "Open Terminal Here".
Type pwd (print working directory) to see which directory you're in. (The string ending in ~$ is assumed to be the command prompt, and should not be typed.)
uname@hostname: ~$ pwd /home/uname
Change directory to your "sfuhome". Files stored here will be stored permanently and available if you log in to another machine.
uname@hostname: ~$ cd sfuhome
Make a directory for the files for this lab, for example, call it lab0.
uname@hostname: ~$ mkdir lab0
Open an editor. I use emacs, which can be opened at the command prompt ("emacs &") or under "Development", "GNU Emacs 23" from the menu icon. You could also use "vim", "gedit", "jedit", or any other text editor.
Copy the following code into the editor
#include <iostream>
using namespace std; int main (void) { cout << "Hello world!"; return 0; }
Save this to a file called hello.cpp in your lab0 directory.
Change to this directory using cd (change directory):
uname@hostname: ~$ cd lab0
Type ls (list) to see the contents of the current directory. hello.cpp should appear:
uname@hostname: ~$ ls hello.cpp
To get input and print output in C++, use the cin and cout functions. cin is used for input and cout for standard output (to the display). But first you need the appropriate library.
Your program must start with include statements to import the necessary libraries. The only library you will need for this program is the iostream library which contains the cin and cout functions. In addition you will need to open the standard namespace to access these functions. That is the function of these lines above the main function.
#include <iostream>
using namespace std;
To get input from the keyboard into a variable named x:
int x = 0;
cin >> x;
To send output to the display:
cout << x;
Note that this will not print the value of x on a new line, but you can use endl to specify a newline. Below I'm printing the value of x on a new line (with some explanatory text) and then printing another line:
cout << endl << "The value of x is: " << x << endl;
In order to run the program we just wrote, we need to compile it into an executable. The C++ compiler we will use is called g++. Run the compiler on the program contained in hello.cpp by typing:
uname@hostname: ~$ g++ hello.cpp
If there are errors in your code, the compiler will let you know. Otherwise, it will produce an executable file called a.out:
uname@hostname: ~$ ls a.out hello.cpp
Run this executable by typing
uname@hostname: ~$ ./a.out Hello world!"." refers to the current directory, and "/" characters are used to separate directory and file names in linux/unix.
The name a.out is a default name given to the executable, but is not particularly mnemonic. You can specify another name for the executable produced by g++ using the -o option:
uname@hostname: ~$ g++ -o hello_world hello.cppYou can now run hello_world in a similar fashion:
uname@hostname: ~$ ./hello_world Hello world!
Obtain a single integer as input using cin.
Print "Hello world!" (no quotes), unless the integer is 42, in which case "Shh!" (no quotes) should be printed.
uname@hostname: ~$ ./hello_world 5 Hello world! uname@hostname: ~$ ./hello_world 42 Shh!
uname@hostname: ~$ unzip lab0-test.zip uname@hostname: ~$ ls 1.gt 2.in a.out hello_world test.py 1.in 2.gt hello.cpp lab0-test.zipRun the program test.py it contains:
uname@hostname: ~$ ./test.pyIf you have correctly modified hello.cpp as specified above, you will see:
Running test 1... passed Running test 2... passed Passed 2 of 2 teststest.py is a Python script, as you might have guessed. The "shebang" (#!) line tells linux to use python to run it. It uses input/output "redirection" (>,<) to pass input (*.in) to your hello_world program and compare (diff) this output (*.out) against specified output (*.gt).
If you have passed 2 of 2 tests, please ask a TA to take a look, and receive your 1 mark for this lab.