Iostream Library and Basic I/O in C++ - Conclusion
(Page 4 of 4 )
All of this looks good, but what if you want to read a whole line of data? As you can see, the stream extraction will delimit each input flow by thewhitespaces it will find. This obviously makes it impossible to read a whole line. However, there is another way to accomplish this, by using the "getline" function.
The result will be put into a string, though you need to include thestringheader and declare it. The function is also declared in this header in the form of a template, making it possible to work for both narrow or wide characters.
#include <string>
...
string putInTo;
getline( cin, putInTo, ' n');
I have explicitly requested to use as delimiter the newline character, just to show you that as the delimiter, you may use any character, but by default, it has defined the newline one. There you go; you've just learned the basics of how to communicate with users in C/C++.
All that is left is to start playing with the compiler, experimenting with different situations, and getting used to the method. Don't you find this more practical than the standard I/O used in C/C++?
You can answer this question or ask for further clarification regarding this article in the blog found right under this statement, or you can join our friendly, ever-growing forum over atDevHardware, and let the community over there answer your questions. Until the next time we meet, live with passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |