Tutorial 2: Compiling and Running C++ Source Code

Similar documents
Tutorial 1: Unix Basics

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

1) Log on to the computer using your PU net ID and password.

Your First C++ Program. September 1, 2010

Understanding main() function Input/Output Streams

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Program Organization and Comments

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

CHAPTER 3 BASIC INSTRUCTION OF C++

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

1 Unit 8 'for' Loops

The C++ Language. Arizona State University 1

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Chapter 2: Overview of C++

4. Structure of a C++ program

Getting started with C++ (Part 2)

Probably the best way to start learning a programming language is with a program. So here is our first program:

Lab: Supplying Inputs to Programs

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

We first learn one useful option of gcc. Copy the following C source file to your

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Chapter 1 INTRODUCTION

Lab # 02. Basic Elements of C++ _ Part1

Scientific Computing

Chapter 1 Introduction to Computers and C++ Programming

Unit 7. 'while' Loops

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Unit 13. Linux Operating System Debugging Programs

Chapter 2 C++ Fundamentals

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

My First Command-Line Program

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Unit 10. Linux Operating System

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Chapter 2 Basic Elements of C++

Objectives. In this chapter, you will:

Introduction to Programming

clicking on the on the New

CS101 Linux Shell Handout

CSCE Practice Midterm. Data Types

Getting started with UNIX/Linux for G51PRG and G51CSA

Introduction to Scientific Computing with C++ Introduction Textbook Assignments Lab Accounts and Software HELLO ADD INTS MANDEL Conclusion

BITG 1233: Introduction to C++

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

CMPS 221 Sample Final

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

C++ Support Classes (Data and Variables)

Using the Dev C++ Compiler to Create a Program

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CS2141 Software Development using C/C++ Compiling a C++ Program

assembler Machine Code Object Files linker Executable File

1) You want to determine whether time has run out. The following code correctly implements this:

Software and Programming 1

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

C++ for Python Programmers

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Programming in Microsoft Visual C++ Programming in Microsoft Visual C++

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Chapter 2. Editing And Compiling

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Tiny Instruction Manual for the Undergraduate Mathematics Unix Laboratory

2 nd Week Lecture Notes

Computer Programming : C++

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Compiling C/C++ programs

Chapter 1 Introduction to Computers and Programming

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

UEE1302 (1102) F10: Introduction to Computers and Programming

COMP s1 Lecture 1

Why Is Repetition Needed?

First C or C++ Lab Paycheck-V1.0 Using Microsoft Visual Studio

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction. Overview of 201 Lab and Linux Tutorials. Stef Nychka. September 10, Department of Computing Science University of Alberta

PYOTE installation (Windows) 20 October 2017

Lecture 3 Tao Wang 1

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

How to approach a computational problem

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Intermediate Programming, Spring Misha Kazhdan

Fundamentals of Programming CS-110. Lecture 2

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

MICROSOFT VISUAL STUDIO AND C PROGRAMMING

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Transcription:

Tutorial 2: Compiling and Running C++ Source Code 1. Log in to your UNIX account as you did during the first tutorial. 2. Click on the desktop with the left mouse button and consider the menu that appears before you. 3. Select utilities. 4. Select Nedit. As I m sure you remember, this is the text editor with which you will be writing your C++ code. 5. Type the following: #include <iostream> using namespace std; int main() { cout << Hello World! ; cout << endl; return 0; } 6. Now click on File at the top of the Nedit window. 7. Select Save As. The directory in which your file should be saved appears by default. As before, you do not need to modify this directory. In the space provided, name your file hello.cpp Congratulations! You have just written your first C++ program; now to compile. 8. Go back to your xterm window and type: g++ -pedantic -o hello hello.cpp 9. Press enter. (In case it is not clear, there is a space between g++ and -o, and the -o is the letter o, not a zero.) This will compile the program you have just written using the g++ compiler. Using pedantic makes the compiler check your code against the C++ standard. The filename after the -o indicates the name of the executable file (i.e. runnable file) you would like created after compiling your source code (hello.cpp) successfully. If you don t specifiy the o hello, a file called a.out will be created automatically. If the compilation was successful, you will simply get your command prompt again (if you see some other messages displayed, they are errors see item 11).

