Chapter 3 - Notes Input/Output

Size: px
Start display at page:

Download "Chapter 3 - Notes Input/Output"

Transcription

1 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 Types of Streams: i. Input Stream: A sequence of characters from an input device to the computer. ( Usually the keyboard ) ii. Output Stream: A sequence of characters from the computer to an output device. ( Usually the monitor ) 3. Every C++ Program must use the header file iostream and it contains two data types: i. istream ( input stream ) ii. ostream ( output stream ) 4. The header file also contains two variable declarations: i. cin ( common input ) a. Variables of the type istream are called input stream variables ii. cout ( common output ) a. Variables of the type ostream are called output stream variables iii. A stream variable is either an input or output stream variable. B. cin and the Extraction Operator Page 1

2 1. cin is a variable and >> is an extraction operator 2. >> is a binary operator and therefore requires two operands i. cin is the left hand operand and works as an input stream variable ii. The right hand side of the extraction operator must be a variable because the purpose of an input statement is to read and store values into a memory location and only variables refer to a memory location. 3. A single cin statement can read in more than one variable: Example: cin >> num1 >> num2 ; i. Every occurrence of >> extracts the next data item from the input stream. ii. The following statements are the same: cin >> num1 >> num2 ; or cin >> num1 ; cin >> num2 ; 4. The extraction operator >> skips all whitespace characters when scanning for input. i. Whitespace characters: Blanks, tabs, newline characters, and certain nonprintable characters. ii. Therefore, the following statement cin >> num1 >> num2 ; would properly read any of the three example inputs from the screen ( Assuming num1 and num2 were declared as integers ): and 67 separated by a single blank space and 67 separated by a several blank spaces and 67 separated by a tab space and 67 separated by a carriage return/line feed Page 2

3 Now num1 = 12 and num2 = How does the extraction operator determine what data type is being entered? cin >> X ; i. Suppose the user types in 2. Does the user want the number 2 or the character two. ii. The variable on the right hand side of the operator determines whether the 2 is retrieved as a number or a character. a. If the variable was declared as a data type int, it will be stored as a number. b. If the variable was declared as a char data type, it will be stored as a character. ii. What if the user types in 25? a. If the variable was declared as an integer data type, it will save the number 25. b. If the variable was declared as a float or double, it will save the number c. If the variable was declared as a character data type, it will save only the digit 2. Valid Input for a Variable of a Simple Data Type Data Type char int float or double Valid Input One printable character except the blank space An integer, possibly preceded by a ( + or - ) sign. A decimal number, possibly preceded by a ( + or - ) sign. If the actual data input is an integer, the input is converted to a decimal number with the zero decimal part. C. What if the User doesn't type the input properly? Page 3

4 1. What if the user types in more data than the program has variables? i. Once the variables in the cin statement are filled, it ignores and discards any other data the user may have typed in. 2. What if the user enters an integer for a float or double variable? i. It is not necessary for the user to type in a decimal for a float or double variable. The program will convert the integer to a decimal number before storing it in the variable. 3. What happens if the user enters a character for an integer variable or a decimal number for an integer variable? i. Entering a character into a variable declared as an integer during a cin statement causes an error called an input error. 4. What happens when the user enters a non-numeric character into an integer variable? i. The program enters a fail state when a non-numeric character is entered for an integer. The input operator immediately ceases and the program skips to the next line of code. It may appear on your screen as if nothing went wrong, but if you look at your output, you will find errors. II. Using Predefined Functions in a Program A. Predefined Functions: A set of instructions that performs a defined task, and is pre-written and available in a library that must be included with the source code. 1. A function, other than the main function, is normally called from the main function. This is known as a function call. Example: #include <cmath> Page 4

5 float base, exponent ; cout << "Please enter a number for the base of the power function: " ; cin >> base; cout << "Please enter a number for the exponent of the power function: " ; cin >> exponent; cout << endl; cout << base << " taken to the power of " << exponent << " = " << pow(base, exponent) << endl; 2. In the function call pow(base, exponent), pow is the function name and refers to the set of instructions that execute the power function. 3. The variables or values inside of the parenthesis are called arguments or parameters. This particular function requires two arguments or parameters. pow( 2, 3 ) Function call Arguments or parameters Function name 4. To use a predefined function in a program, you must know the following: i. The name of the header file to be included in your program. ii. iii. iv. The name of the function. This also means the exact spelling, etc. The number of parameters (arguments) the function takes. The data type of each parameter. v. What does the function do? What is its purpose? Will it return a value? 5. I/O provides several useful functions that can be called with the Page 5

