In this lab we will write a simple C++ program.
A hello.cpp file (with this exact file name) that takes an hour in the list of arguments and greets the user with "Good morning", "Good afternoon", or "Good night" without the quotes and go to the next line.
The input hour will be in 24 hour clock system (6:00pm will be given as 18:00). 5:00 - 11:59 is considered morning; 12:00 - 18:59 is considered afternoon; and 19:00 - 4:59 is considered night.
The file should run as follows:
uname@hostname:~$ g++ -o hello_world hello.cpp uname@hostname:~$ ./hello_world 9:00 Good morning uname@hostname:~$ ./hello_world 11 Good morning uname@hostname:~$ ./hello_world 12:00 Good afternoon uname@hostname:~$ ./hello_world 12:10 Good afternoon uname@hostname:~$ ./hello_world 18 Good afternoon uname@hostname:~$ ./hello_world 22:10 Good night uname@hostname:~$ ./hello_world 3:30 Good night uname@hostname:~$ ./hello_world 5 Good morning
Make sure the output is as expected before you submit your code.
All C++ programs start their execution at the main function. This is similar to Java, though different in that the C++ main function is not contained in a class. Here is a set of instructions to create a very simple main function that prints Hello world!.
You should be logged onto Ubuntu.
Open a terminal window. To do so, click on the "Applications" menu located on the top left of the desktop, and select its "Utilities" option, then the "Terminal" option.
At the command line, type pwd (print working directory) to see which directory you're in. In the following examples, the string ending in ~$ is assumed to be the command line 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 CMPT 225 in which you will store all your CMPT 225 work (labs and assignments) throughtout the semester. For example, call it cmpt-225.
uname@hostname:~$ mkdir cmpt-225
Change directory to your cmpt-225.
uname@hostname:~$ cd cmpt-225
Make a directory for the files for this lab, for example, call it Lab0.
uname@hostname:~$ mkdir Lab0
Change to this directory using cd (change directory):
uname@hostname:~$ cd Lab0
Open an editor such as "Sublime Text", which can be opened at the command prompt ("subl &") or from the "Applications" then "Programming" menu options. You could also use "vim", "gedit", "jedit", "emacs" 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.
Type ls (list) to see the contents of the current directory. hello.cpp should appear:
uname@hostname:~$ ls hello.cpp