You are on page 1of 3

/*

* File: 20160912_CPP_L03.cpp.
* Title: 2016-09-12 C++ Lesson 03
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/

/* Reminder: Manipulators */
cout << hex;
//hex is the name of a function, i.e. a function pointer
//names of functions can be used as function pointers

/* * inheritance */
ios_base
/\
|
basic_ios<> //special name: ios
//<> because it's a class template
/\ /\
| |
basic_istream<> basic_ostream<>
istream ostream

//ios_base has the following method:
ios_base operator<<(ios_base&(*pf)(ios_base&)) { //a method in ios_base class
//let's read the parameter type: pointer to a function
//the function takes an ios_base by reference
//returns ios_base by reference
/** reference vs. pointers */
(*pf)(*this); // *this is a pointer to the current object
return *this;
}

ios_base& hex(ios_base& b) {
b.setf(ios_base::hex, ios_base::basefield);
return b;
}

cout << hex // ---> hex(cout)
//remember: cout is a kind of ios_base
//---> cout.setf(ios_base::hex, ios_base::basefield)

/* Input */

//Input more complicated than output because it may fail.
//Output generally doesn't fail.
cin //pre-defined istream object associated with standard input
//e.g.:
int m, n;
cin >> m >> n; //tries to read 2 ints
//>> is called the extraction operator or input operator
int m;
double d;
cin >> m >> d;
//Input can fail. To detect failure, we need to look at the state

The state is represented by members (data members) //of type ios_base::iostate. cin >> n. >> skips leading white space. basically //bad is worse in some sense eof() //Example: int n. OKAY? //it's complicated to test bits. //The problem is the same as using setf instead of manipulator.fail()) break. Possible values.//of the istream object. similarly to scanf //TABLE: let's say the input is " 123hello" (initial space) //input n failbit eobit // //" 123hello" 123 x x //hello 0* YES x // //*in previous versions of C++. ios_base::goodbit failbit badbit eofbit //these are the possible values. while(1) { cout << "Enter an integer:". cin >> n. //y default. //We can use the clear() method to put the stream back into a good state.fail() returns true //The above can be rewritten as: while(1) { . CAREFUL! //let's say input ends in end-of-file: "123/E" //"123\E" 123 x YES //"\E" 0* YES x //Note: if an istream is NOT in a good state. n retains its previous value. //It's IMPORTANT to clear()! Or it may not read again! /* Example: Summing integers */ int n.g. sum += n. //Make sure to call clear() after failure. } cout << sum << endl.: cin is false if & only if cin. if(cin. sum = 0. so what they do is to provide methods //The methods to test this are: good() fail() //test both failbit & badbit //this is implemented thus for more convenience bad() //bad means the stream is corrupted. //You're breaking up the flow of the program(!?) //We need to break up the flow(!?) /** (!?) Read book on input */ //it turns out that an istream can be used as a truth value //it is true if & only if fail() return false when applied to it //e. all further input // operation will fail & basically won't do anything.

this can fail in two different ways //-*-nothing is read. code becomes simple //how they make this.. the getline method is not easy to use char line[128]. when we're at the end-of-file //-*-the buffer is not large enough for the line . //by making cin behave like a truth value.'\n') //throws away 20 chars or until '\n' is thrown away // whichever comes first //this is still fairly primitive. stop break. if you can't read the n.e. we should read line by line //there is a getline method //unfortunately. like with fgets //unfortunately. 128). cout << "Enter an integer:". cin.getline(line. is complicated //there is a conversion operator somewhere // that doesn't convert to true or false // but rather *void // true / false end result is therefore dependent on pointer being null or not /* Discarding input */ //three different ways to do it: //-ignore method //---ignore() //throw away 1 char //---ignore(10) //throw away 10 chars //---ignore(20. } //If we don't need to print the prompt. //the problem here is we don't know how long the line is. this becomes: while(cin >> n) sum += n. what should we do? //recall that for interactive input.. sum += n. if(!(cin >> n)) //extra brackets due to precedence //i.