6 iostream header file and using namespace std. These are functions that the programmer will use frequently but would be difficult to write for themselves. B. cin and the get Function 1. Remember that the use of cin and the extraction operator ( >> ) does not allow the programmer to store any whitespace ( blanks, tabs, or newline characters) into a character variable. But what if you need or want to store these characters? Example: Suppose the following code is written, char ch1, ch2 ; int num ; cin >> ch1 >> ch2 >> num ; During execution of the code, the user enters the following information: A 25 This is what happens: A gets stored in the memory space called ch1. 2 gets stored in the memory space called ch2. 5 gets stored in the memory space called num. This is not what you, the programmer, intended. You wanted ch1 to store A, ch2 to store the blank space, and num to store the integer cin.get( charvariable ) i. This is a function provided by the iostream header files. It allows the programmer to grab one character at a time from the input device (keyboard - shown on monitor) even if it is a whitespace character (blank, tab, newline character). ii. Notice that this function statement requires one argument or parameter. That parameter/argument is a character variable to store a character data type that will be extracted from the input. Page 6

7 iii. Notice the period between cin and get. This is called a dot rather than a period when used outside string notation. When used this way ( cin.get( chvar )), it is known as dot notation and becomes an operator called the member access operator. Suppose we now have the following code, char ch1, ch2 ; int num ; cin.get ( ch1 ) ; cin.get ( ch2 ) ; cin >> num ; During execution of the code, the user enters the same information: A 25 This is what happens: A gets stored in the memory space called ch1. a blank gets stored in the memory space called ch2. 25 gets stored in the memory space called num. iii. You cannot use the cin.get ( ) function statement with a data type argument of integer or float because it will give you a compiler error. C. cin and the ignore Function 1. cin.ignore ( intexpression, charexpression ); i. Allows the program to ignore or skip a designated number of characters. ii. iii. intexpression represents a variable or integer number of the amount of characters you would like to skip or ignore. charexpession represents a variable of type character or character that you would like to skip up to. Page 7

8 iv. In other words, if the statement cin.ignore( 5, 'A' ); was in your code, it would skip the next five characters in the input line unless one of those characters was a letter A. If no letter (or character) A was found in the next five characters, it would take the sixth character regardless of what it is ( assuming you have code that wants to take the sixth character ). v. Remember that a '\n' is a character. You could skip all the characters of a line until you got to the new line character. vi. An example of how it works: Suppose you have the following line of code: cin >> letr1 >> letr2 ; cin.ignore ( 100, '\n' ); cin >> letr3 ; And the user puts the following input on the screen: ghwyrkhlhj385609gjr98 7lkvcywhgn The result will be the following: 2 is stored in variable letr1 4 is stored in variable letr2 7 is stored in letr3 then the cursor waits at the character following the 7. D. The putback and peek Functions 1. putback Function: Allows the programmer to put back into the input stream the last character extracted from it by the get function. i. The syntax for this function: istreamvar.putback( ch ) ii. iii. istreamvar would be the cin variable, since we haven't covered any others yet. ch is the variable you stored the character from the get function. Page 8

9 iv. Here's an example of how to use it: Suppose you have the following line of code: cin >> letr1 >> letr2 ; cin.ignore ( 100, '\n' ); cin.putback ( letr2 ); cin >> letr3 ; And the user puts the following input on the screen: ghwyrkhlhj385609gjr98 7lkvcywhgn The result will be the following: 2 is stored in variable letr1 4 is stored in variable letr2 4 is put back on the input stream 4 is stored in variable letr3 then the cursor waits at the character peek Function: Allows the programmer to see or copy what the next character in the input stream is without taking it off of the input stream. i. The syntax for this function: ch = istreamvar.peek( ) ii. iii. iv. istreamvar would be the cin variable, since we haven't covered any others yet. Notice there is no required argument or parameter inside the parenthesis. You must include the parenthesis, however, you just don't have to put anything between them. The function cin.peek( ) returns a character. Because of this, you must store the character in a variable or print it to the screen like the following examples: cout << "The next character in the istream is: " << cin.peek( ) ; letr1 = cin.peek( ); Page 9

