Hello world!
A first program in C++ Goals
● By the end of this lesson you will be able to: – Log on to a remote server from your computer – Enter basic Linux commands – Use a text editor in Linux – Write a computer program – Compile and execute a computer program Beginning
● A little bit of everything at once – Terminal emulation – Linux usage – Text editors – Compilers – Execution – Debugging Terminal emulation
● Terminal emulator: program to connect from your computer to a terminal (command line or shell)
● CS department has Linux servers that will be used for programming and submitting assignments
● PuTTY is a popular terminal emulator for Windows, while you can connect from the terminal in OS X Server access
● CS server is bobby.cs.unlv.edu
● You can connect via the secure shell (SSH) protocol
● If you do not have an account, visit tux.cs.unlv.edu for more information PuTTY installation
● To install PuTTY, visit putty.org and download & run the 32-bit or 64-bit MSI installer
● Run PuTTY, then enter the following data: – Host Name: bobby.cs.unlv.edu – Connection type: SSH (this should modify the port to 22) – Saved Sessions: bobby – In Connection:
● Seconds between keepalives: 30 – In Connection → Data:
● Auto-login username: your username ● Go back to the Session screen, hit Save, select your bobby saved session, and click open
● You will get a warning message about security and keys, but just accept it Connecting via macOS
● Open a terminal by going to Finder → Applications → Utilities → Terminal
● Type ssh [email protected]
● You may get a message about keys, security, etc. – just type yes – this will only happen the first time Transferring files
● You may want to transfer files between the server and your computer, including for printing purposes
● For Windows, I recommend WinSCP (use SFTP or SCP)
● For macOS, I recommend Cyberduck
● Download and install, enter server information, and pick either SCP or SFTP as your protocol Why use CS department servers?
● Familiarity with Linux shells is a very useful skill to have
● Future courses and future jobs will utilize Linux
● You can log in from almost anywhere you have an Internet connection, including UNLV lab computers
● To submit your programs
● To access my shared programs and data
● It provides a common familiar platform to use/discuss all necessary things related to programming Using Linux
● Linux can be graphical in the same way that Windows is, but when logging in to a terminal everything is text-based
● You will interact with the server by typing various commands
● Some examples: – ls – to view a list of the files in the current directory – nano – to open a text editor to write a program in – g++ – to compile a program that you have written Text editors
● Text editor: a program that allows you to view/edit text
● We will use a text editor to write programs
● Common Linux text editors for programming: – nano – vim – emacs ● These editors have features that make programming easier Compiling & executing
● Compiler: A program that turns a program that you write into something that the computer can run
● The compiler will either successfully compile or produce an error with details
● Upon successful compilation, the program can be executed
● The standard Linux compiler for C++ is called g++ Steps for writing your first program
0) Download a terminal emulator (PuTTY) 1) Log on to a server (bobby) 2) Open a text editor (nano) 3) Type in a program 4) Save & exit text editor 5) Compile program (g++) 6) Execute program What is “Hello world!”?
● “Hello world!” refers to a very basic program that prints out a simple message to the screen that says Hello world!
● For both experienced and beginner programmers, such a program illustrates: – Basic syntax – Basic style – The bare minimum needed to write a program that does something useful ● You do not need to fully understand the what and why of every part of the program right now Hello world!
Line Program number 1 #include
Line Program number 1 #include
Line Program number 1 #include
Line Program number 1 #include
Line Program number 1 #include
Line Program number 1 #include
Line Program number 1 #include
● After you have typed the program into a text editor, save the file
● When writing C++ programs it is standard, though not required, to name your files with a .cpp extension: – hello.cpp – assignment_1.cpp – 1.cpp ● Suppose you named your file hello.cpp – to compile you will type g++ hello.cpp Compiling the program
● If compilation was successful then there will be no output – the next thing you see will be your shell prompt
● If there was a problem with the program the compiler will display an error message
● If you get an error message, double check everything – you may have misspelled something (e.g. cuot instead of cout) or left out a semicolon Running the program
● If the program compiles successfully, the compiler will produce an executable file called a.out
● You can execute a.out by typing ./a.out
● You should see the text Hello world! displayed on the terminal Exploration
● What happens if you type diferent things between the quotes?
● What happens if you add/remove certain lines or characters?
● What happens if you add/remove spaces or blank lines?