Online Book Reader

Home Category

Professional C__ - Marc Gregoire [271]

By Root 1100 0
is reached. Note that in command-line environments, the end-of-file is indicated by the user typing a particular character. In Unix and Linux, it is Control+D, in Windows it is Control+Z. The exact character is operating system dependent, so you will need to know what your operating system requires:

cout << "Enter numbers on separate lines to add. "

<< "Use Control+D to finish (Control+Z in Windows)." << endl;

int sum = 0;

if (!cin.good()) {

cerr << "Standard input is in a bad state!" << endl;

return 1;

}

int number;

while (true) {

cin >> number;

if (cin.good()) {

sum += number;

} else if (cin.eof()) {

break; // Reached end of file

} else {

// Error!

cin.clear(); // Clear the error state.

string badToken;

cin >> badToken; // Consume the bad input.

cerr << "WARNING: Bad input encountered: " << badToken << endl;

}

}

cout << "The sum is " << sum << endl;

Code snippet from ErrorCheck\ErrorCheck.cpp

Input Manipulators

The built-in input manipulators, described in the list that follows, can be sent to an input stream to customize the way that data is read.

boolalpha and noboolalpha. If boolalpha is used, the string false will be interpreted as a Boolean value false; anything else will be treated as the Boolean value true. If noboolalpha is set, 0 will be interpreted as false, anything else as true. The default is noboolalpha.

hex, oct, and dec. Reads numbers in hexadecimal, octal, and base 10, respectively.

skipws and noskipws. Tells the stream to either skip white space when tokenizing or to read in white space as its own token.

ws. A handy manipulator that simply skips over the current series of white space at the present position in the stream.

get_money. Reads a money amount from a stream.

get_time. Reads a formatted time from a stream.

Input is locale aware. For example, take the following code, which enables your system locale for cin. Locales are discussed in Chapter 14:

cin.imbue(locale(""));

int i;

cin >> i;

If your system locale is U.S. English, you can enter 1,000 and it will be parsed as 1000. On the other hand, if your system locale is Dutch Belgium, you should enter 1.000 to get the value of 1000.

Input and Output with Objects

You can use the << operator to output a C++ string even though it is not a basic type. In C++, objects are able to prescribe how they are output and input. This is accomplished by overloading the << and >> operator to understand a new type or class.

Why would you want to overload these operators? If you are familiar with the printf() function in C, you know that it is not flexible in this area. printf() knows about several types of data, but there really isn’t a way to give it additional knowledge. For example, consider the following simple class:

class Muffin

{

public:

string getDescription() const;

void setDescription(const string& inDesc);

int getSize() const;

void setSize(int inSize);

bool getHasChocolateChips() const;

void setHasChocolateChips(bool inChips);

protected:

string mDesc;

int mSize;

bool mHasChocolateChips;

};

string Muffin::getDescription() const { return mDesc; }

void Muffin::setDescription(const string& inDesc) { mDesc = inDesc; }

int Muffin::getSize() const { return mSize; }

void Muffin::setSize(int inSize) { mSize = inSize; }

bool Muffin::getHasChocolateChips() const { return mHasChocolateChips; }

void Muffin::setHasChocolateChips(bool inChips) { mHasChocolateChips = inChips; }

Code snippet from Muffin\Muffin.cpp

To output an object of class Muffin by using printf(), it would be nice if you could specify it as an argument, perhaps using %m as a placeholder:

printf("Muffin output: %m\n", myMuffin); // BUG! printf doesn't understand Muffin.

Unfortunately, the printf() function knows nothing about the Muffin type and is unable to output an object of type Muffin. Worse still, because of the way the printf() function is declared, this will result in a run-time error, not a compile-time error (though a good compiler will give you a warning).

The best you can do with printf() is to add a new output() method

Return Main Page Previous Page Next Page

®Online Book Reader