10 v. Now let's see how it works: Suppose you have the following line of code: cin >> letr1 >> letr2 ; cin.ignore ( 100, '\n' ); letr3 = cin.peek ( ); cin >> letr4 ; And the user puts the following input on the screen: ghwyrkhlhj385609gjr98 7lkvcywhgn The result will be the following: 2 is stored in variable letr1 4 is stored in variable letr2 7 is stored in variable letr3 7 is stored in variable letr4 then the cursor waits at the character after 7. III. Input Failure A. Input Failure: An attempt to read invalid data. For example, trying to read a character value into an integer variable. 1. Fail State: When input failure occurs, the input stream enters the fail state in which all further I/O statements using that stream are ignored. WARNING: The program then continues to execute with whatever values are in the variables and will produce incorrect results!! B. The clear Function: Restores the Input Stream to a working state. 1. Syntax and Use: istreamvar.clear( ) Page 10

11 Example of Use: int a = 23, b = 34 ; cout << "Enter a number followed by a character: " ; cin >> a >> b; cout << endl << "a = " << a << ", b = " << b << endl ; cin.clear ( ) ; // Restores istream in case of input failure cin.ignore ( 200, '\n' ) ; // Essentially skips to the next line cout << "Enter two numbers: " ; cin >> a >> b; cout << endl << "a = " << a << ", b = " << b << endl ; IV. Output and Formatting Output A. Manipulators used in the output stream require a preprocessor directive with the following header: B. setprecision: Controls the number of floating numbers output. Default output in some SDK's is 6 or scientific notation or E1 1. Syntax and Use: cout << setprecision( n ); 2. Example: Page 11

12 float num1, num2 ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << setprecision(5) << num1 << " "; cout << setprecision(2) << num2 << endl << endl; Please enter two float numbers: e e Notice that the output is not in decimal format and that the amount of numbers displayed (not counting the e+006) is the same amount as the number in setprecision( n ). Also notice that the numbers were rounded off before being displayed. C. fixed: Forces the output of floating point numbers to be displayed as decimal point numbers. If an integer number is input as a float, it will add a decimal point and zeros afterward. ( NOTE: The manipulator showpoint does this also with integer input for float.) 1. Syntax and Use: cout << fixed; 2. Example of Use: float num1, num2 ; cout << fixed ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; Page 12

13 cout << endl; cout << num1 << " " << num2 << endl << endl; Please enter two float numbers: Notice that the integer number 12 has a decimal point and zeros in the output. Also notice that the first and second number have a precision of 8 numbers. This is evidently the default precision for this compiler. 4. What if we want to control the amount of numbers after the decimal point like in monetary output. This requires the use of setprecision( ) with the fixed manipulator. Example: float num1, num2 ; cout << fixed << setprecision(2) ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << num1 << " " << num2 << endl << endl; Page 13

14 Please enter two float numbers: Notice that the setprecision( ) manipulator combined with the fixed manipulator controls only the amount of digits after the decimal point. 6. To undo the fixed manipulator, issue the following command: cout.unsetf ( ios :: fixed ) ; D. showpoint: Forces a decimal point and subsequent zeros on the output of an integer number input as a float. For the DevC++ compiler, both fixed and showpoint do this. You can use showpoint without the use of fixed and it will force a decimal ouput. If you use fixed, you do not need to use showpoint. 1. Syntax and Use: cout << showpoint ; Example of Use: float num1, num2 ; cout << showpoint ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << num1 << " " << num2 << endl << endl; Page 14

15 Please enter two float numbers: What about using showpoint with the setprecision( ) manipulator? Example: float num1, num2 ; cout << showpoint << setprecision(2) ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << num1 << " " << num2 << endl << endl; Please enter two float numbers: Another Example with setprecision(4) : Page 15

16 float num1, num2 ; cout << showpoint << setprecision(4) ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << num1 << " " << num2 << endl << endl; Please enter two float numbers: Notice that showpoint with setprecision( ) controls all of numbers in the float, not just the digits after the decimal point. E. setw: Outputs the value of an expression in specified columns. The value of the expression can be a number, a character, or a string. How it works is that the statement setw(n) outputs the next expression in n columns and right justifies that expression in the n columns. If the number of columns specified is less than the number of columns required by the output, then the output is automagically expanded so as not to truncate the expression. NOTE: The setw( ) manipulator only works on the next expression output. It is not like the setprecision( ) or fixed manipulators that can be called once and all subsequent outputs will conform. 1. Syntax and Use: cout << setw( n ) << variable or expression ; Example of Use: Page 16

