Tuesday, July 28, 2009

C++ question please help.?

#include %26lt;string%26gt;


#include %26lt;vector%26gt;


#include %26lt;iostream%26gt;


#include %26lt;stdio.h%26gt;


//#include %26lt;ifstream%26gt;


using namespace std;








int main (int argc, char* argv[])


{





string fakefile;


cout %26lt;%26lt; "The file content is: ";


cin %26gt;%26gt; fakefile;


cout %26lt;%26lt; endl %26lt;%26lt; fakefile %26lt;%26lt; endl; //the body part


}





If I input "This is a string" for cin here, the output of the program is just "This". It seems that cin doesn't take space characters as part of the string, what can I do to solve the problem?

C++ question please help.?
See http://cppreference.com/cppstring/getlin...


Remarks:


1. #include %26lt;cstdio%26gt; , not #include %26lt;stdio.h%26gt; . Stdio.h is deprecated in modern C++.


2. To say that cin doesn't take spaces is misleading. cin is an object of istream, and it can definitely store spaces. It's the %26gt;%26gt; (extraction operator) that is in question here. It tries to extract everything into tokens (integers, strings, doubles, etc.). Whitespace is ignored. You can actually force %26gt;%26gt; to retain whitespace (see http://www.cplusplus.com/reference/iostr... ).


3. Don't post code directly here again. It's unreadable. Use http://www.rafb.net/paste/
Reply:you can use getline
Reply:try getline


No comments:

Post a Comment