11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 24: Using Stream I/O 325The result from a sample execution appears as follows:=============================Chester, 12345 56.60name = Chester,account = 12345, balance = 56.6Chester, 56.6 12345=============================Arthur, 34567 67.50name = Arthur,account = 34567, balance = 67.5Arthur, 67.5 34567=============================Trudie, 56x78 78.90Error parsing string=============================Valerie, 78901 89.10name = Valerie,account = 78901, balance = 89.1Valerie, 89.1 78901=============================Press any key to continue . . .Reflect a second before continuing. Notice how the program was able toresynch itself after the error in the input file. Notice, also, the simplicity ofthe heart of the program, the parseString() function. Consider what thisfunction would look like without the benefit of the istringstream class.Manipulating ManipulatorsYou can use stream I/O to output numbers and character strings by usingdefault formats. Usually the defaults are fine, but sometimes they don’t cut it.For example, I was less than tickled when the total from the result of a financialcalculation from a recent program appeared as 249.600006 rather than 249.6(or, better yet, 249.60). There must be a way to bend the defaults to my desires.True to form, C++ provides not one but two ways to control the format ofoutput.Depending on the default settings of your compiler, you may get 249.6 as youroutput. Nevertheless, you really want 249.60.First, you can control the format by invoking a series of member functions onthe stream object. For example, the number of significant digits to display isset by using the function precision() as follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!