"space character"

I define
char A[50];
, then write
....
cin>>A;
cout<<A;

Why if i write, for example,
abcd efghi
as an input for A, then it will show
abcd
?
I mean, why after "space character", another character will be null character?

How can it show
abcd efghi
?
That is because cin stops writing after a space = ( delinmiter )
To fix this just use cin.get()

1
2
3
4
5
6
7
8
9
#include <iostream>
int main()
{
	using namespace std;
	char name[20];
	cin.get(name,19);
	cout << "Name : " << name;
	return 0;
}

For user input, you should be using getline() with strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
  {
  string name;

  cout << "Please enter your full name> " << flush;
  getline( cin, name );

  cout << "\nHello " << name << ".\nCan I call you "
       << name.substr( 0, name.find_first_of( " \t" ) ) << "?\n";

  return 0;
  }

Hope this helps.

[edit] There is also the istream::getline() function, which accepts input into a char[].
Last edited on
Topic archived. No new replies allowed.