In this lab we will write a simple C++ program. This is mainly to ensure that you have familiarity with and can use the tools in the lab.
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 objects and the functions for them. 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.
Compile hello.cpp into the executable program "hello_world" using the "-o" flag to g++, as above.
uname@hostname: ~$ ./hello_world 5 Hello world! uname@hostname: ~$ ./hello_world 42 Shh!
To have your program read directly from a file, first create an ifstream (input file stream). To do this, you must include the fstream (file stream) header and make sure you are using the std namespace. Then, in main, create an object named input. Input should have type ifstream, using the filename as the argument to the constructor.
Now you can read from input just like you read from cin, as in the example below:
#include <fstream>
using namespace std;
int main(void) {
ifstream input("inputFile.in");
int x = 0;
input >> x;
return 0;
}
The major difference is that cin is already created for you by C++,
where you have to create the input file stream (like input above) yourself.
To write to a file, use an ofstream (output file stream). ofstreams work similarly to cout. Construct an ofstream like you construct an ifstream, giving the filename as an argument to the constructor:
ofstream output("file.out");
Copy hello.cpp to hello2.cpp, and then modify it to have the following behaviour:
Obtain a single integer as input from the input file "3.in".
Print "Hello world!" (no quotes), to the output file "3.out", unless the integer is 42, in which case "Shh!" (no quotes) should be printed (to the output file).
Compile hello2.cpp into the program "hello_world2" using the "-o" flag to g++, as above.
uname@hostname: ~$ unzip lab0-test.zip uname@hostname: ~$ ls 1.gt 2.in 3.gt a.out hello_world hello_world2 test.py 1.in 2.gt 3.in hello.cpp hello2.cpp lab0-test.zipRun the program test.py it contains:
uname@hostname: ~$ ./test.pyIf you have correctly modified hello.cpp and hello2.cpp as specified above, you will see:
Running test 1... passed Running test 2... passed Running test 3... passed Passed 3 of 3 tests
test.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). For your hello_world2 program, it doesn't need the input/output redirection--your program is reading and writing the files itself.
If you have passed 3 of 3 tests, please ask a TA to take a look, and receive your 1.5 marks for this lab.