Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

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

Chapter 2. C++ Basics

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

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

Engineering Problem Solving with C++, Etter/Ingber

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

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

What we will learn about this week:

CS2141 Software Development using C/C++ Stream I/O

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to Programming EC-105. Lecture 2

Chapter 2. Outline. Simple C++ Programs

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Fundamentals of Programming CS-110. Lecture 2

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

LECTURE 02 INTRODUCTION TO C++

Programming. C++ Basics

BITG 1233: Introduction to C++

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

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

C++ Basics 1 CS 16: Solving Problems with Computers I Lecture #3

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

Chapter 2: Introduction to C++

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

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Getting started with C++ (Part 2)

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

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

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

2 nd Week Lecture Notes

Introduction to Programming

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

The C++ Language. Arizona State University 1

Week 3: File I/O and Formatting 3.7 Formatting Output

Expressions, Input, Output and Data Type Conversions

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Introduction to Programming using C++

Chapter 12. Streams and File I/O

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++

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

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

6.096 Introduction to C++ January (IAP) 2009

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

A First Program - Greeting.cpp

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

Chapter 1 Introduction to Computers and C++ Programming

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

Chapter 21 - C++ Stream Input/Output

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

Chapter 21 - C++ Stream Input/Output

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

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Programming with C++ as a Second Language

I/O Streams and Standard I/O Devices (cont d.)

Chapter 2: Overview of C++

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

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

CMPS 221 Sample Final

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Chapter 3 - Notes Input/Output

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Creating a C++ Program

Lecture 2 Tao Wang 1

C++ Input/Output: Streams

Full file at

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CHAPTER 3 BASIC INSTRUCTION OF C++

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CSCI 123 Introduction to Programming Concepts in C++

Lecture 4 Tao Wang 1

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Basic Elements of C++

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Computer Programming : C++

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

UEE1303(1070) S 12 Object-Oriented Programming in C++

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

Chapter 3. Numeric Types, Expressions, and Output

Introduction to C++ (Extensions to C)

by Pearson Education, Inc. All Rights Reserved. 2

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Chapter 3 : Assignment and Interactive Input (pp )

C++ Programming Lecture 10 File Processing

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

UNIT- 3 Introduction to C++

CHAPTER 3 Expressions, Functions, Output

Your First C++ Program. September 1, 2010

Transcription:

Lecture 3 Input and Output Review from last week Variable - place to store data in memory identified by a name should be meaningful Has a type- int double char bool Has a value may be garbage change value using assignment statements: Variable = Expression 1

Constants - Use a constant instead of a number Easier to change a single constant declaration than to find all uses of the constant in your program. Easier to read and understand the program Remember to use const in the declaration to prevent accidental modification of the value Arithmetic Operations +,-,*,/ Follow Rules of Precedence Appendix 2 Integer division dividing by two integers will result in an integer even if there is a remainder, regardless of the variable s type. If one of the variables in an expression is double, the result will be double 2

Arithmetic with + = and * behaves as expected for both integer and floating point types. Arithmetic for / behaves as expected for floating point numbers. Arithmetic for / behaves in a somewhat surprising way for integer types (short, int, long) Division for integers is TRUNCATING - it discards the fractional part. Division /, and modulus % are complementary operations. Mod, or modulus, %, works ONLY for integer types. 4 14 / 3 = 3 NOT 4.66 3 14-12 2 14 % 3 = 2 Programming Style Grouping things that belong together Indenting Leaving a blank line Comments // comment follows until end of line /* multi-line comments must end with another */ Header use the template for your starter program for all lab assignments 3

Program Template Header Include and Using directives Variable and Constant Declarations Output Identification Hints Read the Problem! Listen in class! What are the requirements? Ask Questions! 4

