Online Book Reader

Home Category

Professional C__ - Marc Gregoire [270]

By Root 1552 0
ch;

cin >> noskipws;

while (cin >> ch) {

if (isdigit(ch)) {

cin.unget();

if (cin.fail())

cout << "unget() failed" << endl;

break;

}

guestName += ch;

}

// Read partysize

cin >> partySize;

cout << "Thank you '" << guestName

<< "', party of " << partySize << endl;

if (partySize > 10) {

cout << "An extra gratuity will apply." << endl;

}

}

Code snippet from Unget\Unget.cpp

putback()

putback(), like unget(), lets you move backward by one character in an input stream. The difference is that the putback() method takes the character being placed back on the stream as a parameter:

char ch1;

cin >> ch1;

cin.putback(ch1);

// ch1 will be the next character read off the stream.

peek()

The peek() method allows you to preview the next value that would be returned if you were to call get(). To take the chute metaphor perhaps a bit too far, you could think of it as looking up the chute without a value actually falling down it.

peek() is ideal for any situation where you need to look ahead before reading a value. For example, the following code implements the getReservationData() function that allows white space in the name, but uses peek() instead of unget():

void getReservationData()

{

string guestName;

int partySize = 0;

// Read letters until we find a non-letter

char ch;

cin >> noskipws;

while (true) {

// 'peek' at next character

ch = cin.peek();

if (!cin.good())

break;

if (isdigit(ch)) {

// next character will be a digit, so stop the loop

break;

}

// next character will be a non-digit, so read it

cin >> ch;

guestName += ch;

}

// Read partysize

cin >> partySize;

cout << "Thank you '" << guestName

<< "', party of " << partySize << endl;

if (partySize > 10) {

cout << "An extra gratuity will apply." << endl;

}

}

Code snippet from Peek\Peek.cpp

getline()

Obtaining a single line of data from an input stream is so common that a method exists to do it for you. The getline() method fills a character buffer with a line of data up to the specified size. The specified size includes the \0 character. Thus, the following code will read a maximum of kBufferSize-1 characters from cin, or until an end-of-line sequence is read:

char buffer[kBufferSize];

cin.getline(buffer, kBufferSize);

Code snippet from Getline\Getline.cpp

When getline() is called, it reads a line from the input stream, up to and including the end-of-line sequence. However, the end-of-line character or characters do not appear in the string. Note that the end-of-line sequence is platform dependent. For example, it can be \r\n, or \n, or \n\r.

There is a form of get() that performs the same operation as getline(), except that it leaves the newline sequence in the input stream.

There is also a function called getline() that can be used with C++ strings. It is defined in the header file and is in the std namespace. It takes a stream reference, a string reference, and an optional delimiter as parameters:

string myString;

std::getline(cin, myString);

Code snippet from Getline\Getline.cpp

Handling Input Errors

Input streams have a number of methods to detect unusual circumstances. Most of the error conditions related to input streams occur when there is no data available to read. For example, the end of stream (referred to as end-of-file, even for non-file streams) may have been reached. The most common way of querying the state of an input stream is to access it within a conditional. For example, the following loop will keep looping as long as cin remains in a good state:

while (cin) { ... }

You can input data at the same time:

while (cin >> ch) { ... }

You can also call the good() method, just like on output streams. There is also an eof() method that returns true if the stream has reached its end.

You should also get in the habit of checking the stream state after reading data so that you can recover from bad input.

The following program shows the common pattern for reading data from a stream and handling errors. The program reads numbers from standard input and displays their sum once end-of-file

Return Main Page Previous Page Next Page

®Online Book Reader