#include <iostream>
using namespace std;
void main(){
char name[20];
cin>>name; // when I input "This is"
cout<<name<<endl; // output was "This"
}
How to do that when I input “This is” , output too will be “This is” , not only “This” ?
You could use
or
Depending on whether you want the new line character in your string.
EDIT:
If you want to further simplify your code you could use a string instead of a char array, you wouldn’t have to worry about the user exceeding your buffer size then ie.
EDIT 2:
As David Heffernan also pointed out in a comment I should explain why it’s not working for you as expected. The extraction operator (
operator >>) ends when a whitespace character — a space in your case — is reached. The operator also terminates when a null-character or the end of file is reached.