17 float num1, num2 ; cout << showpoint << setprecision(4) ; cout << "Please enter two float numbers: " ; cin >> num1 >> num2; cout << endl; cout << " " << endl; cout << setw(10) << num1 << setw(10) << num2; cout << endl << endl; Please enter two float numbers: Note that the setw( ) manipulator can be used without other manipulators or in conjunction with them. To use setw( ), you must also include the preprocessor directive: Another Example without Numbers: cout << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; Page 17

18 Alan Jay Spurgeon 1313 Mockingbird Lane Eerie IN Another example with the setw( ) not wide enough for output. Notice that the setw(5) is smaller than the number of columns needed to output the string to the screen. Also notice that it does not chop off the string to the first 5 characters or the last 5 characters, but prints out the whole thing. Example: cout << setw(5) << "Alan Jay Spurgeon" << endl ; cout << setw(5) << "1313 Mockingbird Lane" << endl ; cout << setw(5) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; Alan Jay Spurgeon 1313 Mockingbird Lane Eerie IN V. Additional Output Formatting Tools A. fill and setfill: A function and manipulator that allows the programmer to fill the blank spaces caused by a setw( ) manipulator with a specified character. Page 18

19 1. Syntax and Use for fill: ostreamvar.fill ( ch ) ; i. Where ostreamvar would be cout if we are outputting to the screen. ii. iii. iv. Where ch would be some character data type variable or a character between single quotes. Like the manipulators fixed and setprecision( ), the fill function only has to be called once to affect the output afterwards until it is changed again by code. Example: cout.fill('*') ; cout << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; *****Alan Jay Spurgeon *****1313 Mockingbird Lane *****Eerie**IN**01313 v. Another Example Page 19

20 cout.fill('*') ; cout << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout.fill('#'); cout << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; *****Alan Jay Spurgeon *****1313 Mockingbird Lane #####Eerie##IN## Syntax and Use of setfill: ostreamvar << setfill( ch ) ; i. Where ostreamvar would be cout if we are outputting to the screen. ii. iii. iv. Where ch would be some character data type variable or a character between single quotes. Like the manipulators fixed and setprecision( ), the setfill manipulator only has to be called once to affect the output afterwards until it is changed again by code. Example: cout << setfill('*') << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; Page 20

21 cout << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; *****Alan Jay Spurgeon *****1313 Mockingbird Lane *****Eerie**IN**01313 v. Another Example: cout << setfill('*') << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout << setfill('^') << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; *****Alan Jay Spurgeon *****1313 Mockingbird Lane ^^^^^Eerie^^IN^^ fill and setfill can be used interchangeably. Page 21

22 B. The left and right Manipulators: Allows the programmer to left or right justify when using the setw( ) manipulator. Recall that the default justification is right. 1. Syntax and Use: ostreamvar << left; i. Where ostreamvar would be cout since we have not changed the default output device. ii. The fill manipulator works like the fixed and setprecision( ) manipulators, once called, it is in effect until changed. iii. Example: cout << left << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; Alan Jay Spurgeon 1313 Mockingbird Lane Eerie IN iv. Another Example: Page 22

23 cout << left << setw(22) << "Alan Jay Spurgeon" << endl ; cout << setw(26) << "1313 Mockingbird Lane" << endl ; cout << right << setw(10) << "Eerie" << setw(4) << "IN" ; cout << setw(7) << "01313" << endl << endl; Alan Jay Spurgeon 1313 Mockingbird Lane Eerie IN v. Another way to undo the call for left justification is the following: cout.unsetf ( ios :: left ) ; vi. Again, another way to do the same thing as cout << left << right. cout.setf ( ios :: left, ios :: adjustfield ) ; or cout.setf ( ios :: right, ios :: adjustfield ) ; or cout << setiosflags ( ios :: left ) ; C. The flush Function: Allows the programmer to force the output buffer to print to the screen even if it is not full. The endl function does the same thing except that it also forces a newline. When you do not want a new line, use the flush function. 1. Syntax and Use: ostreamvar.flush ( ) ; or ostreamvar << "some expression" << flush; i. Where ostreamvar is cout if the default output device has not been changed. Page 23

