The C++ Language. ! What happens when executing the following code? int anint; Cout << "The integer is " << anint << endl;

Similar documents
The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

CPE Summer 2015 Exam I (150 pts) June 18, 2015

LECTURE 02 INTRODUCTION TO C++

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

Chapter 2: Overview of C++

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Engineering Problem Solving with C++, Etter

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

READ THIS NOW! Do not start the test until instructed to do so!

Your First C++ Program. September 1, 2010

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

a data type is Types

Chapter 4 - Notes Control Structures I (Selection)

Memory and Pointers written by Cathy Saxton

Copyright 2003 Pearson Education, Inc. Slide 1

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Pointers, Dynamic Data, and Reference Types

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

CSCI 1061U Programming Workshop 2. C++ Basics

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

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

Objectives. In this chapter, you will:

2 nd Week Lecture Notes

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Input And Output of C++

Getting started with C++ (Part 2)

Types, Values, Variables & Assignment. EECS 211 Winter 2018

Chapter 2: Introduction to C++

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

CSCE 121 ENGR 112 List of Topics for Exam 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Chapter 2: Basic Elements of C++

REVIEW. The C++ Programming Language. CS 151 Review #2

Week 5: Files and Streams

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

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

CS242 COMPUTER PROGRAMMING

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

If Control Construct

Introduction to the C++ Programming Language

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Algorithms and Programming I. Lecture#12 Spring 2015

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

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

Integer Data Types. Data Type. Data Types. int, short int, long int

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

Chapter 2. C++ Basics

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Strings and Stream I/O

The C++ Language. Arizona State University 1

Separate Compilation Model

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Why Is Repetition Needed?

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Array Elements as Function Parameters

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

1. In C++, reserved words are the same as predefined identifiers. a. True

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

A SHORT COURSE ON C++

Programming with C++ as a Second Language

Cours de C++ Introduction

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Definition Matching (10 Points)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

This watermark does not appear in the registered version - Slide 1

Topics. Functions. Functions

Chapter 4: Control Structures I (Selection)

Name Section: M/W T/TH Number Definition Matching (6 Points)

C++ for Python Programmers

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS Final Exam Review Suggestions - Fall 2017

C++ Input/Output: Streams

CIS 130 Exam #2 Review Suggestions

Increment and the While. Class 15

CS Final Exam Review Suggestions - Spring 2014

Transcription:

The C++ Language Variable Initialization and Strings Unitialized Variables! Recall that variables are declared before they are referenced.! What happens when executing the following code? int anint; Cout << "The integer is " << anint << endl; 2 1

Unitialized Variables! Variables that are referenced before assigning a value to them are unitialized variables. Used on the right hand side of an assignment Used in an output statement Used as a parameter to a function (getline)! Dangerous and unpredictable Usually see a Visual C++ warning! Syntactically OK, but semantically bad. 3 Initializing Variables! In C++, variables can be initialized with an assignment statement just after the declaration. int anint; anint = 0; // So we know what value anint has.! C++ also provides a shorter mechanism int anint = 0; int anint1 = 0, anint2 = 0; 4 2

Initializing Variables! Variables should always be initialized Safety Debugging! Use a special value that is known, easy to recognize, but not expected. 5 Strings! We've seen string variables and constants several times. How do we tell the compiler we want to use strings?! Strings are sequences of characters! What other kinds of things might we do with strings? initialize length empty concatenate 6 3

String Initialization! string variables can be initialized with literal constants string president = "George Bush";! Cannot break lines in the middle of a string literal string opening = "In a hole in the ground there lived a hobbit. Not a nasty, dirty, ";! Unterminated string error from the compiler. 7 Escape Sequences! Strings can contain escape sequences, which are interpreted when printed. string opening = "In a hole in the ground\nthere lived"; cout << opening << endl; In a hole in the ground there lived 8 4

String Length! You can find out how many characters are in a string by using the length function for strings.! Syntax! Semantics StringVariable.length() Evaluates to the number of characters in the string. 9 String Length (Examples) string name = "Fred Flintstone"; int namelen = name.length(); cout << "The length of the name is: " << namelen << endl; // Can be part of an expression as well int linelen; cout << name; linelen = name.length() + linelen; 10 5

String Length! Problem: Assuming names are no longer than 15 characters, print an age right-justified at column 20. Name Age Mary Baker 27 20 11 const int AGE_COLUMN = 20; // Where to justify the age. cout << name; cout << setw(age_column - name.length()) << age; String Length (Exercise)! Assuming that lines are 80 characters wide and the string to print is less than 80 characters, what C++ statements center a string on a line? 12 6

Empty Strings! There is a special string called the empty string "" Can test if a string is empty by using the empty function for strings.! Syntax! Semantics StringVariable.empty() Evaluates to true if the string in StringVariable is empty or false otherwise. 13 Empty Strings (Examples) string emptystring = ""; string name = "Fred Flintstone"; bool isempty; isempty = emptystring.empty(); isempty = name.empty(); // true // false! Unitialized string variables are not necessarily empty! 14 7

Concatenating Strings! Two strings can be put together or concatenated by using the + operator. Can update string variables too string s1 = "Hello ", s2 = "World!"; cout << s1 << s2 << endl; cout << s1 + s2 << endl; string s3 = s1 + s2; cout << s3 << endl; // Prints Hello World! // Also prints Hello World! // Again prints Hello World! 15 Strings and File Names 16! You may want to use a string variable or constant to store a file name.! The open function for file streams require C style strings, not C++ style strings.! Use the c_str() function for strings to get a C style string.! Syntax StringVariable.c_str() 8

Strings and File Names (Examples) const string IN_FILENAME = "InputData.txt"; // Opens the file named InputData.txt for input ifstream In; In.open(IN_FILENAME.c_str()); // alternatively, you can declare and open a file stream // all at once ifstream In(IN_FILENAME.c_str()); 17 9