Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input & Output: Console

Similar presentations

Presentation on theme: "Input & Output: Console"— Presentation transcript:

1 Input & Output: Console
Console – term is left over from days of mainframes and refers to the monitor display and keyboard entry Input from the keyboard (console input) cin >> marked_price ; (Note: input goes INTO the memory storage location defined as marked_price) Output to the monitor screen (console output) cout << “Sales tax: “ << tax << endl; (Note: Output goes OUT OF the memory storage location tax to the monitor screen – along with some literal text and an ‘insert end of line’ indicator) << and >> are ‘operators’ indicating a direction of flow (stream insertion operators where ‘stream’ means a sequence of data)

2 Variables and Literals
Fixed value, not subject to change during program execution Used to assign values to variables or output fixed content Conform to some standard type such as float, double, char, string, int Examples: , “Hello”, 28, ‘Y’ Variable Value can change during execution of the code Has a name defined by the programmer Has a specific ‘type’ defined by the programmer (i.e. int, float, double, char) Has a storage location in memory that is assigned by the computer at the time the program is executed. The programmer refers to this storage location in his/her program using the name he/she used to define it. All variables are represented in the storage location by a sequence of 1’s and 0’s

3 Escape Sequences The \ (backslash) character has a special significance. It is used to ‘escape’ or change the meaning of the character that follows it. The combination of the \ and the following character is called an ‘escape sequence’ \n Newline (does same as endl) \t Inserts a tab \\ Causes a \ to be printed \’ Causes a ‘ to be printed \” Causes a “ to be printed Note: Never put a space in the middle of an escape sequence

4 Example: Using Escape Sequences
//This is an example of a program that utilizes the console out object and escape sequences #include <iostream> using namespace std; int main() { cout << "Some Common Escape Sequences"; cout << endl; cout << "Newline\t\t\\n"; cout << "Tab\t\t\\t"; cout << “\n”; cout << "Backslash\t\\\\"; cout << "Single Quote\t\\\'"; cout << "Double Quote\t\\\""; return 0; }

5 Naming Variables Cannot use any of the C++ reserved (key) words (syntax) Should indicate the role of the variable in the program Should follow a naming convention (style) Variable names should be in all lower case with underscore between words ( Should be descriptive but moderate in length Names can only start with letter or underscore and may contain only letters, numbers and underscores Examples: body_weight_pounds or body_weight_lbs max_number_suitcases test_grade1 final_grade_average

6 Data Types & Variable Definitions
Program variables must be defined not only by a name but also by a data type. The type declaration statement determines how the value of the variable is stored. Below are typical storage sizes and ranges for different types. Integers: short 2 bytes -32,768 to +32,767 unsigned short 2 bytes 0 to 65,535 int 4 bytes -2,147,483,648 to +2, 147,483,647 unsigned int 4 bytes 0 to 4,294,967,295 long 4 bytes -2,147,483,648 to +2, 147,483,647 unsigned long 4 bytes 0 to 4,294,967,295

7 Typical vs. Guaranteed What storage size is guaranteed on all systems? int is at least as large as short int long int is as least as large as int unsigned short is the same size as short unsigned int is the same size as int unsigned long is the same size as long The sizeof special operator can be used to determine the storage size on any given computing platform.

8 Floating Point (Real Numbers)
Real numbers (floating point numbers) are stored in a format similar to scientific notation. Examples: 2,498, is x 106 where is called the mantissa and 6 is the exponent is x where is the mantissa and -3 is the exponent For computer storage purposes these numbers would be written as E6 and 34459E-3 A designated part of the allocated memory space is used for the mantissa and the rest for the exponent. One bit is used for the sign.

9 Floating Point Data Types
Below are the data types with storage sizes typically found on PCs. float 4 bytes 3.4E-38 to 3.4E38 double 8 bytes 1.7E-308 to 1.7E308 long double 8 bytes 1.7E-308 to 1.7E308 Note: Remember the computer actually stores both the mantissa and exponent in binary. Although the exponent being an integer can be stored exactly, the mantissa may not be able to be stored exactly and at some point will be truncated. Ex. .1 or 1/10 decimal = (binary) where … means the pattern repeats.

10 Char Data Type Characters are internally represented by numbers and thus can be thought of as a type of integer. Most systems use the ASCII (American Standard Code for Information Interchange) convention for the storage of characters. Strings are a sequence of characters stored in consecutive memory locations. We will be talking more about this later.

11 ASCII characters 0 to 127 Code 0 to 31 (and # 127) are non-printing, mostly obsolete control characters that affect how text is processed. There are 95 printable characters. To print one, press the ALT key (hold it down) and type the decimal number.

12 A popular variation of an extended ASCII set

13 Strings A string is a sequence of characters and is stored using the string ‘class’. You must include a preprocessor directive (#include <string>) if you want to use a string type variable. “”s must be around the literal assigned to a string variable. #include <string> string my_first_name, my_last_name; my_first_name = “Trish”; my_last_name = “Sumbera”’; The sequence of char that make up a string is stored in consecutive memory locations and the end is marked with a null (ASCII 0) character. Note: The ASCII code for the char 0 (zero) is 48. We use \0 to represent the null character. Also Note: char literals use single quotes. string literals use double quotes. ‘T’ takes up one byte of memory but “T” requires two bytes. Why?

14 bool Data Type bool (boolean) data have values of either ‘true’ or ‘false’ Needless to say this value is represented numerically where true is represented by 1 and false by 0

15 Named Constants Literals that may need to be changed in the future can be given a name. Use ALL_CAPS with words separated by _ (Note: This is a style issue and not part of the C++ language) Value cannot be changed while the program is running Defined using const <data type> <NAME> = <value>; Examples: const float PI = ; const int MAX_VACATION_HRS = 120; Are stored in a named memory location that is read-only #define preprocessor directive may be seen used in older programs Not stored in memory Text substitution occurs during preprocessor stage No defined data type

16 Binary Representation of Numbers
As a programmer you should know how the computer stores numerical data in memory. EVERYTHING is stored using 0’s and 1’s. Binary representation of numbers is essential to understanding data types and some of the nuances resulting from binary representation. Let’s look at binary vs. decimal and how integers are stored in memory.

Download ppt "Input & Output: Console"

Similar presentations

Ads by Google