Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.

Similar presentations


Presentation on theme: "Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently."— Presentation transcript:

1 Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently used char functions Introduce enum types as a means of defining your own types! I/O Streams Using streams for I/O with files. Some functions for processing data files.

2 Integers n bits -> can represent 2 n values Example : in 5 bits one can represent 2 5 = 32 values TYPEbitssmallestlargest --------------------------------- int32-21474836472147483647 short16-3276732767 unsigned int3204294967295 long int32-21474836472147483647 unsigned long int3204294967295 char is formed as an 8 bit integer and can be used as as an integer (-127 to 127) or unsigned char (0 to 255) enum is also basically an int as we will see.

3 Casting types When the type isn't exactly what is needed, one can sometimes employ a cast to convert it to the proper type. Example: // will output // value of 5 / 2 is 2 cout << "value of 5 / 2 is " << (5 / 2) << endl; // will output // value of 5 / (float)2 is 2.5 cout << "value of 5 / (float) 2 is " << (5 / (float) 2) << endl; Others : int i; i = 'A'; // automatic conversion to integer 65 i = (int)'A'; // this is the same, but with cast i = int('A'); // uses the function form to cast // in all cases i gets the value 65

4 enum Lets you define a set of values.. A TYPE. Examples: enum day {sunday, monday, tuesday, wednesday,thursday, friday, saturday}; day appointment; // variable appointment // type is day appointment = monday; // VALUE assigned is monday What are they really?.... integer constants sunday = 0, monday = 1, tuesday=2, etc. What can you do with them? document anything you can do with an integer; What CAN'T you do with them? I/O (unless you only enter/expect integers) see Chapter 3, p. 165

5 Useful char functions In order to process input data, it is sometimes useful to be able to ask questions about what type of value it is. See p. 818-819 PrototypeDescription int isdigit (int c)Returns true if c is a digit, and false otherwise int isalpha (int c)Returns true if c is a letter, and false otherwise int isalnum (int c)Returns true if c is a digit or a letter, and false otherwise int isxdigit (int c)Returns true if c is a hexidecimal character, and false otherwise. int islower (int c)Returns true if c is a lowercase letter, and false otherwise int isupper (int c)Returns true if c is an uppercase, and false otherwise int tolower (int c)If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, toupper returns the argument unchanged. int toupper (int c)If c is an lowercase letter, toupper returns c as an uppercase letter. Otherwise, tolower returns the argument unchanged....

6 Useful char functions (cont.) cout A cout a.... isdigit('A')... // returns false.... isalpha('A')... // returns true.... islower('A')... // returns false.... isupper('A')... // returns true You need not be too concerned about these now. The need for these types of functions will come much later. You should simply be aware of their existence.

7 I/O Streams Remember REDIRECTION? It lets you read from a file! drake> a.out <inputfile We have substituted the disk file inputfile for the standard input file (keyboard) What if we wanted to read from more than one file at the same time? student registration file -> prepare bill -> tuition bill student info file -> Its really not much different than using cin and cout. Why call it a STREAM? All devices can be viewed simply as a stream of chars (like a river is a stream of water) whether it is an input or output stream.

8 An example of an I/O stream Copies an input file to an output file (cp!) like cp datafile outfile in unix

9 More on the stream functions See Chapter 11, section 11.3 - 11.4, pp. 604-614 open, get, put, eof, fail, close (these are enough for now) You can have as many file streams as variables (close) put and get are used for reading files a char at a time THEY DO NOT SKIP SPACES etc! You can use cin type input ( >> ) but it skips white space. Remember how cin does the same? To read three integers from each file above: Use >> or << for numbers and put get for char

10 cerr What if you wanted to output an error but the person was redirecting the output to a file? drake> a.out >outfile YOU WOULDN'T SEE THE ERROR! The cerr stream is a special kind of output which would NOT be redirected to the standard output device. For example: ins.open("datafile"); if (ins.fail()) {cerr << "UNABLE TO OPEN DATAFILE.TERMINATING\n"; return 1; } Whether the user redirects or not, this would print on the screen if the file did not exist. USE cerr TO OUTPUT ERROR MESSAGES!

11 Output Manipulators cout and any ofstream

12 Input Manipulators cin and any ifstream


Download ppt "Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently."

Similar presentations


Ads by Google