24 ii. Example: int num1, num2 ; cout << "Enter an integer: " ; cout.flush( ) ; cin >> num1 ; cout << "Enter another integer: " << flush ; cin >> num2 ; cout << num1 << setw( 8 ) << num2 << endl; cout << endl ; 2. Reminder: Enter an integer: 34 Enter another integer: i. To use the stream functions: get, ignore, fill, clear, and setf, you must include the header file iostream. ii. To use a stream manipulator with parameters such as: setprecision, setw, setfill, and setiosflags, you must include the header file iomanip. D. Input/Output and the string Type 1. You can save a string to a string variable type from the input device using Page 24

25 the cin >> ( inputstreamvariable and extraction operator ). Remember, though, that cin >> ignores white space!! 2. Example of use: #include <string> string firstname, lastname, name ; cout << "Enter your first name here: " ; cin >> firstname ; cout << "Enter your last name here: " ; cin >> lastname ; cout << "Your name is " << firstname << " " << lastname ; cout << endl << endl ; Enter your first name here: Alan Enter your last name here: Spurgeon Your name is Alan Spurgeon 3. You can not do this: Same code... Produces the following output with different input: Enter your first name here: Alan Spurgeon Enter your last name here: Your name is Alan Spurgeon Page 25

26 4. To read strings containing blanks you must use the getline function. This function will read all characters up to, but not including, a new line character. i. Syntax and Use: getline ( istreamvar, strvar ) ; a. Where istreamvar is cin unless the default input device has been changed. b. Where strvar is a variable that is declared of type string. Don't forget to include the header file string. c. Now let's look at the previous program using the getline function: #include <string> string name ; cout << "Enter your first and last name here: " ; getline( cin, name ); cout << "Your name is " << name ; cout << endl << endl ; Enter your first and last name here: Alan Spurgeon Your name is Alan Spurgeon ii. Remember that the getline function does not ignore blank spaces, so be careful you do not have blank spaces where you don't want them ( at the beginning or end especially ). Page 26

27 5. You may also output strings to the standard or default output device by using the cout << ( outputstream variable and insertion operator ) For an example, look at the previous code and output example. VI. File Input/Output A. Purpose: Not all data has to originate from the keyboard or be output to the screen. In cases where a large amount of data is to be input or output, it is more efficient to read in from or out to a file. 1. File: An area in secondary storage used to hold data. B. Procedure: Reading data in from a file or writing data out to a file is a five step process: 1. Include the header file fstream in the program 2. Declare file stream variables 3. Associate the file stream variables with the input/output sources 4. Use the file stream variables with >>, << or other input/output functions 5. Close the files C. More Detail: 1. Step 1: Make sure you put the statement #include <fstream> with the rest of the header files you are using. 2. Step 2 : Declare your file stream variables such as: ifstream infile ; input file stream variable like a data type for input stream variables ofstream outfile ; output file stream variable like a data type for output stream var.s Page 27

28 3. Step 3 : Open the File(s) by associating the file stream variables with the input/output sources. i. Syntax for opening a file: filestreamvariable.open ( sourcename, fileopeningmode ); a. Where filestreamvariable would be the identifier name you created, like infile or outfile in the example in Step 2. b. Where sourcename is the file path and file name of where you want data written from or to. Example: infile.open( "C : myfile.dat " ) ; Notice that the file path and name are enclosed in double quotes. Also, if you want to open a file that is in the same directory as your source code, you can eliminate the file path and just put the filename inside double quotes. Example: outfile.open ( "myfile.dat " ) ; c. Where fileopeningmode is a mode in which the file is to be opened. This is an optional part of the syntax that does not have to be included if you want the default mode. File-Opening Mode ios :: in ios :: out ios :: nocreate ios :: app ios :: ate Description Opens the file for input. This is the default for ifstream variables. Opens the file for output. This is the default for ofstream variables. If the file does not exist, the open operation fails. If the file exists, adds the output at the end of the file ( append ). If the file does not exist, creates an empty file. Opens the file for output and moves to the end of the file. Output may be written anywhere in the file. ios :: trunc If the file exists, the contents will be deleted ( truncated ). ios :: noreplace If the file exists, the open operation fails. Page 28