10. Now that your program is compiled, you need only run it in order to view the results. To do this type the following in the xterm window and press enter:./hello 11. If the compiler found errors in your code, it will display them. For example, if you forget an ending quotation mark in your output string (i..e cout << Hello World!;), you will receive the following error after compiling: [dea] [/home/a_gombar] > g++ -pedantic -o hello hello.cpp hello.cpp:6: warning: string constant runs past end of line hello.cpp:6: unterminated string or character constant hello.cpp:6: possible real start of unterminated constant Note that your prompt will differ from [dea] [/home/a_gombar] > You should recreate this error to see how errors are displayed with g++. Don t forget to save your changes before compiling!! Shortcut: Press Control-S in Nedit to save your changes. Don t forget to save any modifications to your source code before recompiling, otherwise only your last changes will be compiled by g++. An easy way to see if you have saved your file in nedit is to look at the filename displayed at the top left corner of the Nedit window. If the word (modified) is displayed after the filename, then you have modified the file and not saved it yet. If you save the file (File Save or Ctrl-S) you will see that (modified) will be gone. 12. The line number that the error was detected is also displayed. In the above case, the error was found on line 6: hello.cp:6: It would be nice to view line numbers in Nedit, which can be done as follows: i. In Nedit, go to Preferences Default Settings and click on Statistics Line. ii. Exit the Nedit program (File Exit). iii. A message box is displayed stating Default Preferences have changed. Click on Save to accept the changes. iv. Restart Nedit. There should be an information line just below the top menu that displays the line and column position for the current cursor location.

Now let s try prompting the user for an input and then displaying what they entered. We re also going to add some comments to our program because we want to develop good programming habits. 1. From Nedit, click File and select New from the drop down menu. Then type the following: //input.cpp Prompts the user for input #include <iostream> // required for cin/cout using namespace std; int main() //start of main function { float input; //input: float variable //input by user //Get the input cout << "Enter a float number: "; cin >> input; //Display the input cout << "You entered " << input << endl; return 0; } Save the file as input.cpp. Return to xterm window, compile and run. g++ -pedantic -o input input.cpp./input When it runs, notice that you are prompted to enter a number. Type 2.9 and press enter. Notice that 2.9 is the output. If error messages are displayed then you will need to debug your source code (i.e. check for errors). Do not panic, this is a normal part of the programming process. You will often spend more time debugging then you do writing the actual program and that s okay.

Some easy debugging fixes. 1. Check your brackets and parentheses. Make sure that every bracket/parentheses opened is also closed {} ( ) 2. Quotation marks. Like brackets, what is opened must be closed. 3. Make sure that every line of code ( not comments // ) ends with a semicolon ; 4. Make sure that comment lines begin with TWO forward slashes // 5. Make sure that when you use cin >> and cout << properly, so for example, cin << will cause an error because the << is facing the wrong way (>> is the stream extraction operator (cin) and << is the stream insertion operator (cout)). 6. It may happen that when you look at the line number where the compiler found an error, you don t see any mistakes. Sometimes the error is actually just above the line number displayed, so look a few lines above for a mistake. Many times a missing semicolon is the culprit! 7. Sometimes you will get overwhelmed by very many errors. But in certain situations correcting one error can correct a cascade of other related errors. 8. Build your program in a piecewise fashion. In other words, build a basic program that can run (i.e. just a main() function). Then add functionality by coding a small component of your complete program. Compile and test what you have so far and once you re satisfied with it, add another component. This is also a very good method when you doing the online exam. If you try to complete most of your program in one sitting, you will be inundated with errors!! Also, an error in one part of your program might create errors in other parts.

As an exercise, compile and run the following program that includes some errors. Type and compile the file as is so you get a feel for the type of error messages you can receive. Then correct the errors and recompile until the program is running correctly. Shortcut: instead of retyping the code, select it with your mouse and then paste it into nedit using the middle mouse button The program requires the user to enter a number in pounds and it will be converted to kilograms. Test your program by inputting 10. Your answer should be 22. // Program converts pounds to kilograms #include <iostreaam> using namespace std; int main() { const float LBSTOKGS = 2.2 float numpounds, numkilograms; // pounds in 1 kg // Request weight (lbs) from user cout << "Enter weight in pounds << to convert to kilograms: "; cin << numkilograms; // convert pounds to kilograms numkilograms = numpounds * LBSTOKGS; // Output converted value cout << "The weight in kilograms is " << numkilograms; cout << endl; } return 0; Note the good use of spacing and indenting to make the program clear to read. To help you along, the above program contains 5 errors: 4 syntax errors and 1 logic error. A syntax error is a mistake in using the C++ language (e.g. writing ciin instead of cin), whereas a logic error is when your program runs, but does not work as expected. Logic errors will be discovered once you fix any syntax

errors and your program is able to run. For instance, a logic error would be subtracting two numbers when you meant to divide them. In this case your program would run, but you would have an incorrect answer. Close your xterm window by typing exit in the xterm window and press enter. Close Nedit. Quit your session. Good Work! See you next week!