   [c++ - simple] Problem printing char array
  I'm trying to print a char array that includes an inputted message in which a null character is part of the message (a space). So if the user types in "hello world", instead of printing just "hello" because the char array terminates at the space, I want to print the whole thing, "hello world".char message[256];cin>>message; // user types in "hello world"cout<<message;The output is just, "hello".What is the "correct" way to get around this?Thanks in advance   
  A space isn't a null character. Your problem is that >> only reads up to the first whitespace, so your array doesn't contain anything except the hello. Use std::getline() to read a line at a time.   ;  Getline: http://www.cplusplus.com/reference/iostream/istream/getline/   ;  Moving to For Beginners.Also, you should not be using raw char arrays; use std::string instead.   ;Quote:Original post by Chris ReynoldsI'm trying to print a char arrayIn C++, we use std::string to represent text.Quote:that includes an inputted message in which a null character is part of the message (a space).Spaces are not null characters. There is only one null character, the one with the value 0. Spaces have value 32 (assuming ASCII).Quote:So if the user types in "hello world", instead of printing just "hello" because the char array terminates at the spaceThe char array does not "terminate at the space". Even if the space were a null character, the array would not terminate at the null character; arrays have a size that is set ahead of time, and they keep that size no matter what.The output routine interprets the array contents such that the text ends with the first character in the array that is a null character. However, the space is not such a character.What has happened is that the read operation has stopped reading at the space, not put the space itself into the array, and then added a null terminator.If you use std::string, you can avoid having to think about null terminators. But reading into a std::string instance with operator>> will still read only up to the space, and will not make the space itself a part of the read-in string.Quote:I want to print the whole thing, "hello world".Then you must read it in.To do so, use the free function std::getline(), which will read from the specified stream into the specified std::string object, up to the first newline.Quote:What is the "correct" way to get around this?As above: use std::string, and read it with the free function std::getline().std::string message;std::getline(std::cin, message);std::cout << message;;  Another interesting thing that is related is the input buffer.  "world" is still in the input buffer.If you do another cin >> message after your cout, it will immediately store "world" in your 'message' array without user intervention because it's still in the input buffer.  If you do another cout, "world" will print.getline is the "correct" solution, but learning about the input buffer is potentially useful.   