Code
I created the following program:
#include <iostream>
void f(char v[])
{
for(char* p = v; *p != 0; p++)
{
std::cout << *p << std::endl;
}
}
int main()
{
std::cout << int(' ') << std::endl;
char c[256];
std::cin >> c;
f(c);
return 0;
}
Compiler
I compiled with GNU GCC Compiler in Code::Blocks with the -std=c++0x flag (with no warnings).
Problem
The problem I have is that if I enter a string containing one or more spaces (i.e. “one and two”) then only the letters prior to the first space are printed (i.e. ‘o’, ‘n’, ‘e’).
What I’ve tried
The only thing that sprung to mind was to do a quick reality check. I added the line:
std::cout << "Val: " << int(' ') << std::endl;
and, as expected, the value printed isn’t 0 (because, obviously, it’s not the null ‘\0’ character)…
I’m not sure what I’m missing. Is it the case that the line:
std::cin >> c;
uses ‘ ‘ as some sort of termination character?
Thanks.
will only read one word delimited by whitespace. That’s the functionality for all the
>>operators.If you want to read a whole line, use the
getlinefunction instead.