Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input
|
|
- Clarissa Johns
- 6 years ago
- Views:
Transcription
1 Input and Output Outline Formatting outputs String data type Interactive inputs File manipulators CS 1410 Comp Sci with C++ Input and Output 1 CS 1410 Comp Sci with C++ Input and Output 2 No I/O is built into C++ instead, a library provides input stream and output stream Keyboard fstream istream executing program fstream ostream Screen <iostream> Header File Access to a library that defines 3 objects An istream object named cin (keyboard) An ostream object named cout (screen) An ostream object named cerr (screen) 3 4 1
2 Output Output in computer programming means to display literal values that contain in variables either to the screen (standard output) or to a file. Appearance of the output can be controlled through the process called formatting. Output Statement cout << Expression or Manipulator; cout << Expression or Manipulator; cout << ; or cout << Expression or Manipulator << Expression or Manipulator << ; << : insertion operator or symbol cout << directs the expression after it to the screen cout<<"hello world!!! "; cout<<"the area of the triangle is << Area; CS 1410 Comp Sci with C++ Input and Output 5 CS 1410 Comp Sci with C++ Input and Output 6 Header File <iostream> for a library that defines 3 objects an istream object named cin (keyboard) an ostream object named cout (screen) an ostream object named cerr (screen) Insertion Operator (<<) the insertion operator << takes 2 operands the left operand is a stream expression, such as cout the right operand is an expression of simple type, or a string, or a manipulator CS 1410 Comp Sci with C++ Input and Output 7 CS 1410 Comp Sci with C++ Input and Output 8 2
3 Header File for Output Object To use output object cout the header file iostream must be included in the source program and directive using for referencing cout, i.e., #include <iostream> Example #include < iostream > /* This program computes the sum of two numbers. */ int main(void) double Number1, Number2, Sum; cout << "Enter two numbers --> "; cin >> Number1 >> Number2; Sum = Number1 + Number2; cout<< "The sum of " << Number1 << " and " << Number2 << " is "<< Sum; cin.get(); cin.get(); return 0; CS 1410 Comp Sci with C++ Input and Output 9 CS 1410 Comp Sci with C++ Input and Output 10 Output Display Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used to control output format endl is use to terminate the current output line, and create blank lines in output CS 1410 Comp Sci with C++ Input and Output 11 CS 1410 Comp Sci with C++ Input and Output 12 3
4 Manipulators in iostream endl: terminate the output line fixed: display in decimal point format with 6 decimal places as default --.XXXXXX showpoint: display a whole number in decimal format with 6 significant digits as default XX.XXXX X.XXXXX XXXX.XX Example #include < iostream > /* This program computes the sum and product of two numbers. */ int main(void) double Number1, Number2, Sum, Product; cout << "Enter two numbers --> "; cin >> Number1 >> Number2; Sum = Number1 + Number2; Product = Number1 * Number2; cout<< "The sum of " << Number1 << " and " << Number2 << " is "<< Sum << endl; endl manipulator cout<< "The product of " << Number1 << " and " << Number2 << " is "<< Product; cin.get(); cin.get(); return 0; CS 1410 Comp Sci with C++ Input and Output 13 CS 1410 Comp Sci with C++ Input and Output 14 Output Display with endl Output Display with showpoint #include < iostream > /* This program computes the sum and product of two numbers. */ int main(void) double Number1, Number2, Sum, Product; cout << "Enter two numbers --> "; cin >> Number1 >> Number2; Sum = Number1 + Number2; Product = Number1 * Number2; cout<<showpoint; showpoint manipulator cout<< "The sum of " << Number1 << " and " << Number2 << " is "<< Sum << endl; cout<< "The product of " << Number1 << " and " << Number2 << " is "<< Product; cin.get(); cin.get(); return 0; CS 1410 Comp Sci with C++ Input and Output 15 CS 1410 Comp Sci with C++ Input and Output 16 4
5 Output Display with fixed #include < iostream > /* This program computes the sum and product of two numbers. */ int main(void) double Number1, Number2, Sum, Product; cout << "Enter two numbers --> "; cin >> Number1 >> Number2; Sum = Number1 + Number2; Product = Number1 * Number2; cout<<fixed; fixed manipulator cout<< "The sum of " << Number1 << " and " << Number2 << " is "<< Sum << endl; cout<< "The product of " << Number1 << " and " << Number2 << " is "<< Product; cin.get(); cin.get(); return 0; Manipulators in iomanip setw(n) fieldwidth specification manipulator: set the width of the next value (integer, float, string) to be printed over n columns with right justification as default The header file #include <iomanip> must be inserted into the source code to use manipulator setw(n) setw(n) affects only the very next item displayed, and is useful to align columns of output CS 1410 Comp Sci with C++ Input and Output 17 CS 1410 Comp Sci with C++ Input and Output 18 Output Display with setw() #include <iomanip> #include <iostream> Position #include <string> int main ( ) int mynumber = 123 ; int yournumber = 5 ; cout<<setw(10)<< Mine <<setw(10)<< Yours <<endl <<setw(10)<<mynumber <<setw(10)<<yournumber <<endl; return 0 ; Mine Yours each is displayed rightjustified and each is located in a total of 10 positions Decimal Number Outputs Decimal number specification manipulator -- setprecision(n): set ALL subsequent floating-point outputs to be displayed with n decimal places of accuracy provided that fixed manipulator has been previously specified. Default is usually 6. CS 1410 Comp Sci with C++ Input and Output 19 CS 1410 Comp Sci with C++ Input and Output 20 5
6 iomanip Manipulators Scientific: set ALL subsequent floating point outputs to be displayed in scientific notation form. This is the default. left: set ALL subsequent outputs to leftjustified within the printing field. right: set ALL subsequent outputs to right-justified within the printing field. This is the default. Output Display with setprecision() #include <iomanip> #include <iostream> int main ( ) float mynumber = ; float yournumber = ; cout<<fixed<<showpoint <<setprecision(4); cout<< Numbers are: <<endl <<setw(10)<<mynumber<<endl <<setw(10)<<yournumber <<endl; return 0 ; Numbers are: each is displayed right-justified and rounded if necessary and each is located in a total of 10 positions with 4 places after the decimal point CS 1410 Comp Sci with C++ Input and Output 21 CS 1410 Comp Sci with C++ Input and Output 22 File Manipulators Examples Header File Manipulator Argument Type Effect <iostream> endl none terminates <iostream> showpoint none <iostream> fixed none <iomanip> setw(n) int <iomanip> setprecision(n) int displays decimal point suppresses scientific notation sets fieldwidth to n positions sets precision to n digits int INum1, INum2; double DNum; INum1=123; INum2=4567; DNum=12.34; Statement Output cout<<setw(4)<<inum1 <<setw(6)<<inum2; cout<<setw(20)<<"number 1 is" <<setw(4)<<inum1<<'\n' <<setw(10)<<"number 2 is" <<setw(2)<<inum2; cout<<setw(5)<<dnumber<<endl <<setw(7)<<dnumber<<endl <<setw(2)<<dnumber<<endl Number 1 is 123 Number 2 is CS 1410 Comp Sci with C++ Input and Output 23 CS 1410 Comp Sci with C++ Input and Output 24 6
7 Examples Examples double DNum1, DNum2; DNum1 = 1.23; DNum2 = ; Statement cout << fixed << DNum1 << '\n' << DNum2; cout << fixed << setprecision(2) << DNum1 << '\n' << DNum2; cout << scientific << DNum1 <<'\n' << DNum2; cout << fixed << setprecision(2) << setw(7) << DNum1 << '\n' << setw(7) << DNum2; Output e e int INum1, INum2; double DNum; INum1=123; INum2=4567; DNum=12.34; Statement Output cout<<setw(4)<<inum1 <<setw(6)<<left<<inum2; cout<<setw(20)<<left<<"number 1 is" <<setw(4)<<inum1<<'\n' <<setw(10)<<"number 2 is" <<setw(2)<<inum2; Number 1 is 123 Number 2 is4567 CS 1410 Comp Sci with C++ Input and Output 25 CS 1410 Comp Sci with C++ Input and Output 26 Giving a Value to a Variable In your program you can assign(give) a value to the variable by using the assignment operator = ageofdog = 12; or by another method, such as cout << How old is your dog? ; cin >> ageofdog; >> Operator >> is called the input or extraction operator >> is a binary operator >> is left associative Expression cin >> age Statement Has value cin cin >> age >> weight;
8 Extraction Operator(>>) Variable cin is predefined to denote an input stream from the standard input device(the keyboard) The extraction operator >> called get from takes 2 operands; the left operand is a stream expression, such as cin--the right operand is a variable of simple type Operator >> attempts to extract the next item from the input stream and to store its value in the right operand variable Input Input in computer programming means to copy literal values to corresponding variables either from the keyboard or from a file. Interactive input is the input obtaining from the keyboard (standard input). 29 CS 1410 Comp Sci with C++ Input and Output 30 Input Statement cin >> Variable >> Variable >> Variable >> ; >> : extraction operator or symbol cin >> directs literal values to variables cin >> Number1; cin >> Letter1 >> Letter2 >> Amount; Header File for input Object To use input object cin the header file iostream must be included in the source prorgam and directive using for referencing cout, i.e., #include <iostream> CS 1410 Comp Sci with C++ Input and Output 31 CS 1410 Comp Sci with C++ Input and Output 32 8
9 Extraction Operator (>>) Variable cin is predefined to denote an input stream from the standard input device(the keyboard) The extraction operator >> called get from takes 2 operands; the left operand is a stream expression, such as cin--the right operand is a variable of simple type Operator >> attempts to extract the next item from the input stream and to store its value in the right operand variable skips over (actually reads but does not store anywhere) leading white space characters as it reads your data from the input stream (either keyboard or disk file) Whitespace Characters blanks tabs end-of-line (newline) characters The newline character is created by pressing Enter or Return at the keyboard, or by using the manipulator endl or \n in a program Types of Inputs char a single character int a literal-integer value -- 24, -30 float a literal-integer value or a literal-floatpoint value -- 24, 24.0, -24, 2.4e01 Example int INum1, INum2; double DNum1, DNum2; char Letter1, Letter2; Statement Input at Keyboard Content cin>>inum1>>inum2 INum1=123 >>Letter1>>Letter2; AB INum2=4567 Letter1=A Letter2=B cin>>dnum1>>letter1 >>DNum2>>Letter2; 123.0A4567B DNum1=123.0 DNum2= Letter1=A Letter2=B CS 1410 Comp Sci with C++ Input and Output 35 CS 1410 Comp Sci with C++ Input and Output 36 9
10 Reading Inputs Statement Input Content AB\n INum1=123 cin>>inum AB\n INum2=4567 >>INum2 Letter1=A AB\n >>Letter1 Letter2=B >>Letter2; AB\n AB\n cin>>dnum1 >>Letter1 >>DNum2 >>Letter2; 123.0A4567B\n 123.0A4567B\n 123.0A4567B\n 123.0A4567B\n 123.0A4567B\n DNum1=123.0 DNum2= Letter1=A Letter2=B String Data Type String is a sequence of characters enclosed in double quotes A string containing no characters is a null string string is not primitive data type The header file <string> must be included in the source code Examples:, Joe Nobody, Hello C++" CS 1410 Comp Sci with C++ Input and Output 37 CS 1410 Comp Sci with C++ Input and Output 38 String Declaration string LastName; string FirstName; string Name; string Message = "Hello C++"; const string ConjugateAnd = "and"; LastName = "Nobody"; N o b o d y \n FirstName = "Joe"; J o e \n Name = First + " " + LastName; cout<<name; cin<<firstname; Example #include < iostream > #include < string > int main(void) const string Blank = " "; string LastName; string FirstName, Name; cout<<"enter your lastname --> "; cin>>lastname; cout<<"enter your firstname --> "; cin>>firstname; Name = FirstName + Blank + LastName + "."; cout<<endl<<"your name is "<<Name; cin.get(); cin.get(); return 0; CS 1410 Comp Sci with C++ Input and Output 39 CS 1410 Comp Sci with C++ Input and Output 40 10
11 get(), getline() -- Functions Reading Character Data with the get() function cin.get(somechar) cin.get does not skip blanks Reading entire line of input including blanks with the getline() function getline(cin, inputstring); Examples string FirsrtName, LastName, Name; char MiddleIntial; char Letter1, Letter2, Letter3; Statement Input at Keyboard Content cin>>firstname >>MiddleInitial >>LastName; getline(cin,name); cin>>letter1 >>Letter2; cin.get(letter3); Joe M Nobody\n Joe M Nobody\n Joe M Nobody\n Joe M Nobody\n Joe M Nobody\n Joe M Nobody\n A B C D\n A B C D\n A B C D\n A B C D\n FirstName=Joe MiddleInitial=M LastName=Nobody Name=Joe M Nobody Letter1=A Letter2=B Letter3= CS 1410 Comp Sci with C++ Input and Output 41 CS 1410 Comp Sci with C++ Input and Output 42 String Input Using getline string firstname; string lastname; getline(cin, firstname); getline(cin, lastname); Results Using getline string firstname; string lastname; getline(cin, firstname); getline(cin, lastname); Suppose input stream looks like this: Joe Hernandez 23 What are the string values? Joe Hernandez 23? firstname lastname
12 ignore( ) -- Function Skipping Characters with the ignore function cin.ignore(10, \n); cin.ignore(10, 'c'); The above statements skips the next 10 input characters or to skip characters until a newline character is read or a character 'c' is read. CS 1410 Comp Sci with C++ Input and Output 45 Examples Statement Input Content \n INum1= \n INum2=4567 INum3= \n cin>>inum1>>inum2; \n cin.ignore(50,\n); \n \n cin>>inum3; \n \n cin.ignore(4,\n); cin>>letter1 >>Letter2; cin.ignore(100,'d'); cin>>letter3, 12345ABCDEF\n 12345ABCDEF\n 12345ABCDEF\n 12345ABCDEF\n 12345ABCDEF\n 12345ABCDEF\n Letter1=5 Letter2=A Letter3=E CS 1410 Comp Sci with C++ Input and Output 46 Interactive I/O In an interactive program the user enters information while the program is executing Before the user enters data, a prompt should be provided to explain what type of information should be entered After the user enters data, the value of the data should be printed out for verification. This is called echo printing that way, the user will have the opportunity to check for erroneous data Prompting for Interactive I/O // prompt cout << Enter part number : << endl; cin >> partnumber ; // prompt cout << Enter quantity ordered : << endl; cin >> quantity; // prompt cout << Enter unit price : << endl; cin >> unitprice; // calculate totalprice = quantity * unitprice; // echo the input cout << Part # << partnumber << endl; cout << Quantity: << quantity << endl; cout << Unit Cost: $ << setprecision(2) << unitprice << endl; // output cout << Total Cost: $ << totalprice << endl;
13 Diskette Files for I/O #include <fstream> input data disk file A:\myInfile.dat your variable (of type fstream) executing program your variable (of type fstream) output data disk file A:\myOut.dat To Use Disk I/O, you must Access use #include <fstream> Choose valid identifiers for your filestreams and declare them Open the files and associate them with disk names Use your filestream identifiers in your I/O statements (using >> and <<, manipulators, get, ignore) Close the files Files and Streams Operating files in C++ requires the following steps: 1. Including the <fstream> header file 2. Declare a file variable of type fstream that will be associated with the actual or physical file 3. Opening the file connecting the file variable to the actual file 4. Operations using the file name (i.e., read - >> and write - <<) 5. Closing the file Example Writing to a file 1. #include <fstream> 2. fstream outfile; 3. outfile.open( a:\\prog1.txt",ios::out); 4. outfile<<data; 5. outfile.close(); CS 1410 Comp Sci with C++ Input and Output 51 CS 1410 Comp Sci with C++ Input and Output 52 13
14 Example ios -- Arguments Reading from a file 1. #include <fstream> 2. fstream infile; 3. infile.open( a:\\prog1.txt",ios::in); 4. infile<<data; 5. infile.close(); arguments: ios::in ios::out ios::app input(already exist) output(creates a new file) append(appends to the end of existing file) CS 1410 Comp Sci with C++ Input and Output 53 CS 1410 Comp Sci with C++ Input and Output 54 Example Using Disk I/O #include <fstream> int main() fstream myinfile, myoutfile; //declarations //open file to read myinfile.open( A:\\myIn.dat,ios::in); //open file to write myoutfile.open( A:\\myOut.dat,ios::out); // close files myinfile.close( ); myoutfile.close( ); return 0; What does opening a file do? associates the C++ identifier for your file with the physical (disk) name for the file if the input file does not exist on disk, open is not successful if the output file does not exist on disk, a new file with that name is created if the output file already exists, it is erased places a file reading marker at the very beginning of the file, pointing to the first character in it
15 Stream Fail State when a stream enters the fail state, further I/O operations using that stream have no effect at all. But the computer does not automatically halt the program or give any error message possible reasons for entering fail state include: invalid input data (often the wrong type) opening an input file that doesn t exist opening an output file on a diskette that is already full or is write-protected 57 15
C++ Input/Output Chapter 4 Topics
Chapter 4 Topics Chapter 4 Program Input and the Software Design Process Input Statements to Read Values into a Program using >>, and functions get, ignore, getline Prompting for Interactive Input/Output
More informationChapter 3 - Notes Input/Output
Chapter 3 - Notes Input/Output I. I/O Streams and Standard I/O Devices A. I/O Background 1. Stream of Bytes: A sequence of bytes from the source to the destination. 2. 2 Types of Streams: i. Input Stream:
More informationWeek 3: File I/O and Formatting 3.7 Formatting Output
Week 3: File I/O and Formatting 3.7 Formatting Output Formatting: the way a value is printed: Gaddis: 3.7, 3.8, 5.11 CS 1428 Fall 2014 Jill Seaman spacing decimal points, fractional values, number of digits
More informationI/O Streams and Standard I/O Devices (cont d.)
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 from the standard input device Learn how to use predefined
More informationThe C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.
The C++ Language Input and Output Output! Output is information generated by a program.! Frequently sent the screen or a file.! An output stream is used to send information. Another type supplied by C++
More informationC++ Input/Output: Streams
C++ Input/Output: Streams Basic I/O 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams:
More informationSoftware Design & Programming I
Software Design & Programming I Starting Out with C++ (From Control Structures through Objects) 7th Edition Written by: Tony Gaddis Pearson - Addison Wesley ISBN: 13-978-0-132-57625-3 Chapter 3 Introduction
More informationFile I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents
File I/O 1 File Names and Types A file name should reflect its contents Payroll.dat Students.txt Grades.txt A file s extension indicates the kind of data the file holds.dat,.txt general program input or
More informationCS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.
REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout
More informationTEST 1 CS 1410 Intro. To Computer Science Name KEY. October 13, 2010 Fall 2010
TEST 1 CS 1410 Intro. To Computer Science Name KEY October 13, 2010 Fall 2010 Part I: Circle one answer only. (2 points each) 1. The decimal representation of binary number 11011 2 is a) 19 10 b) 29 10
More informationChapter 3. Numeric Types, Expressions, and Output
Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a Value-Returning
More informationDefinition Matching (10 Points)
Name SOLUTION Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. There are no syntax errors
More informationThe cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;
The cin Object Short for console input. It is used to read data typed at the keyboard. Must include the iostream library. When this instruction is executed, it waits for the user to type, it reads the
More informationCPE Summer 2015 Exam I (150 pts) June 18, 2015
Name Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. You can assume that there is one
More information3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley
More informationCS2141 Software Development using C/C++ Stream I/O
CS2141 Software Development using C/C++ Stream I/O iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make
More informationPiyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class
C++ IO C++ IO All I/O is in essence, done one character at a time For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Concept: I/O operations act on streams
More informationCSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]
CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1] LEARNING OBJECTIVES Upon completion, you should be able to: o define C++ text files o explain the benefits of using I/O file processing o explain
More informationExpressions, Input, Output and Data Type Conversions
L E S S O N S E T 3 Expressions, Input, Output and Data Type Conversions PURPOSE 1. To learn input and formatted output statements 2. To learn data type conversions (coercion and casting) 3. To work with
More informationC++ does not, as a part of the language, define how data are sent out and read into the program
Input and Output C++ does not, as a part of the language, define how data are sent out and read into the program I/O implementation is hardware dependent The input and output (I/O) are handled by the standard
More informationObjects and streams and files CS427: Elements of Software Engineering
Objects and streams and files CS427: Elements of Software Engineering Lecture 6.2 (C++) 10am, 13 Feb 2012 CS427 Objects and streams and files 1/18 Today s topics 1 Recall...... Dynamic Memory Allocation...
More informationC++ Programming Lecture 10 File Processing
C++ Programming Lecture 10 File Processing By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Outline Introduction. The Data Hierarchy. Files and Streams. Creating a Sequential
More informationString Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting
String Variables and Output/Input Adding Strings and Literals to Your Programming Skills and output/input formatting A group of characters put together to create text is called a string. Strings are one
More informationCopyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Slide 7-1 Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Type Conversation / Casting Name Constant - const, #define X When You Mix Apples and Oranges: Type Conversion Operations
More information! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char
Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated
More informationLecture 3 The character, string data Types Files
Lecture 3 The character, string data Types Files The smallest integral data type Used for single characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0',
More informationc++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.
Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object
More informationC++ Basics. Lecture 2 COP 3014 Spring January 8, 2018
C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing
More informationWe will exclusively use streams for input and output of data. Intro Programming in C++
C++ Input/Output: Streams The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: 1 istream
More informationStrings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationIntroduction to C++ (Extensions to C)
Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.
More informationChapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Slide 6-3 6.1 Streams and Basic File I/O I/O Streams I/O
More informationInput and Output. Data Processing Course, I. Hrivnacova, IPN Orsay
Input and Output Data Processing Course, I. Hrivnacova, IPN Orsay Output to the Screen Input from the Keyboard IO Headers Output to a File Input from a File Formatting I. Hrivnacova @ Data Processing Course
More informationChapter 3: Input/Output
Chapter 3: Input/Output I/O: sequence of bytes (stream of bytes) from source to destination Bytes are usually characters, unless program requires other types of information Stream: sequence of characters
More informationInput and Output File (Files and Stream )
Input and Output File (Files and Stream ) BITE 1513 Computer Game Programming Week 14 Scope Describe the fundamentals of input & output files. Use data files for input & output purposes. Files Normally,
More informationChapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects
Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Copyright 2008 Pearson Addison-Wesley. All rights reserved.
More informationStreams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing
Input Data 1 Streams 2 Streams Conceptual Model of a Stream Associating a File Stream with a File Basic Stream Input Basic Stream Output Reading Single Characters: get() Skipping and Discarding Characters:
More informationStreams in C++ Stream concept. Reference information. Stream type declarations
Stream concept A stream represent a sequence of bytes arriving, being retrieved, being stored, or being sent, in order. A stream is continuos and offer sequential access to the data. Each byte can be read
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationLECTURE 02 INTRODUCTION TO C++
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION
More informationCS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12
CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing
More information3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object The cin Object in Program 3-1 Standard input object Like cout, requires iostream file Used to read input from keyboard Information
More informationChapter 3: Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information retrieved from cin with >>
More informationCPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)
Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow
More informationFundamentals of Programming CS-110. Lecture 2
Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout
More informationThe C++ Language. Arizona State University 1
The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationChapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information retrieved from cin with >>
More informationReview for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure
Computer Systems Computer System Computer Structure C++ Environment Imperative vs. object-oriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Winter 2011 Lecture 05 - I/O using the standard library & Introduction to Classes Milena Scaccia School of Computer Science McGill University February 1, 2011 Final note on
More informationIntroduction to Programming EC-105. Lecture 2
Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically
More informationStreams. Rupesh Nasre.
Streams Rupesh Nasre. OOAIA January 2018 I/O Input stream istream cin Defaults to keyboard / stdin Output stream ostream cout std::string name; std::cout > name; std::cout
More information! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.
Week 1: Introduction to C++ Gaddis: Chapter 2 (excluding 2.1, 2.11, 2.14) CS 1428 Fall 2014 Jill Seaman Literals A literal represents a constant value used in a program statement. Numbers: 0, 34, 3.14159,
More informationEngineering Problem Solving with C++, 3e Chapter 2 Test Bank
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. integer B 1.427E3 B. double D "Oct" C. character B -63.29 D. string F #Hashtag
More informationName Section: M/W T/TH Number Definition Matching (8 Points)
Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
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. B. Outputs to the console a floating point number f1 in scientific format
More informationBITG 1233: Introduction to C++
BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token
More informationBITG 1113: Files and Stream LECTURE 10
BITG 1113: Files and Stream LECTURE 10 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of input & output files. 2. Use data files for input & output
More informationName Section: M/W T/TH Number Definition Matching (6 Points)
Name Section: M/W T/TH Number Definition Matching (6 Points) 1. (6 pts) Match the words with their definitions. Choose the best definition for each word. Event Counter Iteration Counter Loop Flow of Control
More informationIntro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?
Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we
More informationGetting started with C++ (Part 2)
Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output
More informationCHAPTER 3 Expressions, Functions, Output
CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationUEE1303(1070) S 12 Object-Oriented Programming in C++
Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1303(1070) S 12 Object-Oriented Programming in C++ Lecture 06: Streams and File Input/Output I/O stream istream and ostream member
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationModule C++ I/O System Basics
1 Module - 36 C++ I/O System Basics Table of Contents 1. Introduction 2. Stream classes of C++ 3. Predefined Standard Input/Output Streams 4. Functions of class 5. Functions of class
More informationConvenient way to deal large quantities of data. Store data permanently (until file is deleted).
FILE HANDLING Why to use Files: Convenient way to deal large quantities of data. Store data permanently (until file is deleted). Avoid typing data into program multiple times. Share data between programs.
More information1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.
Engineering Problem Solving With C++ 4th Edition Etter TEST BANK Full clear download (no error formating) at: https://testbankreal.com/download/engineering-problem-solving-with-c-4thedition-etter-test-bank/
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationC++ Quick Guide. Advertisements
C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and
More informationChapter 2: Introduction to C++
Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++
More informationInput/Output Streams: Customizing
DM560 Introduction to Programming in C++ Input/Output Streams: Customizing Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides by Bjarne Stroustrup]
More informationOutline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.
Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations
More information3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.
Chapter 2: Problem Solving Using C++ TRUE/FALSE 1. Modular programs are easier to develop, correct, and modify than programs constructed in some other manner. ANS: T PTS: 1 REF: 45 2. One important requirement
More informationChapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley
More informationCS201 Solved MCQs.
15.1 Answer each of the following: a. Input/output in C++ occurs as of bytes. b. The stream manipulators that format justification are, and. c. Member function can be used to set and reset format state.
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationPhysics 6720 I/O Methods October 30, C++ and Unix I/O Streams
Physics 6720 I/O Methods October 30, 2002 We have been using cin and cout to handle input from the keyboard and output to the screen. In these notes we discuss further useful capabilities of these standard
More informationWindow s Visual Studio Output
1. Explain the output of the program below. Program char str1[11]; char str2[11]; cout > str1; Window s Visual Studio Output Enter a string: 123456789012345678901234567890 Enter
More informationCSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 4-5 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 3 Assignment, Formatting, and Interactive Input
More information3.1. Chapter 3: The cin Object. Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 The cin Object Standard input stream object, normally the keyboard,
More informationObject-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering
Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)
More informationWe can even use the operator << to chain the output request as:
IO Management Writing to the stream: cout
More informationBasics of C++ C++ Overview Elements I / O Assignment and Comments Formatted Output. Dr.Hamed Bdour
Basics of C++ C++ Overview Elements I / O Assignment and Comments Formatted Output 1 History of C and C++ BCPL 1967 By Martin Richards Lang. to write OS NO Data Type + B :By Ken Thompson Used to create
More informationby Pearson Education, Inc. All Rights Reserved. 2
The C++ standard libraries provide an extensive set of input/output capabilities. C++ uses type-safe I/O. Each I/O operation is executed in a manner sensitive to the data type. If an I/O member function
More informationC++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh
C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationStarting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key
Starting Out with C++ Early Objects 9th Edition Gaddis TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/starting-c-early-objects-9thedition-gaddis-test-bank/ Starting
More informationWeek 5: Files and Streams
CS319: Scientific Computing (with C++) Week 5: and Streams 9am, Tuesday, 12 February 2019 1 Labs and stuff 2 ifstream and ofstream close a file open a file Reading from the file 3 Portable Bitmap Format
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationInteger Data Types. Data Type. Data Types. int, short int, long int
Data Types Variables are classified according to their data type. The data type determines the kind of information that may be stored in the variable. A data type is a set of values. Generally two main
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationFall 2017 CISC/CMPE320 9/27/2017
Notices: CISC/CMPE320 Today File I/O Text, Random and Binary. Assignment 1 due next Friday at 7pm. The rest of the assignments will also be moved ahead a week. Teamwork: Let me know who the team leader
More informationToday in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1
Today in CS162 External Files What is an external file? How do we save data in a file? CS162 External Data Files 1 External Files So far, all of our programs have used main memory to temporarily store
More informationSetting Justification
Setting Justification Formatted I/O 1 Justification - Justification refers to the alignment of data within a horizontal field. - The default justification in output fields is to the right, with padding
More informationSystem Design and Programming II
System Design and Programming II CSCI 194 Section 01 CRN: 10968 Fall 2017 David L. Sylvester, Sr., Assistant Professor Chapter 12 Advanced File Operation File Operations A file is a collection of data
More informationChapter 14 Sequential Access Files
Chapter 14 Sequential Access Files Objectives Create file objects Open a sequential access file Determine whether a sequential access file was opened successfully Write data to a sequential access file
More information