C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.

Slides:



Advertisements
Similar presentations
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 CS161 Introduction to Computer Science Topic #13.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CS161 Introduction to Computer Science Topic #3.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
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.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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 CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Learners Support Publications Introduction to C++
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Program Input and the Software Design Process ROBERT REAVES.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
LESSON 2 Basic of C++.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Basic Elements Skill Area 313 Part B. Lecture Overview Basic Input/Output Statements Whitespaces Expression Operators IF Statements Logical Operators.
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.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Topic Pre-processor cout To output a message.
Electrical Engineering Department 1st Year Programming Languages
Chapter 1.2 Introduction to C++ Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
Chapter 3: Expressions and Interactivity.
Introduction to C++ October 2, 2017.
Data Streams.
Basic Elements of C++ Chapter 2.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
2.1 Parts of a C++ Program.
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 3: Input/Output
Introduction to cout / cin
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Using string type variables
Strings Skill Area 313 Part C
Introduction to cout / cin
Chapter 1 c++ structure C++ Input / Output
CSE Module 1 A Programming Primer
Presentation transcript:

C++ Basics #7 Basic Input and Output

In this video Standard output (cout) Standard input (cin) stringstream

Input and Output? Interaction with the user. Changing program according to user input. C++ uses streams to perform input and output. Stream is object where an object can either insert or extract characters to/from it. C++ have iostream file in library where the input and output streams objects are declared.

Standard output (cout) Default standard output is screen. To give output we use ‘cout’ with << (Two less than signs) Example: cout << 23; (gives 23 in screen) cout << x; (prints the value of x to screen) cout << “Hello world!” (prints Hello World to screen)

cout continues… Remember “hello” and hello are different when printing or doing cout. Example cout << “hello”; (prints hello to screen) cout << hello; (prints contents of hello variable to screen) Multiple << can be written in same statement Example: cout << “Hello ” << “World”; Multiple << can be used with combination of variables and strings. Example: cout << “My age is: “ << age; Will give: My age is: 32 (if age = 32).

cout continues… Writing cout in multiple lies does not give multiline outputs. Example: cout << “This is a string.”; cout << “This is another string”; Will give as: This is a string.This is another string. Using endl or “\n” will take output to next line. Example: cout << “This is a string.\n”; or cout << “This is a string.” << endl; cout << “This is another string.”; Will give: This is a string. This is another string

Standard input (cin) cin is used with >> to get a input from the stream. Example: cin >> VariableName(To store the input) To request more than one datum we can use multiple >> Example: cin >> data1 >> data2; Which is same as cin >> data1; cin >> data2;

cin countinues When getting string with cin, it will stop taking character as soon as it gets to a blank space. Example: cin >> AString; And user inputs Hello world, then only Hello will be taken into AString. For this we will use a function called getline as following. Example: getline(cin, Astring); Using same variable to get input in different places will just replace the previous value of variable with new one.

stringstream Header file defines the class stringstream that allows a string-based objects to be treated as a stream. Especially useful to convert numerical value or string and viceversa Example: string UserInput; float NumValue = 0; getline(cin, UserInput); stringstream(UserInput) >> NumValue; This is extract the number from UserInput and store to NumValue.

Please rate, comment and subscribe Done with basics Next: Control structures and functions and codes!! Please visit for more materials to learn  Quizzes, challenges, articles and news.