Input/Output Streams • I/O is a sequence of bytes, called a - TopicsExpress



          

Input/Output Streams • I/O is a sequence of bytes, called a stream of bytes, from the source to the destination. • The bytes are usually characters, unless the program requires other types of information such as a graphic image or digital speech. • A stream is a sequence of characters from the source to the destination. Input Stream: A sequence of characters from an input device to the computer. Output Stream: A sequence of characters from the computer to an output device. I/O STREAMS AND STANDARD I/O DEVICES • To extract (that is, receive) data from keyboard and send output to the screen, every C++ program must use the header file iostream. • This header file, iostream, contains definitions of two data types, istream (input stream) and ostream (output stream). • The header file, iostream, contains the declaration of two variables, cin (stands for common input), pronounced see-in, and cout (stands for common output), pronounced see-out, and the declaration is similar to the following C++ statements: istream cin; ostream cout; • To use cin and cout every C++ program must use the preprocessor directive #include • Variables of the type istream are called input stream variables. • Variables of the type ostream are called output stream variables. • A stream variable is either an input stream variable or an output stream variable. cin and the Extraction Operator (>>) Consider the following C++ statement: cin>>payRate; If you type 15.50, the value stored in payRate after the execution of this statement is 15.50. • The extraction operator >> is binary. The left-hand operand is an input stream variable such as cin. The right-hand operand is a variable of a simple data type. • The purpose of an input statement is to read and store values in a memory location and only variables refer to memory locations, the items (that is, right hand operand in the case of the extraction operator, >>) must be a variable in an input statement. • The syntax of an input statement using cin and the extraction operator >> is cin>>variable>>variable...; • Every occurrence of >> extracts the next data item from the input stream. • You can read both payRate and hoursWorked via a single cin statement by using the following code: cin>>payRate>>hoursWorked; • There is no difference between the preceding cin statement and the following two cin statements. cin>>payRate; cin>>hoursWorked; • When scanning for the next input, >> skips all whitespaces. • Whitespace characters consist of blanks and certain nonprintable characters, such as tabs and the newline character. • Every occurrence of >> extracts the next data item from the input stream. • You can read both payRate and hoursWorked via a single cin statement by using the following code: cin>>payRate>>hoursWorked; • There is no difference between the preceding cin statement and the following two cin statements. cin>>payRate; cin>>hoursWorked; • When scanning for the next input, >> skips all whitespaces. • Whitespace characters consist of blanks and certain nonprintable characters, such as tabs and the newline character. Whether the input is 15.50 48.30 or 15.50 48.30 or 15.50 48.30 The input statement: cin>>payRate>>hoursWorked; would store 15.50 in payRate and 48.30 in hoursWorked. • Suppose the input is 2. How does >> distinguish between character 2 and the number 2? • This is distinguished by the right hand operand of >>. • If the right hand operand, that is, variable, is of type char, input 2 is treated as character 2 (recall that in this case, the ASCII value of 2 will be stored). And if the right-hand operand is of the type int (or double), input 2 is treated as the number 2. • Now consider the input 25 and the statement: cin>>a; where a is a variable of some simple data type. • If a is of the data type char, then only the single character 2 will be stored in a. • If a is of the data type, say int, then 25 will be stored in a. • If a is of the type double, then the input 25 is converted to a decimal number with zero decimal part. Consider the statement cin>>a; where a is a variable of some simple data type. • When reading data into a char variable, after skipping any leading whitespace characters, the extraction operator >> finds and stores only the next character; reading stops after a single character. • To read data into an int or double variable, after skipping all leading whitespace characters and reading the plus or minus sign (if any), the extraction operator >> reads the digits of the number, including the decimal point for floating-point variables, and stops when it finds a whitespace character or a character other than a digit. Example 3-1 int a,b; double z; char ch,ch1,ch2; Statement Input Value Stored in Memory 1 cin>>ch; A ch=A 2 cin>>ch; AB ch=A, B is held for later input 3 cin>>a; 48 a=48 4 cin>>a; 46.35 a=46, .35 is held for later input 5 cin>>z; 74.35 z=74.35 6 cin>>z; 39 z=39.0 7 cin>>z>>a; 65.78 38 z=65.78, a=38 8 cin>>a>>b; 4 60 a=4, b=60 9 cin>>a>>ch>>z; 57 A 26.9 a=57, ch=A, z=26.9 16 cin>>a>>z; 46 32.4 68 a=46, z=32.4, 68 is held for later input 17 cin>>ch>>a; 256 ch=2, a=56 18 cin>>a>>ch; 256 a=256, computer waits for the input value for ch. 19 cin>>ch1>>ch2; A B ch1 = A, ch2 = B’ • During program execution, when entering character data such as letters, you do not enter the single quotes around the character. • Entering a char value into an int or double variable causes serious errors, called input failure. cin and the get Function Consider the declaration: char ch1, ch2; int num; and the input A 25 Now consider the statement: cin>>ch1>>ch2>>num; • When the computer executes this statement, A is stored in ch1, blank is skipped by >>, 2 is stored in ch2, and 5 is stored in num. • If we intended to store A in ch1, blank in ch2 and 25 in num? It is clear that we can not use the extraction operator >>. Now, again, consider the input A 25 We can effectively use the get function as follows: cin.get(ch1); cin.get(ch2); cin>>num; to store A in ch1, blank in ch2, and 25 in num. The above set of statements is equivalent to the following: cin>>ch1; cin.get(ch2); cin>>num; Consider the following statement: cin.ignore(100,\n); The execution of this statement will ignore the next 100 characters or until the newline character is found whichever comes first. The execution of the statement: cin.ignore(100,A); will result in ignoring the first 100 characters or until the character A is found, whichever comes first. Example 3-2 int a,b; Suppose the input is: 25 67 89 43 72 12 78 34 Consider the statements: cin>>a; cin.ignore(100,\n); cin>>b; • The first statement cin>>a; stores 25 in a. • The second statement, cin.ignore(100,\n); discards all of the remaining numbers in the first line. • The third statement cin>>b; stores 12 (from the next line) in b. Example 3-3 char ch1,ch2; Suppose the input is Hello there. My name is Mickey. Now consider the statements: cin>>ch1; cin.ignore(100,.); cin>>ch2; • The first statement cin>>ch1; stores H in ch1. • The second statement, cin.ignore(100,. ); results in ignoring all characters until . (period). • The third statement cin>>ch2; stores M (from the same line) in ch2. Input/Output and the string Type • You can use an input stream variable, such as cin, and the extraction operator >> to read a string into a variable of the data type string. • If the input is the string Shelly, the following code stores this input into the string variable name: string name; // declaration cin>>name; // input statement • The extraction operator skips any leading whitespace characters and that reading stops at a whitespace character. • You cannot use the extraction operator to read strings that contain blanks. File Input/Output File: An area in secondary storage used to hold information. For file I/O, the following steps are necessary. 1. Include the header file fstream in the program. So the following statement is needed. #include 2. Declare file (fstream) variables. For example the statements: ifstream inData; ofstream outData; declare the variable inData for input and outData for output. That is, inData is an input (file) stream variable and outData is an output (file) stream variable. Problem Analysis and Algorithm Design To find the average of five test scores, first we add the five test scores and then divide the sum by 5. Now, the input data is in the form: student ID followed by five test scores. Therefore, we first read the student ID and then the five test scores. This discussion translates in the following algorithm: 1. Read student ID and the five test score. 2. Output student ID and five test scores. 3. Calculate the average. 4. Output the average. We will output the average test score in the fixed decimal format with two decimal places. Variables: ifstream inFile; //input file stream variable ofstream outFile; //output file stream variable int test1, test2, test3, test4, test5; // variables //to read five test scores double average; //variable to store average //test score char studentId; //variable to store //student ID Main Algorithm 1. Declare the variables. 2. Open the input file. 3. Open the output file. 4. To output the floating-point numbers in a fixed decimal format with a decimal point and trailing zeros, set the manipulators fixed and showpoint. Also, to output the floating-point numbers with two decimal places, set the precision to two decimal places. 5. Read the student ID. 6. Output the student ID. 7. Read the five test scores. 8. Output the five test scores. 9. Find the average test score. 10. Output the average test score. 11. Close the input and output files. //Program to calculate average test score. #include #include #include using namespace std; int main() { //Declare variables; Step 1 ifstream inFile; //input file stream variable ofstream outFile; //output file stream variable int test1, test2, test3, test4, test5; double average; char studentId; inFile.open(a:test.txt); //Step 2 outFile.open(a:testavg.out); //Step 3 outFile
Posted on: Fri, 15 Nov 2013 14:38:23 +0000

Trending Topics



Recently Viewed Topics




© 2015