/* ** $Id: io.cc 824 2008-03-09 19:23:52Z phf $ ** ** Some "mixed" examples of input and output using the C++ ** library. */ #include #include #include // :-) const double PI = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679; int main() { // The << operator is overloaded for lots of different types, // so we can chain together output of mixed type as follows: std::cout << "There are " << 12 << " lectures\n left! " << PI << " " << '*' << std::endl; // This time the "<<" operator is also overloaded for *general* // pointers. So we can do the following: std::cout << std::cout << std::endl; // This needs to be read carefully: We are printing std::cout // *to* std::cout and then we're printing std::endl as well. // Yes, it turns out that "std::cout" is a pointer to something // called an "ostream" which is equivalent to a write-only file // from the old stdio.h in C. // Note that the output is in a format useful for pointers. In // general, the C++ output library is easier to use in most cases // (since you don't have to remember printf-style format codes). // However, if you don't like the defaults, things get somewhat // more complicated... // There are so-called I/O manipulators that allow us to make // changes to the formatting; here's just one example: std::cout << "Default: " << PI << std::endl << std::setprecision(10) << "High Precision: " << PI << std::endl << std::setprecision(3) << "Low Precision: " << PI << std::endl; // Feel free to read up on other I/O manipulators, should be // easy to google for since C++ is the only language I recall // using these things. But I may be wrong... :-) // We can read from standard input into a string; each time // around the loop we'll get the next "word" and the loop // will stop on "end-of-file" because of some C++ magic we // didn't talk about yet. std::string buffer; while( std::cin >> buffer ) { std::cout << buffer << std::endl; } // We can also read into variables of other types, ints for // example; try that yourself, and try what happens if you // don't enter an integer... Note: You may not be able to // add code for this here since the loop above stopped on EOF; // for some reason, on some platforms, you can't use the // std::cin.clear() method to reset the EOF state... :-/ // Finally, here's a limitation of the C++ way of doing output, // we'll address it later. Say you have a struct like this: struct xyz { int a; int b; } abc; abc.a = 10; abc.b = 20; // The "<<" operator is overloaded only for a certain set of // types by default. If we introduce new types, there's no // version for the type in the standard library. So printing // "abc" as follows does *not* work: // // std::cout << abc << std::endl; // // Again, it doesn't work because the type of "abc" is "xyz" // which was not known to the people who wrote the standard // library. We can, however, print the components of "abc" // since they are just "int" again: std::cout << abc.a << std::endl << abc.b << std::endl; // Trust your government... :-) return 0; }