29 d. Examples of use : infile.open ( "mydata.dat", ios :: nocreate ) ; outfile.open ( "yourdata.dat", ios :: app ) ; infile.open ("A:School\myData.dat", ios :: nocreate); outfile.open ( "yourdata.dat", ios :: trunc ) ; 4. Step 4 : Use your file stream variables with <<, >>, or any other input/output functions. In other words, where ever you would normally use the cin or cout variables, you instead use the file stream variables you made up. i. Examples: Instead of : cout << "Hello World!" << endl; Write this : outfile << "Hello World!" << endl; Instead of : cin >> num1 >> num2 ; Write this : infile >> num1 >> num2; Instead of : cout << fixed << setprecision (2 ); Write this : outfile << fixed << setprecision (2 ); Instead of : getline ( cin, stringvar ) ; Write this : getline ( infile, stringvar ) ; Get the idea? 5. Step 5 : Close the file(s). i. Syntax and Use: filestreamvariable.close ( ); a. Where filestreamvariable is the variable name you created, such as infile, outfile, indata, outdata, etc. b. Example : Page 29

30 infile.close ( ) ; or outfile.close ( ) ; D. Example Program Using Input and Output file: #include <fstream> ifstream infile; ofstream outfile; int num1, num2 ; float avg ; infile.open( "mydata.dat", ios :: nocreate ); outfile.open ( "yourdata.dat", ios :: app ); infile >> num1 >> num2; avg = (num1 + num2) / 2.0 ; outfile << fixed << setprecision(2); outfile << "The average of the two numbers is: " << avg ; outfile << endl << endl ; infile.close( ); outfile.close ( ); V. Binary Conversion A. Binary to Decimal 1. Binary Notation (Method of representing numbers using only 1's and 0's) 2. Given the number 19 in the decimal system: Page 30

31 = ( 1 x 10 ) + ( 9 x 10 ) 3. Now we demonstrate the same number in the Binary System i. 19 (decimal) = (binary) ii = ( 1 x 2 ) + ( 0 x 2 ) + ( 0 x 2 ) + ( 1 x 2 ) + ( 1 x 2 ) 4. Now we demonstrate how to convert from binary to decimal. Given the binary number B. Decimal to Binary 0 = 1 * 2 = 1 1 = 1 * 2 = 2 2 = 0 * 2 = 0 3 = 1 * 2 = Given the decimal number / 2 = 33 with remainder of 1 33 / 2 = 16 with remainder of 1 16 / 2 = 8 with remainder of 0 8 / 2 = 4 with remainder of 0 4 / 2 = 2 with remainder of 0 2 / 2 = 1 with remainder of 0 1 Stop when you get a one ( 1 ) here. Now take the remainders in reverse order, starting with the bottom one. Therefore the answer is : = 67 Page 31

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

I/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 information

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input 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

More information

Lecture 3 The character, string data Types Files

Lecture 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 information

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

The 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 information

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

Week 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 information

C++ Input/Output: Streams

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

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

File 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 information

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Piyush 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 information

C++ Input/Output Chapter 4 Topics

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 information

Chapter 3: Input/Output

Chapter 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 information

Software Design & Programming I

Software 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 information

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

CS2141 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 information

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

CS 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 information

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

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

