ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Chapter 01: Introduction to Computer Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Week 1 Algorithmization and Programming Languages.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 2: Software Design Cycle, Requirements Specification and.
Chapter 1.2 Introduction to C++ Programming
Topic 2 Input/Output.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ECE 264 Object-Oriented Software Development
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Console input.
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
Programming Fundamental-1
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics

Lecture outline Announcements/reminders  Lab 1- Monday session: due 5:00 pm, 02/06  Lab1 – Wednesday Session: due 5:00 pm, 02/01  Lab attendance is required, students must sign your name on the attendance sheet  TA: Justin R Lacle  TA Office hour: 11:00 -12:00 pm, Monday and Wednesday Continue with C++ basics  Review basic I/O  Work through examples  Applying design cycle to ECE /29/2015 ECE 264: Lecture 4 2

Review Covered basic C++ program structure—very similar to C! Namespaces  Introduced std namespace—includes cin, cout, etc.  Could include entire namespace: using namespace std;  Or, just include members being used: using std::cout; Output ( cout ) streams  Can output multiple values in same statement cout << “x=“ << x << “, y=“ << y << endl; Input ( cin ) streams  Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Input value must be compatible with type of x 11/29/2015 ECE 264: Lecture 4 3

4 Another C++ Program: Adding Integers Variable  Is a location in memory where a value can be stored  Common data types (fundamental, primitive or built-in) int – for integer numbers char – for characters double – for floating point numbers  Declare variables with data type and name before use int integer1; int integer2; int sum; 11/29/2015 ECE 264: Lecture 4

Basic program 2 // Adapted from figure 2.5 in text // Addition program that displays the sum of two integers. #include using std::cout; using std::cin; using std::endl; int main() { // variable declarations int number1; int number2; int sum; // prompt user for data and read into appropriate variables cout << "Enter first integer: "; cin >> number1; cout << "Enter second integer: "; cin >> number2; sum = number1 + number2; // add the numbers; store result in sum cout << "Sum is " << sum << endl; // display sum; end line return 0; // indicate that program ended successfully } // end function main 11/29/2015 ECE 264: Lecture 4 5

11/29/2015 ECE 264: Lecture 4 6 output 1,2 4.5 cm _ //Example 1: Determine the output #include using std::cout; using std::cin; using std::endl; #include using std::string; int main() { int i, j; double x; string units = " cm"; cin >> i >> j; cin >> x; cout << "output \n"; cout << i << ',' << j << endl << x << units << endl; return 0; } //Input stream is:

11/29/2015 ECE 264: Lecture 4 7 //Example 2: Determine the output #include using std::cout; using std::cin; using std::endl; int main() { int i, j; double x, y; cin >> i >> j >> x >> y; cout << "First output " << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; return 0; } //Input stream is: First output 1,2,3.4,5 Second output 3,2,2,3 _

Example 2 (cont.) Issues with second output  Before highlighted lines i = 1, j = 2, x = 3.4, y = 5  Second cin appears to assign: x = 2, y = 3, i = 3.4, j = 7  But printing i, j, x, and y in order yields: 3,2,2,3 j never gets value 7—what happens?  i and j are integers  cin reads 3 into i and then stops at decimal point  cin can’t skip non-whitespace character  j doesn’t change 11/29/2015 ECE 264: Lecture 4 8

11/29/2015 ECE 264: Lecture 4 9 Characters and input >> discards leading whitespace get() method used to input whitespace characters  Optional second argument allows you to input multiple characters Default is 1 cin.get(buffer, 10) reads 10 characters from input Example: int x; char ch; cin >> x >> ch; cin >> x; cin.get(ch); xch xch Input stream: 45 c 39 b 45‘c’ 39‘\n ’

Characters and input (cont.) Reading an entire line: getline(char[], num)  Reads up to num characters on a line  Stops at newline character  Example: cin.getline(buffer, 10);  Must be careful if input is read using stream extraction operator ( >> ) as well as getline() 11/29/2015 ECE 264: Lecture 4 10

getline example int numR; char name[20]; cin >> numR; cin.getline(name, 20); If input is: 6 Room 12 what values do numR and name hold?  numR = 6  name = “\n”  why? cin >> numR stops at any whitespace character  \n cin.getline(name,20) starts with next char, ends at newline 11/29/2015 ECE 264: Lecture 4 11

