Professional C__ - Marc Gregoire [268]
// Simulate "%06d" with streams
printf("This should be '000123': %06d\n", i);
cout << "This should be '000123': " << setfill('0') << setw(6) << i << endl;
// Fill with *
cout << "This should be '***123': " << setfill('*') << setw(6) << i << endl;
// Reset fill character
cout << setfill(' ');
// Floating point values
double dbl = 1.452;
double dbl2 = 5;
cout << "This should be ' 5': " << setw(2) << noshowpoint << dbl2 << endl;
cout << "This should be @@1.452: " << setw(7) << setfill('@') << dbl << endl;
// Format numbers according to your location
cout.imbue(locale(""));
cout << "This is 1234567 formatted according to your location: " << 1234567 << endl;
// C++11 put_money:
cout << "This should be a money amount of 1200, "
<< "formatted according to your location: "
<< put_money("120000") << endl;
// C++11 put_time:
time_t tt;
time(&tt);
tm t;
localtime_s(&t, &tt);
cout << "This should be the current date and time "
<< "formatted according to your location: "
<< put_time(&t, "%c") << endl;
Code snippet from Manipulator\Manipulator.cpp
If you don’t care for the concept of manipulators, you can usually get by without them. Streams provide much of the same functionality through equivalent methods like precision(). For example, take the following line:
cout << "This should be '1.2346': " << setprecision(5) << 1.234567 << endl;
This can be converted to use a method call as follows:
cout.precision(5);
cout << "This should be '1.2346': " << 1.23456789 << endl;
Code snippet from Manipulator\Manipulator.cpp
See the Standard Library Reference resource on the website for details.
Input with Streams
Input streams provide a simple way to read in structured or unstructured data. In this section, the techniques for input are discussed within the context of cin, the console input stream.
Input Basics
There are two easy ways to read data by using an input stream. The first is an analog of the << operator that outputs data to an output stream. The corresponding operator for reading data is >>. When you use >> to read data from an input stream, the variable you provide is the storage for the received value. For example, the following program reads one word from the user and puts it into a string. Then the string is output back to the console:
string userInput;
cin >> userInput;
cout << "User input was " << userInput << endl;
Code snippet from Input\string.cpp
By default, the >> operator will tokenize values according to white space. For example, if a user runs the previous program and enters hello there as input, only the characters up to the first white space character (the space character in this instance) will be captured into the userInput variable. The output would be as follows:
User input was hello
One solution to include white space in the input is to use get(), discussed later in this chapter.
The >> operator works with different variable types, just like the << operator. For example, to read an integer, the code differs only in the type of the variable:
int userInput;
cin >> userInput;
cout << "User input was " << userInput << endl;
Code snippet from Input\int.cpp
You can use input streams to read in multiple values, mixing and matching types as necessary. For example, the following function, an excerpt from a restaurant reservation system, asks the user for a last name and the number of people in their party:
void getReservationData()
{
string guestName;
int partySize;
cout << "Name and number of guests: ";
cin >> guestName >> partySize;
cout << "Thank you, " << guestName << "." << endl;
if (partySize > 10) {
cout << "An extra gratuity will apply." << endl;
}
}
Code snippet from Input\getReservationData.cpp
Note that the >> operator will tokenize values according to white space, so the getReservationData() function does not allow you to enter a name with white space. A solution using unget() is discussed later in this chapter. Note also that even though the use of cout does not explicitly flush the buffer using endl or flush(), the text will