C++ Programming Lecture 10 File Processing

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

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() { Text File I/O We can use essentially the same techniques we ve been using to input from the keyboard and output to the screen and just apply them to files instead. If you want to prepare input data ahead,

More information

Definition Matching (10 Points)

Definition 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 information

We will exclusively use streams for input and output of data. Intro Programming in C++

We 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 information

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

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

Objects and streams and files CS427: Elements of Software Engineering

Objects 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 information

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

CSC 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 information

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CPE 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 information

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Streams. 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 information

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

3.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 information

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Input 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 information

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

The 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 information

Introduction to C++ (Extensions to C)

Introduction 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 information

Objectives. In this chapter, you will:

Objectives. 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 information

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

! 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 information

Engineering Problem Solving with C++, Etter/Ingber

Engineering 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 information

Objectives. 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++ 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 information

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Strings 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 information

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

Chapter 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 information

Chapter 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. 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 information

Streams - Object input and output in C++

Streams - Object input and output in C++ Streams - Object input and output in C++ Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input

More information

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

Chapter 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 information

Chapter 2: Basic Elements of C++

Chapter 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 information

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Malak Abdullah The Evolution of Programming Languages (cont'd.)

More information

Input/Output Streams: Customizing

Input/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 information

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

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 12 Streams and File I/O Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag

More information

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved Topic 2 1. Reading and writing text files 2. Reading text input 3. Writing text output 4. Parsing and formatting strings 5. Command line arguments 6. Random access and binary files Reading Words and Characters

More information

Streams. Rupesh Nasre.

Streams. 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

File I/O Christian Schumacher, Info1 D-MAVT 2013

File I/O Christian Schumacher, Info1 D-MAVT 2013 File I/O Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Input and Output in C++ Stream objects Formatted output Writing and reading files References General Remarks I/O operations are essential

More information

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Review 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 information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Expressions, Input, Output and Data Type Conversions

Expressions, 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 information

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

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

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

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 12 Streams and File I/O Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output,

More information

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

Engineering 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 information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 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 information

Basics 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. 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 information

Chapter 3. Numeric Types, Expressions, and Output

Chapter 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 information

Chapter 12. Streams and File I/O

Chapter 12. Streams and File I/O Chapter 12 Streams and File I/O Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Introduction Streams Special objects Deliver

More information

String 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 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 information

Week 5: Files and Streams

Week 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 information

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Copyright 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

Input and Output File (Files and Stream )

Input 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 information

COMP322 - Introduction to C++

COMP322 - 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 information

UNIT- 3 Introduction to C++

UNIT- 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 information

Getting started with C++ (Part 2)

Getting 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 information

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 14 Input File Streams A program that reads 10 integers from a file int main() { string filename; // the name of the file to be opened ifstream fin; // the input file stream int array[10]; // an array

More information

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.

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. 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 information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

BITG 1233: Introduction to C++

BITG 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 information

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Convenient 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 information

The C++ Language. Arizona State University 1

The 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 information

Streams in C++ Stream concept. Reference information. Stream type declarations

Streams 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 information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

COMP322 - Introduction to C++

COMP322 - 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 information

What we will learn about this week:

What we will learn about this week: What we will learn about this week: Streams Basic file I/O Tools for Stream I/O Manipulators Character I/O Get and Put EOF function Pre-defined character functions Objects 1 I/O Streams as an Introduction

More information

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Chapter 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 information

Setting Justification

Setting 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 information

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

UEE1303(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 information

Today 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 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 information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 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 information

LECTURE 02 INTRODUCTION TO C++

LECTURE 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 information

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018 More File Operations Lecture 17 COP 3014 Spring 2018 april 18, 2018 eof() member function A useful member function of the input stream classes is eof() Stands for end of file Returns a bool value, answering

More information

C++ does not, as a part of the language, define how data are sent out and read into the program

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

by Pearson Education, Inc. All Rights Reserved. 2

by 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 information

Chapter 12 - C++ Stream Input/Output

Chapter 12 - C++ Stream Input/Output Chapter 12 - C++ Stream Input/Output 1 12.1 Introduction 12.2 Streams 12.2.1 Classic Streams vs. Standard Streams 12.2.2 iostream Library Header Files 12.2.3 Stream Input/Output Classes and Objects 12.3

More information

Chapter 4 - Notes Control Structures I (Selection)

Chapter 4 - Notes Control Structures I (Selection) Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by

More information

Object Oriented Programming Using C++ UNIT-3 I/O Streams

Object Oriented Programming Using C++ UNIT-3 I/O Streams File - The information / data stored under a specific name on a storage device, is called a file. Stream - It refers to a sequence of bytes. Text file - It is a file that stores information in ASCII characters.

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

CSc 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 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 information

Chapter 2: Introduction to C++

Chapter 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 information

Understanding main() function Input/Output Streams

Understanding main() function Input/Output Streams Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout

More information

CS201 Solved MCQs.

CS201 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 information

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

Chapter 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 information

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-

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

More information

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

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

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

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

2.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 information

Chapter 11 Customizing I/O

Chapter 11 Customizing I/O Chapter 11 Customizing I/O Bjarne Stroustrup www.stroustup.com/programming Overview Input and output Numeric output Integer Floating point File modes Binary I/O Positioning String streams Line-oriented

More information

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Physics 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 information

Introduction to C ++

Introduction to C ++ Introduction to C ++ Thomas Branch tcb06@ic.ac.uk Imperial College Software Society October 18, 2012 1 / 48 Buy Software Soc. s Free Membership at https://www.imperialcollegeunion.org/shop/ club-society-project-products/software-products/436/

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

BITG 1113: Files and Stream LECTURE 10

BITG 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 information

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

3.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 information