CPS120: Introduction to Computer Science Lecture 15 B Data Files.
CPS120: Introduction to Computer Science
-
Upload
teegan-thornton -
Category
Documents
-
view
41 -
download
1
description
Transcript of CPS120: Introduction to Computer Science
![Page 1: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/1.jpg)
CPS120: Introduction to Computer Science
INPUT/OUTPUT
![Page 2: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/2.jpg)
Input/Output Structures
In our pseudocode algorithms we have used the expressions Read and Write or Prompt
High-level languages view input data as a stream of characters divided into lines
![Page 3: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/3.jpg)
Input/Output Structures
The key to the processing is in the data type that determines how characters are to be converted to a bit pattern (input) and how a bit pattern is to be converted to characters (output)
![Page 4: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/4.jpg)
Console I/O
console I/O refers to using the keyboard and screen as the standard input and output devices, respectively.
![Page 5: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/5.jpg)
C-In (cin)
cin is a stream of data flowing from an input device such as the keyboard (considered to be the standard input device) into your program.
![Page 6: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/6.jpg)
Input OperationsThe operator >> is known as the input operator. It is also known as the extraction operator
You use the input operator in statements like,
cin >> numItems;
which would allow the user to input a value to be stored in the variable numItems.
![Page 7: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/7.jpg)
Multiple Input
cin to allows the user to input several items with one C++ statement. The user however must type one or more spaces between each separate inputted value Example:
cin >> value1 >> value2 >> value3; Inputs need to have spaces or tabs between them (can’t use a comma to delimit C++)However, this prevents you from properly giving the user individual prompt messages
![Page 8: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/8.jpg)
Output Operations
The operator << is known as the output operator. It is also known as the insertion operator
You use the output operator in statements like:
cout << "Hello world";
![Page 9: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/9.jpg)
C-Out (cout)
cout is a stream which represents data flowing from one place to another. The statement:
cout << "Hello world"; causes data to flow from your program to the screen. The stream, cout, leads to what your computer considers to be its standard output device (usually the screen)
![Page 10: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/10.jpg)
Menus and Prompting
// Prompt for values
cout << "What was your beginning mileage? ";
cin >> StartingMileage;
cout << "What was your ending mileage? ";
cin >> EndingMileage;
cout << "How many gallons of gas did you use? ";
cin >> GallonsOfGas;
cout << "How much did one gallon of gas cost? ";
cin >> PriceOfGas;
![Page 11: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/11.jpg)
Inputting Words>> can be used but once it hits a space or a tab, the rest of the string is ignored
Use the get function for strings that contain ‘white space’String ends when you press enter
![Page 12: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/12.jpg)
Complexities of Word InputSome things are done automatically with >>
get does not skip over line breaks and spaces
If the user enters a string longer than the length specified in the call to the get function, the remaining characters are left in the input streamGet always ignores the new line character (‘\n’) and leaves it in the streamUse the ignore function to flush the contents of the input stream
cin.ignore(80, ‘\n’);
![Page 13: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/13.jpg)
Line Spacing
In order to end a line in an output statement you may use the new line character, \n, instead of endl. Examples:cout << "Hello world" << '\n';cout << "Hello world" << "\n";cout << "Hello world\n";
These are practically equivalent to: cout << "Hello world" << endl;
![Page 14: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/14.jpg)
Escape Sequences
Other useful "escape sequences" (since the \ is the escape operator) are: \t to generate a tab
\\ to print a backslash
\' to print a single quote
\" to print a double quote
![Page 15: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/15.jpg)
Using setf and unsetfEach stream has format options that can be changed
OPTION DESCRIPTIONleft Left-justifies the output
right Right-justifies the output
showpoint Displays decimal point and trailing zeros for floats
uppercase Displays e in scientific as E
showpos Displays a leading plus sign
scientific Displays floating point number scientifically
fixed Displays floating-point in normal notation
![Page 16: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/16.jpg)
Using Format Options
Format options are set immediately prior to the COUT statement
float x = 24.0;
cout << x << ‘\n’; // displays 24
cout.setf(ios::showpoint);
cout << x << ‘\n’; // displays 24.00000
cout.unsetf(ios::showpoint);
cout << x << ‘\n’; // displays 24
![Page 17: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/17.jpg)
Using Manipulators
You must include the <iomanip.h> header file at the top of your program in order to use the setprecision, setw, and other manipulators. You must use place the following compiler directive at the top of your program.#include <iomanip.h>
I/O manipulators are placed directly in the output statementcout << setprecision(2) << price << ‘\n’;
![Page 18: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/18.jpg)
Setting Precision
The setprecision manipulator allows you to limit the number of digits that are displayed when a numeric data type is displayed:
cout << setprecision(2) << price << '\n';
only allows the leading two digits of the value stored in the variable, price, to be displayed
![Page 19: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/19.jpg)
More Precisely
If the fixed format was set previously with the statement:
cout.setf(ios::fixed);then the setprecision(2) manipulator would have the effect of rounding or truncating price (and all future floating-point values in the cout stream) to the hundredths place
![Page 20: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/20.jpg)
Field Width
The setw manipulator controls the width of the field when displaying a value. The statement:
cout << setw(10) << umEndow << endl;
sets the width of the field allocated for the variable, umEndow, to 10 characters
![Page 21: CPS120: Introduction to Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081513/56812c58550346895d90e0ba/html5/thumbnails/21.jpg)
Formatted Output
cout.setf(ios : : fixed)
Print “fixed point” form, not in exponential form
cout.setf(ios : : showpoint)
Says to always print the decimal point
cout.precison(2)
Says to print out the two most significant decimal digits, after rounding to this precision