Vous êtes sur la page 1sur 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
//of the istream object. The state is represented by members (data members)
//of type ios_base::iostate. Possible values.
ios_base::goodbit
failbit
badbit
eofbit
//these are the possible values, OKAY?

//it's complicated to test bits, 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, basically
//bad is worse in some sense
eof()

//Example:
int n;
cin >> n; //y default, >> skips leading white space, 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++, n retains its previous value. 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, all further input


// operation will fail & basically won't do anything.

//We can use the clear() method to put the stream back into a good state.
//Make sure to call clear() after failure.
//It's IMPORTANT to clear()! Or it may not read again!

/* Example: Summing integers */


int n, sum = 0;
while(1) {
cout << "Enter an integer:";
cin >> n;
if(cin.fail())
break;
sum += n;
}
cout << sum << endl;
//The problem is the same as using setf instead of manipulator.
//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.g.: cin is false if & only if cin.fail() returns true
//The above can be rewritten as:
while(1) {
cout << "Enter an integer:";
if(!(cin >> n)) //extra brackets due to precedence
//i.e. if you can't read the n, stop
break;
sum += n;
}
//If we don't need to print the prompt, this becomes:
while(cin >> n)
sum += n;
//by making cin behave like a truth value, code becomes simple
//how they make this, 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,'\n') //throws away 20 chars or until '\n' is thrown away
// whichever comes first

//this is still fairly primitive... what should we do?


//recall that for interactive input, we should read line by line
//there is a getline method
//unfortunately, the getline method is not easy to use
char line[128];
cin.getline(line, 128);
//the problem here is we don't know how long the line is, like with fgets
//unfortunately, this can fail in two different ways
//-*-nothing is read, when we're at the end-of-file
//-*-the buffer is not large enough for the line

Vous aimerez peut-être aussi