Introduction to cout / cin

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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:
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.
Expressions and Interactivity. 3.1 The cin Object.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
FALL 2001ICOM Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Lecture 3 Expressions, Type Conversion, Math and String
C++ First Steps.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic Expressions Function Calls Output
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
Chapter 3: Expressions and Interactivity.
getline() function with companion ignore()
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Introduction to C++ Programming
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.
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Introduction to cout / cin
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Fundamental Programming
What Actions Do We Have Part 1
An Introduction to Programming with C++ Fifth Edition
Chapter 2 part #3 C++ Input / Output
Reading Data From and Writing Data To Text Files
Lecture 3 Expressions, Type Conversion, and string
Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
getline() function with companion ignore()
Programming Fundamental-1
Presentation transcript:

Introduction to cout / cin

Purpose cout is used to print to the screen in a C++ Console Application. cin is used to read user input from the keyboard in a C++ Console Application.

#includes cout and cin are defined by the C++ standard library iostream. To use these utilites: #include <iostream> Additional utilities for formatting cout are in: #include <iomanip>

Basic Idea cout is a stream from the application (executing in memory) to the screen. Characters are inserted into the stream and “fall out” on the screen. I/O manipulations may be used to format the characters before they are written.

Basic Idea cout is a stream from the application (executing in memory) to the screen. Characters are inserted into the stream and “fall out” on the screen. I/O manipulations may be used to format the characters before they are written.

Basic Idea

Basic Idea

cout syntax cout << expression; where expression is a literal, variable or more complex expression that evaluates to one value.

cout syntax cout << expr1 << expr2 << ... << exprn; is equivalent to: cout << expr1; cout << expr2; ... cout << exprn;

cout style Can vary greatly depending on the situation. Make it clear to another programmer and easy to maintain.

cout style Good style: cout << ”+-------+\n”; cout << ”| Hello |\n”;

cout style Good style: cout << ”+-------+\n” << ”| Hello |\n” << ”+-------+\n”;

cout style Poor style: cout << ”+-------+\n| Hello |\n+-------+\n”;

iomanip Floating Point Numbers: // print floats/doubles in decimal format with // 2 digits past the decimal point. // Prints nothing. In effect until changed. cout << fixed << setprecision(2);

iomanip Field Widths: cout << setw(10) << ”Hello”; // set total field with of a value cout << setw(10) << ”Hello”; Prints 5 spaces then 5 characters: Hello

iomanip Field Widths: Prints 5 spaces then 5 characters then There // effect is only for 1 value cout << setw(10) << ”Hello” << ”There”; Prints 5 spaces then 5 characters then There HelloThere

Basic Idea cin is a stream from the keyboard to the console application (executing in memory) Characters are inserted into the stream by the user and are extracted by cin.

Basic Idea

cin Syntax: (simple) Semantics: in general cin >> variable; Read as: variable = whatever is entered by the user;

cin Semantics: in general - halts execution of the program - the user enters data and presses ENTER - the data is converted to the data type of the variable. - the data entered is stored in the variable - execution continues

cin Semantics: specifically Exactly how cin behaves depends on the: - data type of the variable - what is currently in "the stream", if anything. - data type of the data entered by the user

More Details Coming Soon! cin More Details Coming Soon! For now, assume that for each cin: - the user enters the correct data type - the user enters NO SPACES/TABS/etc - the user presses ENTER when done

cin examples

cin examples

cin examples