Fixing getline example Skipping whitespace characters: ignore(num)  Discards num characters from input stream without storing them  To fix previous example: int numR; char name[20]; cin >> numR; cin.ignore(1); cin.getline(name, 20); 11/29/2015 ECE 264: Lecture 4 12

Formatted output Recall earlier example: int i, j; double x, y;... cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; x and y are of type double …... but second cout prints 2 & 3 for x & y What if we want to  Always print decimal point?  Always show certain number of places after point?  Force output to align within columns of a particular width?  Change the number base being printed? Use stream manipulators: objects affecting output stream  Already seen one of these: endl  To use others, must add #include May also use stream functions: functions associated with cin/cout 11/29/2015 ECE 264: Lecture 4 13

Integral Stream Base: dec, oct, hex and setbase Change a stream’s integer base by inserting manipulators  hex manipulator Sets the base to hexadecimal (base 16)  oct manipulator Sets the base to octal (base 8)  dec manipulator Resets the base to decimal  setbase parameterized stream manipulator Takes one integer argument: 10, 8 or 16 Sets the base to decimal, octal or hexadecimal Requires the inclusion of the header file  Stream base values are sticky Remain until explicitly changed to another base value 11/29/2015 ECE 264: Lecture 4 14

Example 1: bases // Fig. 15.8: Fig15_08.cpp // Using stream manipulators hex, oct, dec and setbase. #include using std::cin; using std::cout; using std::dec; using std::endl; using std::hex; using std::oct; #include using std::setbase; 11/29/2015 ECE 264: Lecture 4 15

Example 1: bases (cont.) int main() { int number; cout << "Enter a decimal number: "; cin >> number; // input number // use hex stream manipulator to show hexadecimal number cout << number << " in hexadecimal is: " << hex << number << endl; // use oct stream manipulator to show octal number cout << dec << number << " in octal is: " << oct << number << endl; // use setbase stream manipulator to show decimal number cout << setbase( 10 ) << number << " in decimal is: " << number << endl; return 0; } // end main 11/29/2015 ECE 264: Lecture 4 16

Example 2: bases //Example: Determine the output #include using std::cout; using std::endl; using std::hex; using std::oct; int main() { int i; for (i = 0; i <= 32; i += 8) { cout << "Decimal: " << i << endl; cout << oct << "Octal: " << i << endl; if ((i % 16) == 0) cout << hex << "Hexadecimal: " << i << endl; } return 0; } 11/29/2015 ECE 264: Lecture 4 17

Example 2: bases (cont.) Output: Decimal: 0 Octal: 0 Hexadecimal: 0 Decimal: 8 Octal: 10 Decimal: 20 Octal: 20 Hexadecimal: 10 Decimal: 18 Octal: 30 Decimal: 40 Octal: 40 Hexadecimal: 20 11/29/2015 ECE 264: Lecture 4 18 What’s the problem?  Base settings are sticky How would we fix it?  Insert “ dec ” into the cout statement for the decimal values

Questions: What is the difference between endl and \n Std:: endl  Insert a new line + flush the stream buffer \n  Insert a new line In most cases  No much difference  Notes: If you are interested in faster screen output, using \n is faster because flushing the output takes a little time. 11/29/2015 ECE 264: Lecture 4 19

Applying life cycle to ECE 264 How does the software cycle apply to ECE 264?  Project: You’ll be working in teams and taking a project through these stages  Labs: Don’t just sit down and start coding! Read the lab handout  will give you, in essence  Requirements: what should your program be able to do?  Hints about design / programming:  How might you organize your program?  How might you implement that design?  Basic system tests: given particular inputs to your “system” (program), what should the output look like? Identify each of those parts as you read the handout Based on what you read, formulate a high-level design  Given requirements, how are we going to organize the code?  What function(s) do we need?  What class(es) might we use?  We will often provide hints to point you in the right direction  Later labs may ask you to use UML to formalize parts of your design Once high-level design is done and you have a sense of overall program, then start writing code! 11/29/2015 ECE 264: Lecture 4 20

Final notes Next time More on formatted output Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 11/29/2015 ECE 264: Lecture 4 21