Streams and Basic I/O stream of characters - bytes input streams and output streams cin and cout are streams cin is connected to the keyboard cout is connected to the console window We will later look at streams that go to a file Input and Output cout is the output stream, read See-Out.. It is attached to the monitor screen. << is the insertion operator cin is the input stream, read see-in, and is attached to the keyboard. >> is the extraction operator. cin and cout are defined in the iostream library. cout << Press return after entering a number.\n ; cout << Enter the number of pods:\n ; cin >> number_of_pods; cout << Enter the number of peas in a pod.\n ; cin >> peas_per_pod; The first two lines sends a request to the user. The third gets an integer value (Number_of_pods) from the user. 5

COUT Strings of text and values of variables may be output to the screen: cout << num_of_bars << candy bars\n ; OR cout << num_of_bars; cout << candy bars\n ; OR cout << num_of_bars << candy bars << endl; Well spaced output 6

Poorly spaced output: CIN Getting input from the keyboard: cout << Enter the number of bars in a package\n ; cin >> num_of_bars; 7

Escape Sequences New line \n (like endl) horizontal tab \t alert \a backslash \\ double quote \ The \ (backslash) preceding a character tells the compiler that the next character does not have the same meaning as the character by itself. An escape sequence is two characters with no space between them. \\ is a real backslash character, not the escape character, a backslash that does not have the property of changing the meaning of the next character. Directives #include <iostream> using namespace std; Makes the library iostream available. iostream includes the definitions cin and cout. Made part of your program in the linking process. 8

Display 2.1 page 38 #include <iostream> using namespace std; int main( ) { int number_of_bars; double one_weight, total_weight; //variable declaration cout << "Enter the number of candy bars in a package\n"; cout << "and the weight in ounces of one candy bar.\n"; cout << "Then press return.\n"; cin >> number_of_bars; cin >> one_weight; total_weight = one_weight * number_of_bars; cout << number_of_bars << " candy bars\n"; cout << one_weight << " ounces each\n"; cout << "Total weight is " << total_weight << " ounces.\n"; cout << "Try another brand.\n"; cout << "Enter the number of candy bars in a package\n"; cout << "and the weight in ounces of one candy bar.\n"; cout << "Then press return.\n"; cin >> number_of_bars; cin >> one_weight; total_weight = one_weight * number_of_bars; cout << number_of_bars << " candy bars\n"; cout << one_weight << " ounces each\n"; cout << "Total weight is " << total_weight << " ounces.\n"; cout << "Perhaps an apple would be healthier.\n"; } return 0; 9

Enter the number of candy bars in a package and the weight in ounces of one candy bar Then press return. 11 2.1 11 candy bars 2.1 ounces each Total weight is 23.1 ounces. Try another brand. Enter the number of candy bars in a package and the weight in ounces of one candy bar Then press return. 12 1.8 12 candy bars 1.8 ounces each Total weight is 21.6 ounces. Perhaps an apple would be healthier. Take a Break!!! 10

Formatting Decimals (page 51 & 216) The magic formula: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //fixed decimal point // pad with zeros //round to 2 decimal places Member functions setf() (set flags) - unsetf() (unset flags) for floating point numbers: ios::fixed, (always show the number as a fixed point number) ios::scientific, (always show the number in scientific notation) ios::showpoint, (always show the decimal point) width(n) (set the width of the following field to n characters) More flags for any type of value: ios::left, ios::right, (left or right justify the value in the specified field) precision(), (set the number of places to show for a floating point number) 11

Formatting Flags Flag ios::fixed ios::scientific ios::showpoint ios::left ios::right Purpose Display floating point numbers in fixed format Display floating point numbers in scientific format Always show the decimal point of a floating point number Display the data left justified in the field Display the data right justified in the field Manipulators setiosflags() same as setf() resetiosflags() same as unsetf() setw() same as width() setprecision() same as precision() 12

Formatting Commands Purpose Function Manipulator Set flag setf() setiosflags() Unset flag unsetf() resetiosflags() Set field width width() setw() Set number of digits precision() setprecision() Reading Section 2.4 pages 65-74 Sections 7.1 and 7.2 pages 335 364 13